1/* Test program for ungetc/ftell interaction bug. */
2
3#include <stdio.h>
4
5static void do_prepare (void);
6#define PREPARE(argc, argv) do_prepare ()
7static int do_test (void);
8#define TEST_FUNCTION do_test ()
9#include <test-skeleton.c>
10
11static const char pattern[] = "12345";
12static char *temp_file;
13
14static void
15do_prepare (void)
16{
17 int fd = create_temp_file (base: "bug-ungetc.", filename: &temp_file);
18 if (fd == -1)
19 {
20 printf (format: "cannot create temporary file: %m\n");
21 exit (1);
22 }
23 write (fd, pattern, sizeof (pattern));
24 close (fd: fd);
25}
26
27static int
28do_test (void)
29{
30 int i;
31 FILE *f;
32 char buf[10];
33 long offset, diff;
34 int result = 0;
35
36 f = fopen (temp_file, "rw");
37
38 rewind (f);
39 for (i = 0; i < 3; i++)
40 printf (format: "%c\n", getc (stream: f));
41 offset = ftell (stream: f);
42 printf (format: "offset = %ld\n", offset);
43 if (ungetc ('4', f) != '4')
44 {
45 printf (format: "ungetc failed\n");
46 abort ();
47 }
48 printf (format: "offset after ungetc = %ld\n", ftell (stream: f));
49
50 i = fread (ptr: (void *) buf, size: 4, n: (size_t) 1, stream: f);
51 printf (format: "read %d (%c), offset = %ld\n", i, buf[0], ftell (stream: f));
52 diff = ftell (stream: f) - offset;
53 if (diff != 3)
54 {
55 printf (format: "ftell did not update properly. got %ld, expected 3\n", diff);
56 result = 1;
57 }
58 fclose (f);
59
60 return result;
61}
62

source code of glibc/libio/bug-ungetc.c