]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - stdio-common/tst-fseek.c
sparc (64bit): Regenerate ulps
[thirdparty/glibc.git] / stdio-common / tst-fseek.c
index c714b66879716abc733f9b61e96b7f614b600fca..c4ac17cdf4e8ae6f1c9ed4a64ac0cd1746875fb6 100644 (file)
@@ -1,7 +1,6 @@
 /* Tests of fseek and fseeko.
-   Copyright (C) 2000 Free Software Foundation, Inc.
+   Copyright (C) 2000-2023 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
-   Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -14,9 +13,8 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 #include <error.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <time.h>
 #include <sys/stat.h>
 
 
-int
-main (void)
+static int
+do_test (void)
 {
   const char *tmpdir;
   char *fname;
@@ -62,7 +61,7 @@ main (void)
 
   if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("write error");
+      printf ("%d: write error\n", __LINE__);
       result = 1;
       goto out;
     }
@@ -70,248 +69,240 @@ main (void)
   /* The EOF flag must be reset.  */
   if (fgetc (fp) != EOF)
     {
-      puts ("managed to read at end of file");
+      printf ("%d: managed to read at end of file\n", __LINE__);
       result = 1;
     }
   else if (! feof (fp))
     {
-      puts ("EOF flag not set");
+      printf ("%d: EOF flag not set\n", __LINE__);
       result = 1;
     }
   if (fseek (fp, 0, SEEK_CUR) != 0)
     {
-      puts ("fseek(fp, 0, SEEK_CUR) failed");
+      printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
       result = 1;
     }
   else if (feof (fp))
     {
-      puts ("fseek() didn't reset EOF flag");
+      printf ("%d: fseek() didn't reset EOF flag\n", __LINE__);
       result = 1;
     }
 
   /* Do the same for fseeko().  */
-#ifdef USE_IN_LIBIO
     if (fgetc (fp) != EOF)
     {
-      puts ("managed to read at end of file");
+      printf ("%d: managed to read at end of file\n", __LINE__);
       result = 1;
     }
   else if (! feof (fp))
     {
-      puts ("EOF flag not set");
+      printf ("%d: EOF flag not set\n", __LINE__);
       result = 1;
     }
   if (fseeko (fp, 0, SEEK_CUR) != 0)
     {
-      puts ("fseek(fp, 0, SEEK_CUR) failed");
+      printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
       result = 1;
     }
   else if (feof (fp))
     {
-      puts ("fseek() didn't reset EOF flag");
+      printf ("%d: fseek() didn't reset EOF flag\n", __LINE__);
       result = 1;
     }
-#endif
 
   /* Go back to the beginning of the file: absolute.  */
   if (fseek (fp, 0, SEEK_SET) != 0)
     {
-      puts ("fseek(fp, 0, SEEK_SET) failed");
+      printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
       result = 1;
     }
   else if (fflush (fp) != 0)
     {
-      puts ("fflush() failed");
+      printf ("%d: fflush() failed\n", __LINE__);
       result = 1;
     }
   else if (lseek (fd, 0, SEEK_CUR) != 0)
     {
-      puts ("lseek() returned different position");
+      printf ("%d: lseek() returned different position\n", __LINE__);
       result = 1;
     }
   else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("fread() failed");
