.
[glibc/history.git] / libio / bug-ungetc.c
blob51940b68f529ca5fc6f7289888bb0ed548d63a26
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_test (void)
30 int i;
31 FILE *f;
32 char buf[10];
33 long offset, diff;
34 int result = 0;
36 f = fopen (temp_file, "rw");
38 rewind (f);
39 for (i = 0; i < 3; i++)
40 printf ("%c\n", getc (f));
41 offset = ftell (f);
42 printf ("offset = %ld\n", offset);
43 if (ungetc ('4', f) != '4')
45 printf ("ungetc failed\n");
46 abort ();
48 printf ("offset after ungetc = %ld\n", ftell (f));
50 i = fread ((void *) buf, 4, (size_t) 1, f);
51 printf ("read %d (%c), offset = %ld\n", i, buf[0], ftell (f));
52 diff = ftell (f) - offset;
53 if (diff != 3)
55 printf ("ftell did not update properly. got %ld, expected 3\n", diff);
56 result = 1;
58 fclose (f);
60 return result;