2.9
[glibc/nacl-glibc.git] / posix / tst-truncate.c
blob78a5139477bf6bc6560b3e244926ea487c7d56ed
1 /* Tests for ftruncate and truncate.
2 Copyright (C) 2000 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <error.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
28 /* Allow testing of the 64-bit versions as well. */
29 #ifndef TRUNCATE
30 # define TRUNCATE truncate
31 # define FTRUNCATE ftruncate
32 #endif
34 #define STRINGIFY(s) STRINGIFY2 (s)
35 #define STRINGIFY2(s) #s
37 /* Prototype for our test function. */
38 extern void do_prepare (int argc, char *argv[]);
39 extern int do_test (int argc, char *argv[]);
41 /* We have a preparation function. */
42 #define PREPARE do_prepare
44 /* We might need a bit longer timeout. */
45 #define TIMEOUT 20 /* sec */
47 /* This defines the `main' function and some more. */
48 #include <test-skeleton.c>
50 /* These are for the temporary file we generate. */
51 char *name;
52 int fd;
54 void
55 do_prepare (int argc, char *argv[])
57 size_t name_len;
59 #define FNAME FNAME2(TRUNCATE)
60 #define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"
62 name_len = strlen (test_dir);
63 name = malloc (name_len + sizeof (FNAME));
64 mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
65 add_temp_file (name);
67 /* Open our test file. */
68 fd = mkstemp (name);
69 if (fd == -1)
70 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
74 int
75 do_test (int argc, char *argv[])
77 struct stat st;
78 char buf[1000];
80 memset (buf, '\0', sizeof (buf));
82 if (write (fd, buf, sizeof (buf)) != sizeof (buf))
83 error (EXIT_FAILURE, errno, "during write");
85 if (fstat (fd, &st) < 0 || st.st_size != sizeof (buf))
86 error (EXIT_FAILURE, 0, "initial size wrong");
89 if (FTRUNCATE (fd, 800) < 0)
90 error (EXIT_FAILURE, errno, "size reduction with %s failed",
91 STRINGIFY (FTRUNCATE));
93 if (fstat (fd, &st) < 0 || st.st_size != 800)
94 error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
95 STRINGIFY (FTRUNCATE));
97 /* The following test covers more than POSIX. POSIX does not require
98 that ftruncate() can increase the file size. But we are testing
99 Unix systems. */
100 if (FTRUNCATE (fd, 1200) < 0)
101 error (EXIT_FAILURE, errno, "size increase with %s failed",
102 STRINGIFY (FTRUNCATE));
104 if (fstat (fd, &st) < 0 || st.st_size != 1200)
105 error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
106 STRINGIFY (FTRUNCATE));
109 if (TRUNCATE (name, 800) < 0)
110 error (EXIT_FAILURE, errno, "size reduction with %s failed",
111 STRINGIFY (TRUNCATE));
113 if (fstat (fd, &st) < 0 || st.st_size != 800)
114 error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
115 STRINGIFY (TRUNCATE));
117 /* The following test covers more than POSIX. POSIX does not require
118 that truncate() can increase the file size. But we are testing
119 Unix systems. */
120 if (TRUNCATE (name, 1200) < 0)
121 error (EXIT_FAILURE, errno, "size increase with %s failed",
122 STRINGIFY (TRUNCATE));
124 if (fstat (fd, &st) < 0 || st.st_size != 1200)
125 error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
126 STRINGIFY (TRUNCATE));
129 close (fd);
130 unlink (name);
132 return 0;