[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / libio / bug-ungetc3.c
1 /* Test program for ungetc/ftell interaction bug.  */
2
3 #include <stdio.h>
4
5 #include <support/xunistd.h>
6
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"
12
13 static const char pattern[] = "12345";
14 static char *temp_file;
15
16 static void
17 do_prepare (void)
18 {
19   int fd = create_temp_file ("bug-ungetc.", &temp_file);
20   if (fd == -1)
21     {
22       printf ("cannot create temporary file: %m\n");
23       exit (1);
24     }
25   xwrite (fd, pattern, sizeof (pattern));
26   close (fd);
27 }
28
29 static int
30 do_one_test (int mode)
31 {
32   FILE *f;
33
34   f = fopen (temp_file, "r");
35   if (f == NULL)
36     {
37       printf ("could not open temporary file: %m\n");
38       return 1;
39     }
40
41   if (mode == 1 && ftell (f) != 0)
42     {
43       printf ("first ftell returned wrong position %ld\n", ftell (f));
44       return 1;
45     }
46
47   if (fgetc (f) != '1' || fgetc (f) != '2')
48     {
49       puts ("fgetc failed");
50       return 1;
51     }
52
53   if (mode == 2 && ftell (f) != 2)
54     {
55       printf ("second ftell returned wrong position %ld\n", ftell (f));
56       return 1;
57     }
58
59   if (ungetc ('6', f) != '6')
60     {
61       puts ("ungetc failed");
62       return 1;
63     }
64
65   if (ftell (f) != 1)
66     {
67       printf ("third ftell returned wrong position %ld\n", ftell (f));
68       return 1;
69     }
70
71   if (fgetc (f) != '6')
72     {
73       puts ("fgetc failed");
74       return 1;
75     }
76
77   if (ftell (f) != 2)
78     {
79       printf ("fourth ftell returned wrong position %ld\n", ftell (f));
80       return 1;
81     }
82
83   fclose (f);
84
85   return 0;
86 }
87
88 static int
89 do_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 }