Use binutils 2.41 branch in build-many-glibcs.py
[glibc.git] / libio / bug-ungetc3.c
blob6100d7a9360f26c9a5a0c64123862e81c8a16a01
1 /* Test program for ungetc/ftell interaction bug. */
3 #include <stdio.h>
5 #include <support/xunistd.h>
7 static void do_prepare (void);
8 #define PREPARE(argc, argv) do_prepare ()
9 static int do_test (void);
10 #define TEST_FUNCTION do_test ()
11 #include "../test-skeleton.c"
13 static const char pattern[] = "12345";
14 static char *temp_file;
16 static void
17 do_prepare (void)
19 int fd = create_temp_file ("bug-ungetc.", &temp_file);
20 if (fd == -1)
22 printf ("cannot create temporary file: %m\n");
23 exit (1);
25 xwrite (fd, pattern, sizeof (pattern));
26 close (fd);
29 static int
30 do_one_test (int mode)
32 FILE *f;
34 f = fopen (temp_file, "r");
35 if (f == NULL)
37 printf ("could not open temporary file: %m\n");
38 return 1;
41 if (mode == 1 && ftell (f) != 0)
43 printf ("first ftell returned wrong position %ld\n", ftell (f));
44 return 1;
47 if (fgetc (f) != '1' || fgetc (f) != '2')
49 puts ("fgetc failed");
50 return 1;
53 if (mode == 2 && ftell (f) != 2)
55 printf ("second ftell returned wrong position %ld\n", ftell (f));
56 return 1;
59 if (ungetc ('6', f) != '6')
61 puts ("ungetc failed");
62 return 1;
65 if (ftell (f) != 1)
67 printf ("third ftell returned wrong position %ld\n", ftell (f));
68 return 1;
71 if (fgetc (f) != '6')
73 puts ("fgetc failed");
74 return 1;
77 if (ftell (f) != 2)
79 printf ("fourth ftell returned wrong position %ld\n", ftell (f));
80 return 1;
83 fclose (f);
85 return 0;
88 static int
89 do_test (void)
91 int mode;
92 for (mode = 0; mode <= 2; mode++)
93 if (do_one_test (mode))
94 return 1;
95 return 0;