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