Replace an alloca use with a variable-length array.
[glibc.git] / posix / tst-truncate.c
blob51abd13a44da60cd590117bce1ba3728a984ece0
1 /* Tests for ftruncate and truncate.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
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, see
18 <http://www.gnu.org/licenses/>. */
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 size_t 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;