Back to index

glibc  2.9
tst-truncate.c
Go to the documentation of this file.
00001 /* Tests for ftruncate and truncate.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
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 <errno.h>
00022 #include <error.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #include <sys/stat.h>
00026 
00027 
00028 /* Allow testing of the 64-bit versions as well.  */
00029 #ifndef TRUNCATE
00030 # define TRUNCATE truncate
00031 # define FTRUNCATE ftruncate
00032 #endif
00033 
00034 #define STRINGIFY(s) STRINGIFY2 (s)
00035 #define STRINGIFY2(s) #s
00036 
00037 /* Prototype for our test function.  */
00038 extern void do_prepare (int argc, char *argv[]);
00039 extern int do_test (int argc, char *argv[]);
00040 
00041 /* We have a preparation function.  */
00042 #define PREPARE do_prepare
00043 
00044 /* We might need a bit longer timeout.  */
00045 #define TIMEOUT 20 /* sec */
00046 
00047 /* This defines the `main' function and some more.  */
00048 #include <test-skeleton.c>
00049 
00050 /* These are for the temporary file we generate.  */
00051 char *name;
00052 int fd;
00053 
00054 void
00055 do_prepare (int argc, char *argv[])
00056 {
00057    size_t name_len;
00058 
00059 #define FNAME FNAME2(TRUNCATE)
00060 #define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"
00061 
00062    name_len = strlen (test_dir);
00063    name = malloc (name_len + sizeof (FNAME));
00064    mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
00065    add_temp_file (name);
00066 
00067    /* Open our test file.   */
00068    fd = mkstemp (name);
00069    if (fd == -1)
00070      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
00071 }
00072 
00073 
00074 int
00075 do_test (int argc, char *argv[])
00076 {
00077   struct stat st;
00078   char buf[1000];
00079 
00080   memset (buf, '\0', sizeof (buf));
00081 
00082   if (write (fd, buf, sizeof (buf)) != sizeof (buf))
00083     error (EXIT_FAILURE, errno, "during write");
00084 
00085   if (fstat (fd, &st) < 0 || st.st_size != sizeof (buf))
00086     error (EXIT_FAILURE, 0, "initial size wrong");
00087 
00088 
00089   if (FTRUNCATE (fd, 800) < 0)
00090     error (EXIT_FAILURE, errno, "size reduction with %s failed",
00091           STRINGIFY (FTRUNCATE));
00092 
00093   if (fstat (fd, &st) < 0 || st.st_size != 800)
00094     error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
00095           STRINGIFY (FTRUNCATE));
00096 
00097   /* The following test covers more than POSIX.  POSIX does not require
00098      that ftruncate() can increase the file size.  But we are testing
00099      Unix systems.  */
00100   if (FTRUNCATE (fd, 1200) < 0)
00101     error (EXIT_FAILURE, errno, "size increase with %s failed",
00102           STRINGIFY (FTRUNCATE));
00103 
00104   if (fstat (fd, &st) < 0 || st.st_size != 1200)
00105     error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
00106           STRINGIFY (FTRUNCATE));
00107 
00108 
00109   if (TRUNCATE (name, 800) < 0)
00110     error (EXIT_FAILURE, errno, "size reduction with %s failed",
00111           STRINGIFY (TRUNCATE));
00112 
00113   if (fstat (fd, &st) < 0 || st.st_size != 800)
00114     error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
00115           STRINGIFY (TRUNCATE));
00116 
00117   /* The following test covers more than POSIX.  POSIX does not require
00118      that truncate() can increase the file size.  But we are testing
00119      Unix systems.  */
00120   if (TRUNCATE (name, 1200) < 0)
00121     error (EXIT_FAILURE, errno, "size increase with %s failed",
00122           STRINGIFY (TRUNCATE));
00123 
00124   if (fstat (fd, &st) < 0 || st.st_size != 1200)
00125     error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
00126           STRINGIFY (TRUNCATE));
00127 
00128 
00129   close (fd);
00130   unlink (name);
00131 
00132   return 0;
00133 }