+      printf ("%d: fread() failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
     {
-      puts ("content after fseek(,,SEEK_SET) wrong");
+      printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
       result = 1;
     }
 
-#ifdef USE_IN_LIBIO
   /* Now with fseeko.  */
   if (fseeko (fp, 0, SEEK_SET) != 0)
     {
-      puts ("fseeko(fp, 0, SEEK_SET) failed");
+      printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
       result = 1;
     }
   else if (fflush (fp) != 0)
     {
-      puts ("fflush() failed");
+      printf ("%d: fflush() failed\n", __LINE__);
       result = 1;
     }
   else if (lseek (fd, 0, SEEK_CUR) != 0)
     {
-      puts ("lseek() returned different position");
+      printf ("%d: lseek() returned different position\n", __LINE__);
       result = 1;
     }
   else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("fread() failed");
+      printf ("%d: fread() failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
     {
-      puts ("content after fseeko(,,SEEK_SET) wrong");
+      printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
       result = 1;
     }
-#endif
 
   /* Go back to the beginning of the file: relative.  */
-  if (fseek (fp, -(sizeof (outstr) - 1), SEEK_CUR) != 0)
+  if (fseek (fp, -((int) sizeof (outstr) - 1), SEEK_CUR) != 0)
     {
-      puts ("fseek(fp, 0, SEEK_SET) failed");
+      printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
       result = 1;
     }
   else if (fflush (fp) != 0)
     {
-      puts ("fflush() failed");
+      printf ("%d: fflush() failed\n", __LINE__);
       result = 1;
     }
   else if (lseek (fd, 0, SEEK_CUR) != 0)
     {
-      puts ("lseek() returned different position");
+      printf ("%d: lseek() returned different position\n", __LINE__);
       result = 1;
     }
   else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("fread() failed");
+      printf ("%d: fread() failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
     {
-      puts ("content after fseek(,,SEEK_SET) wrong");
+      printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
       result = 1;
     }
 
-#ifdef USE_IN_LIBIO
   /* Now with fseeko.  */
-  if (fseeko (fp, -(sizeof (outstr) - 1), SEEK_CUR) != 0)
+  if (fseeko (fp, -((int) sizeof (outstr) - 1), SEEK_CUR) != 0)
     {
-      puts ("fseeko(fp, 0, SEEK_SET) failed");
+      printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
       result = 1;
     }
   else if (fflush (fp) != 0)
     {
-      puts ("fflush() failed");
+      printf ("%d: fflush() failed\n", __LINE__);
       result = 1;
     }
   else if (lseek (fd, 0, SEEK_CUR) != 0)
     {
-      puts ("lseek() returned different position");
+      printf ("%d: lseek() returned different position\n", __LINE__);
       result = 1;
     }
   else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("fread() failed");
+      printf ("%d: fread() failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
     {
-      puts ("content after fseeko(,,SEEK_SET) wrong");
+      printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
       result = 1;
     }
-#endif
 
   /* Go back to the beginning of the file: from the end.  */
-  if (fseek (fp, -(sizeof (outstr) - 1), SEEK_END) != 0)
+  if (fseek (fp, -((int) sizeof (outstr) - 1), SEEK_END) != 0)
     {
-      puts ("fseek(fp, 0, SEEK_SET) failed");
+      printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
       result = 1;
     }
   else if (fflush (fp) != 0)
     {
-      puts ("fflush() failed");
+      printf ("%d: fflush() failed\n", __LINE__);
       result = 1;
     }
   else if (lseek (fd, 0, SEEK_CUR) != 0)
     {
-      puts ("lseek() returned different position");
+      printf ("%d: lseek() returned different position\n", __LINE__);
       result = 1;
     }
   else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("fread() failed");
+      printf ("%d: fread() failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
     {
-      puts ("content after fseek(,,SEEK_SET) wrong");
+      printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
       result = 1;
     }
 
-#ifdef USE_IN_LIBIO
   /* Now with fseeko.  */
-  if (fseeko (fp, -(sizeof (outstr) - 1), SEEK_END) != 0)
+  if (fseeko (fp, -((int) sizeof (outstr) - 1), SEEK_END) != 0)
     {
-      puts ("fseeko(fp, 0, SEEK_SET) failed");
+      printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
       result = 1;
     }
   else if (fflush (fp) != 0)
     {
-      puts ("fflush() failed");
+      printf ("%d: fflush() failed\n", __LINE__);
       result = 1;
     }
   else if (lseek (fd, 0, SEEK_CUR) != 0)
     {
-      puts ("lseek() returned different position");
+      printf ("%d: lseek() returned different position\n", __LINE__);
       result = 1;
     }
   else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("fread() failed");
+      printf ("%d: fread() failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
     {
-      puts ("content after fseeko(,,SEEK_SET) wrong");
+      printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
       result = 1;
     }
-#endif
 
   if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("write error 2");
+      printf ("%d: write error 2\n", __LINE__);
       result = 1;
       goto out;
     }
 
   if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("write error 3");
+      printf ("%d: write error 3\n", __LINE__);
       result = 1;
       goto out;
     }
 
   if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("write error 4");
+      printf ("%d: write error 4\n", __LINE__);
       result = 1;
       goto out;
     }
 
   if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
     {
-      puts ("write error 5");
+      printf ("%d: write error 5\n", __LINE__);
       result = 1;
       goto out;
     }
 
   if (fputc ('1', fp) == EOF || fputc ('2', fp) == EOF)
     {
-      puts ("cannot add characters at the end");
+      printf ("%d: cannot add characters at the end\n", __LINE__);
       result = 1;
       goto out;
     }
@@ -319,7 +310,7 @@ main (void)
   /* Check the access time.  */
   if (fstat64 (fd, &st1) < 0)
     {
-      puts ("fstat64() before fseeko() failed\n");
+      printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__);
       result = 1;
     }
   else
@@ -328,7 +319,7 @@ main (void)
 
       if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_CUR) != 0)
        {
-         puts ("fseek() after write characters failed");
+         printf ("%d: fseek() after write characters failed\n", __LINE__);
          result = 1;
          goto out;
        }
