1 /* Test program for ungetc/fseekpos interaction.
2 Copyright (C) 2004 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 static void do_prepare (void);
24 #define PREPARE(argc, argv) do_prepare ()
25 static int do_test (void);
26 #define TEST_FUNCTION do_test ()
27 #include "../test-skeleton.c"
29 static const char pattern
[] = "abcdefghijklmnopqrstuvwxyz";
30 static char *temp_file
;
35 int fd
= create_temp_file ("bug-ungetc.", &temp_file
);
38 printf ("cannot create temporary file: %m\n");
41 write (fd
, pattern
, sizeof (pattern
) - 1);
48 do_one_test (int mode
)
50 FILE *f
= fopen (temp_file
, "r");
53 printf ("%d: could not open temporary file: %m\n", mode
);
58 for (i
= 0; i
< TEST_POS
; ++i
)
62 if (fgetpos (f
, &p
) != 0)
64 printf ("%d: fgetpos failed: %m\n", mode
);
70 if (fseek (f
, 0, SEEK_SET
) != 0)
72 printf ("%d: fseek failed: %m\n", mode
);
76 for (i
= 0; i
< TEST_POS
- (mode
>= 2); ++i
)
80 if (mode
!= 2 && ungetc ('X', f
) != 'X')
82 printf ("%d: ungetc failed\n", mode
);
86 if (mode
== 3 && getc (f
) != 'X')
88 printf ("%d: getc after ungetc did not return X\n", mode
);
92 if (fsetpos (f
, &p
) != 0)
94 printf ("%d: fsetpos failed: %m\n", mode
);
98 if (getc (f
) != pattern
[TEST_POS
])
100 printf ("%d: getc did not return %c\n", mode
, pattern
[TEST_POS
]);
113 for (mode
= 0; mode
<= 4; mode
++)
114 ret
|= do_one_test (mode
);