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

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