2.9
[glibc/nacl-glibc.git] / libio / bug-ungetc3.c
blob0c83c1161eb8a0658ded5b135d5f36dd739d4643
1 /* Test program for ungetc/ftell interaction bug. */
3 #include <stdio.h>
5 static void do_prepare (void);
6 #define PREPARE(argc, argv) do_prepare ()
7 static int do_test (void);
8 #define TEST_FUNCTION do_test ()
9 #include "../test-skeleton.c"
11 static const char pattern[] = "12345";
12 static char *temp_file;
14 static void
15 do_prepare (void)
17 int fd = create_temp_file ("bug-ungetc.", &temp_file);
18 if (fd == -1)
20 printf ("cannot create temporary file: %m\n");
21 exit (1);
23 write (fd, pattern, sizeof (pattern));
24 close (fd);
27 static int
28 do_one_test (int mode)
30 FILE *f;
32 f = fopen (temp_file, "r");
33 if (f == NULL)
35 printf ("could not open temporary file: %m\n");
36 return 1;
39 if (mode == 1 && ftell (f) != 0)
41 printf ("first ftell returned wrong position %ld\n", ftell (f));
42 return 1;
45 if (fgetc (f) != '1' || fgetc (f) != '2')
47 puts ("fgetc failed");
48 return 1;
51 if (mode == 2 && ftell (f) != 2)
53 printf ("second ftell returned wrong position %ld\n", ftell (f));
54 return 1;
57 if (ungetc ('6', f) != '6')
59 puts ("ungetc failed");
60 return 1;
63 if (ftell (f) != 1)
65 printf ("third ftell returned wrong position %ld\n", ftell (f));
66 return 1;
69 if (fgetc (f) != '6')
71 puts ("fgetc failed");
72 return 1;
75 if (ftell (f) != 2)
77 printf ("fourth ftell returned wrong position %ld\n", ftell (f));
78 return 1;
81 fclose (f);
83 return 0;
86 static int
87 do_test (void)
89 int mode;
90 for (mode = 0; mode <= 2; mode++)
91 if (do_one_test (mode))
92 return 1;
93 return 0;