Back to index

glibc  2.9
bug-ungetc4.c
Go to the documentation of this file.
00001 /* Test program for ungetc/fseekpos interaction.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 
00023 static void do_prepare (void);
00024 #define PREPARE(argc, argv) do_prepare ()
00025 static int do_test (void);
00026 #define TEST_FUNCTION do_test ()
00027 #include "../test-skeleton.c"
00028 
00029 static const char pattern[] = "abcdefghijklmnopqrstuvwxyz";
00030 static char *temp_file;
00031 
00032 static void
00033 do_prepare (void)
00034 {
00035   int fd = create_temp_file ("bug-ungetc.", &temp_file);
00036   if (fd == -1)
00037     {
00038       printf ("cannot create temporary file: %m\n");
00039       exit (1);
00040     }
00041   write (fd, pattern, sizeof (pattern) - 1);
00042   close (fd);
00043 }
00044 
00045 #define TEST_POS 5
00046 
00047 static int
00048 do_one_test (int mode)
00049 {
00050   FILE *f = fopen (temp_file, "r");
00051   if (f == NULL)
00052     {
00053       printf ("%d: could not open temporary file: %m\n", mode);
00054       return 1;
00055     }
00056 
00057   int i;
00058   for (i = 0; i < TEST_POS; ++i)
00059     getc (f);
00060 
00061   fpos_t p;
00062   if (fgetpos (f, &p) != 0)
00063     {
00064       printf ("%d: fgetpos failed: %m\n", mode);
00065       return 1;
00066     }
00067 
00068   if (mode)
00069     {
00070       if (fseek (f, 0, SEEK_SET) != 0)
00071         {
00072           printf ("%d: fseek failed: %m\n", mode);
00073           return 1;
00074         }
00075 
00076       for (i = 0; i < TEST_POS - (mode >= 2); ++i)
00077         getc (f);
00078     }
00079 
00080   if (mode != 2 && ungetc ('X', f) != 'X')
00081     {
00082       printf ("%d: ungetc failed\n", mode);
00083       return 1;
00084     }
00085 
00086   if (mode == 3 && getc (f) != 'X')
00087     {
00088       printf ("%d: getc after ungetc did not return X\n", mode);
00089       return 1;
00090     }
00091 
00092   if (fsetpos (f, &p) != 0)
00093     {
00094       printf ("%d: fsetpos failed: %m\n", mode);
00095       return 1;
00096     }
00097 
00098   if (getc (f) != pattern[TEST_POS])
00099     {
00100       printf ("%d: getc did not return %c\n", mode, pattern[TEST_POS]);
00101       return 1;
00102     }
00103 
00104   fclose (f);
00105 
00106   return 0;
00107 }
00108 
00109 static int
00110 do_test (void)
00111 {
00112   int mode, ret = 0;
00113   for (mode = 0; mode <= 4; mode++)
00114     ret |= do_one_test (mode);
00115   return ret;
00116 }