1 /* Test program for ungetc/fseekpos interaction.
2 Copyright (C) 2004-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
22 static void do_prepare (void);
23 #define PREPARE(argc, argv) do_prepare ()
24 static int do_test (void);
25 #define TEST_FUNCTION do_test ()
26 #include "../test-skeleton.c"
28 static const char pattern
[] = "abcdefghijklmnopqrstuvwxyz";
29 static char *temp_file
;
34 int fd
= create_temp_file ("bug-ungetc.", &temp_file
);
37 printf ("cannot create temporary file: %m\n");
40 write (fd
, pattern
, sizeof (pattern
) - 1);
47 do_one_test (int mode
)
49 FILE *f
= fopen (temp_file
, "r");
52 printf ("%d: could not open temporary file: %m\n", mode
);
57 for (i
= 0; i
< TEST_POS
; ++i
)
61 if (fgetpos (f
, &p
) != 0)
63 printf ("%d: fgetpos failed: %m\n", mode
);
69 if (fseek (f
, 0, SEEK_SET
) != 0)
71 printf ("%d: fseek failed: %m\n", mode
);
75 for (i
= 0; i
< TEST_POS
- (mode
>= 2); ++i
)
79 if (mode
!= 2 && ungetc ('X', f
) != 'X')
81 printf ("%d: ungetc failed\n", mode
);
85 if (mode
== 3 && getc (f
) != 'X')
87 printf ("%d: getc after ungetc did not return X\n", mode
);
91 if (fsetpos (f
, &p
) != 0)
93 printf ("%d: fsetpos failed: %m\n", mode
);
97 if (getc (f
) != pattern
[TEST_POS
])
99 printf ("%d: getc did not return %c\n", mode
, pattern
[TEST_POS
]);
112 for (mode
= 0; mode
<= 4; mode
++)
113 ret
|= do_one_test (mode
);