Update copyright notices with scripts/update-copyrights
[glibc.git] / libio / bug-ungetc4.c
blob2dc82d6cdfe5f1b9445cec7c21ee89f63e7c7d96
1 /* Test program for ungetc/fseekpos interaction.
2 Copyright (C) 2004-2014 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 <http://www.gnu.org/licenses/>. */
20 #include <stdio.h>
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;
31 static void
32 do_prepare (void)
34 int fd = create_temp_file ("bug-ungetc.", &temp_file);
35 if (fd == -1)
37 printf ("cannot create temporary file: %m\n");
38 exit (1);
40 write (fd, pattern, sizeof (pattern) - 1);
41 close (fd);
44 #define TEST_POS 5
46 static int
47 do_one_test (int mode)
49 FILE *f = fopen (temp_file, "r");
50 if (f == NULL)
52 printf ("%d: could not open temporary file: %m\n", mode);
53 return 1;
56 int i;
57 for (i = 0; i < TEST_POS; ++i)
58 getc (f);
60 fpos_t p;
61 if (fgetpos (f, &p) != 0)
63 printf ("%d: fgetpos failed: %m\n", mode);
64 return 1;
67 if (mode)
69 if (fseek (f, 0, SEEK_SET) != 0)
71 printf ("%d: fseek failed: %m\n", mode);
72 return 1;
75 for (i = 0; i < TEST_POS - (mode >= 2); ++i)
76 getc (f);
79 if (mode != 2 && ungetc ('X', f) != 'X')
81 printf ("%d: ungetc failed\n", mode);
82 return 1;
85 if (mode == 3 && getc (f) != 'X')
87 printf ("%d: getc after ungetc did not return X\n", mode);
88 return 1;
91 if (fsetpos (f, &p) != 0)
93 printf ("%d: fsetpos failed: %m\n", mode);
94 return 1;
97 if (getc (f) != pattern[TEST_POS])
99 printf ("%d: getc did not return %c\n", mode, pattern[TEST_POS]);
100 return 1;
103 fclose (f);
105 return 0;
108 static int
109 do_test (void)
111 int mode, ret = 0;
112 for (mode = 0; mode <= 4; mode++)
113 ret |= do_one_test (mode);
114 return ret;