Update.
[glibc.git] / posix / tst-truncate.c
blob663f0e4a9ff07aeb01174c1983529902b1847cc6
1 /* Tests for ftruncate and truncate.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
27 /* Allow testing of the 64-bit versions as well. */
28 #ifndef TRUNCATE
29 # define TRUNCATE truncate
30 # define FTRUNCATE ftruncate
31 #endif
33 #define STRINGIFY(s) STRINGIFY2 (s)
34 #define STRINGIFY2(s) #s
36 /* Prototype for our test function. */
37 extern void do_prepare (int argc, char *argv[]);
38 extern int do_test (int argc, char *argv[]);
40 /* We have a preparation function. */
41 #define PREPARE do_prepare
43 /* We might need a bit longer timeout. */
44 #define TIMEOUT 20 /* sec */
46 /* This defines the `main' function and some more. */
47 #include <test-skeleton.c>
49 /* These are for the temporary file we generate. */
50 char *name;
51 int fd;
53 void
54 do_prepare (int argc, char *argv[])
56 char name_len;
58 #define FNAME FNAME2(TRUNCATE)
59 #define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"
61 name_len = strlen (test_dir);
62 name = malloc (name_len + sizeof (FNAME));
63 mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
64 add_temp_file (name);
66 /* Open our test file. */
67 fd = mkstemp (name);
68 if (fd == -1)
69 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
73 int
74 do_test (int argc, char *argv[])
76 struct stat st;
77 char buf[1000];
79 memset (buf, '\0', sizeof (buf));
81 if (write (fd, buf, sizeof (buf)) != sizeof (buf))
82 error (EXIT_FAILURE, errno, "during write");
84 if (fstat (fd, &st) < 0 || st.st_size != sizeof (buf))
85 error (EXIT_FAILURE, 0, "initial size wrong");
88 if (FTRUNCATE (fd, 800) < 0)
89 error (EXIT_FAILURE, errno, "size reduction with %s failed",
90 STRINGIFY (FTRUNCATE));
92 if (fstat (fd, &st) < 0 || st.st_size != 800)
93 error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
94 STRINGIFY (FTRUNCATE));
96 /* The following test covers more than POSIX. POSIX does not require
97 that ftruncate() can increase the file size. But we are testing
98 Unix systems. */
99 if (FTRUNCATE (fd, 1200) < 0)
100 error (EXIT_FAILURE, errno, "size increase with %s failed",
101 STRINGIFY (FTRUNCATE));
103 if (fstat (fd, &st) < 0 || st.st_size != 1200)
104 error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
105 STRINGIFY (FTRUNCATE));
108 if (TRUNCATE (name, 800) < 0)
109 error (EXIT_FAILURE, errno, "size reduction with %s failed",
110 STRINGIFY (TRUNCATE));
112 if (fstat (fd, &st) < 0 || st.st_size != 800)
113 error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
114 STRINGIFY (TRUNCATE));
116 /* The following test covers more than POSIX. POSIX does not require
117 that truncate() can increase the file size. But we are testing
118 Unix systems. */
119 if (TRUNCATE (name, 1200) < 0)
120 error (EXIT_FAILURE, errno, "size increase with %s failed",
121 STRINGIFY (TRUNCATE));
123 if (fstat (fd, &st) < 0 || st.st_size != 1200)
124 error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
125 STRINGIFY (TRUNCATE));
128 close (fd);
129 unlink (name);
131 return 0;