lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Test program for ungetc/fseekpos interaction. |
| 2 | Copyright (C) 2004-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2004. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | |
| 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" |
| 27 | |
| 28 | static const char pattern[] = "abcdefghijklmnopqrstuvwxyz"; |
| 29 | static char *temp_file; |
| 30 | |
| 31 | static void |
| 32 | do_prepare (void) |
| 33 | { |
| 34 | int fd = create_temp_file ("bug-ungetc.", &temp_file); |
| 35 | if (fd == -1) |
| 36 | { |
| 37 | printf ("cannot create temporary file: %m\n"); |
| 38 | exit (1); |
| 39 | } |
| 40 | write (fd, pattern, sizeof (pattern) - 1); |
| 41 | close (fd); |
| 42 | } |
| 43 | |
| 44 | #define TEST_POS 5 |
| 45 | |
| 46 | static int |
| 47 | do_one_test (int mode) |
| 48 | { |
| 49 | FILE *f = fopen (temp_file, "r"); |
| 50 | if (f == NULL) |
| 51 | { |
| 52 | printf ("%d: could not open temporary file: %m\n", mode); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | int i; |
| 57 | for (i = 0; i < TEST_POS; ++i) |
| 58 | getc (f); |
| 59 | |
| 60 | fpos_t p; |
| 61 | if (fgetpos (f, &p) != 0) |
| 62 | { |
| 63 | printf ("%d: fgetpos failed: %m\n", mode); |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | if (mode) |
| 68 | { |
| 69 | if (fseek (f, 0, SEEK_SET) != 0) |
| 70 | { |
| 71 | printf ("%d: fseek failed: %m\n", mode); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | for (i = 0; i < TEST_POS - (mode >= 2); ++i) |
| 76 | getc (f); |
| 77 | } |
| 78 | |
| 79 | if (mode != 2 && ungetc ('X', f) != 'X') |
| 80 | { |
| 81 | printf ("%d: ungetc failed\n", mode); |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | if (mode == 3 && getc (f) != 'X') |
| 86 | { |
| 87 | printf ("%d: getc after ungetc did not return X\n", mode); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | if (fsetpos (f, &p) != 0) |
| 92 | { |
| 93 | printf ("%d: fsetpos failed: %m\n", mode); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | if (getc (f) != pattern[TEST_POS]) |
| 98 | { |
| 99 | printf ("%d: getc did not return %c\n", mode, pattern[TEST_POS]); |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | fclose (f); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | do_test (void) |
| 110 | { |
| 111 | int mode, ret = 0; |
| 112 | for (mode = 0; mode <= 4; mode++) |
| 113 | ret |= do_one_test (mode); |
| 114 | return ret; |
| 115 | } |