@@ -342,27 +333,27 @@ main (void)
 
          if (fstat64 (fd, &st2) < 0)
            {
-             puts ("fstat64() after fseeko() failed\n");
+             printf ("%d: fstat64() after fseeko() failed\n\n", __LINE__);
              result = 1;
            }
          if (st1.st_ctime >= t)
            {
-             puts ("st_ctime not updated");
+             printf ("%d: st_ctime not updated\n", __LINE__);
              result = 1;
            }
          if (st1.st_mtime >= t)
            {
-             puts ("st_mtime not updated");
+             printf ("%d: st_mtime not updated\n", __LINE__);
              result = 1;
            }
          if (st1.st_ctime >= st2.st_ctime)
            {
-             puts ("st_ctime not changed");
+             printf ("%d: st_ctime not changed\n", __LINE__);
              result = 1;
            }
          if (st1.st_mtime >= st2.st_mtime)
            {
-             puts ("st_mtime not changed");
+             printf ("%d: st_mtime not changed\n", __LINE__);
              result = 1;
            }
        }
@@ -371,7 +362,7 @@ main (void)
   if (fread (buf, 1, 2 + 2 * (sizeof (outstr) - 1), fp)
       != 2 + 2 * (sizeof (outstr) - 1))
     {
-      puts ("reading 2 records plus bits failed");
+      printf ("%d: reading 2 records plus bits failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0
@@ -380,23 +371,23 @@ main (void)
           || buf[2 * (sizeof (outstr) - 1)] != '1'
           || buf[2 * (sizeof (outstr) - 1) + 1] != '2')
     {
-      puts ("reading records failed");
+      printf ("%d: reading records failed\n", __LINE__);
       result = 1;
     }
   else if (ungetc ('9', fp) == EOF)
     {
-      puts ("ungetc() failed");
+      printf ("%d: ungetc() failed\n", __LINE__);
       result = 1;
     }
   else if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_END) != 0)
     {
-      puts ("fseek after ungetc failed");
+      printf ("%d: fseek after ungetc failed\n", __LINE__);
       result = 1;
     }
   else if (fread (buf, 1, 2 + 2 * (sizeof (outstr) - 1), fp)
       != 2 + 2 * (sizeof (outstr) - 1))
     {
-      puts ("reading 2 records plus bits failed");
+      printf ("%d: reading 2 records plus bits failed\n", __LINE__);
       result = 1;
     }
   else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0
@@ -404,22 +395,86 @@ main (void)
                      sizeof (outstr) - 1) != 0
           || buf[2 * (sizeof (outstr) - 1)] != '1')
     {
-      puts ("reading records for the second time failed");
+      printf ("%d: reading records for the second time failed\n", __LINE__);
       result = 1;
     }
   else if (buf[2 * (sizeof (outstr) - 1) + 1] == '9')
     {
-      puts ("unget character not ignored");
+      printf ("%d: unget character not ignored\n", __LINE__);
       result = 1;
     }
   else if (buf[2 * (sizeof (outstr) - 1) + 1] != '2')
     {
-      puts ("unget somehow changed character");
+      printf ("%d: unget somehow changed character\n", __LINE__);
       result = 1;
     }
 
+  fclose (fp);
+
+  fp = fopen (fname, "r");
+  if (fp == NULL)
+    {
+      printf ("%d: fopen() failed\n\n", __LINE__);
+      result = 1;
+    }
+  else if (fstat64 (fileno (fp), &st1) < 0)
+    {
+      printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__);
+      result = 1;
+    }
+  else if (fseeko (fp, 0, SEEK_END) != 0)
+    {
+      printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
+      result = 1;
+    }
+  else if (ftello (fp) != st1.st_size)
+    {
+      printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
+             (size_t) st1.st_size, (size_t) ftello (fp));
+      result = 1;
+    }
+  else
+    printf ("%d: SEEK_END works\n", __LINE__);
+  if (fp != NULL)
+    fclose (fp);
+
+  fp = fopen (fname, "r");
+  if (fp == NULL)
+    {
+      printf ("%d: fopen() failed\n\n", __LINE__);
+      result = 1;
+    }
+  else if (fstat64 (fileno (fp), &st1) < 0)
+    {
+      printf ("%d: fstat64() before fgetc() failed\n\n", __LINE__);
+      result = 1;
+    }
+  else if (fgetc (fp) == EOF)
+    {
+      printf ("%d: fgetc() before fseeko() failed\n\n", __LINE__);
+      result = 1;
+    }
+  else if (fseeko (fp, 0, SEEK_END) != 0)
+    {
+      printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
+      result = 1;
+    }
+  else if (ftello (fp) != st1.st_size)
+    {
+      printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
+             (size_t) st1.st_size, (size_t) ftello (fp));
+      result = 1;
+    }
+  else
+    printf ("%d: SEEK_END works\n", __LINE__);
+  if (fp != NULL)
+    fclose (fp);
+
  out:
   unlink (fname);
 
   return result;
 }
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"