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_one_test (int mode)
31{
32 FILE *f;
33
34 f = fopen (temp_file, "r");
35 if (f == NULL)
36 {
37 printf (format: "could not open temporary file: %m\n");
38 return 1;
39 }
40
41 if (mode == 1 && ftell (stream: f) != 0)
42 {
43 printf (format: "first ftell returned wrong position %ld\n", ftell (stream: f));
44 return 1;
45 }
46
47 if (fgetc (stream: f) != '1' || fgetc (stream: f) != '2')
48 {
49 puts (s: "fgetc failed");
50 return 1;
51 }
52
53 if (mode == 2 && ftell (stream: f) != 2)
54 {
55 printf (format: "second ftell returned wrong position %ld\n", ftell (stream: f));
56 return 1;
57 }
58
59 if (ungetc ('6', f) != '6')
60 {
61 puts (s: "ungetc failed");
62 return 1;
63 }
64
65 if (ftell (stream: f) != 1)
66 {
67 printf (format: "third ftell returned wrong position %ld\n", ftell (stream: f));
68 return 1;
69 }
70
71 if (fgetc (stream: f) != '6')
72 {
73 puts (s: "fgetc failed");
74 return 1;
75 }
76
77 if (ftell (stream: f) != 2)
78 {
79 printf (format: "fourth ftell returned wrong position %ld\n", ftell (stream: f));
80 return 1;
81 }
82
83 fclose (f);
84
85 return 0;
86}
87
88static int
89do_test (void)
90{
91 int mode;
92 for (mode = 0; mode <= 2; mode++)
93 if (do_one_test (mode))
94 return 1;
95 return 0;
96}
97

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