IA64: Move NPTL public headers to sysdeps/ia64/nptl/.
[platform/upstream/glibc.git] / posix / tst-truncate.c
1 /* Tests for ftruncate and truncate.
2    Copyright (C) 2000-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
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.
10
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.
15
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/>.  */
19
20 #include <errno.h>
21 #include <error.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25
26
27 /* Allow testing of the 64-bit versions as well.  */
28 #ifndef TRUNCATE
29 # define TRUNCATE truncate
30 # define FTRUNCATE ftruncate
31 #endif
32
33 #define STRINGIFY(s) STRINGIFY2 (s)
34 #define STRINGIFY2(s) #s
35
36 /* Prototype for our test function.  */
37 extern void do_prepare (int argc, char *argv[]);
38 extern int do_test (int argc, char *argv[]);
39
40 /* We have a preparation function.  */
41 #define PREPARE do_prepare
42
43 /* We might need a bit longer timeout.  */
44 #define TIMEOUT 20 /* sec */
45
46 /* This defines the `main' function and some more.  */
47 #include <test-skeleton.c>
48
49 /* These are for the temporary file we generate.  */
50 char *name;
51 int fd;
52
53 void
54 do_prepare (int argc, char *argv[])
55 {
56    size_t name_len;
57
58 #define FNAME FNAME2(TRUNCATE)
59 #define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"
60
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);
65
66    /* Open our test file.   */
67    fd = mkstemp (name);
68    if (fd == -1)
69      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
70 }
71
72
73 int
74 do_test (int argc, char *argv[])
75 {
76   struct stat st;
77   char buf[1000];
78
79   memset (buf, '\0', sizeof (buf));
80
81   if (write (fd, buf, sizeof (buf)) != sizeof (buf))
82     error (EXIT_FAILURE, errno, "during write");
83
84   if (fstat (fd, &st) < 0 || st.st_size != sizeof (buf))
85     error (EXIT_FAILURE, 0, "initial size wrong");
86
87
88   if (FTRUNCATE (fd, 800) < 0)
89     error (EXIT_FAILURE, errno, "size reduction with %s failed",
90            STRINGIFY (FTRUNCATE));
91
92   if (fstat (fd, &st) < 0 || st.st_size != 800)
93     error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
94            STRINGIFY (FTRUNCATE));
95
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));
102
103   if (fstat (fd, &st) < 0 || st.st_size != 1200)
104     error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
105            STRINGIFY (FTRUNCATE));
106
107
108   if (TRUNCATE (name, 800) < 0)
109     error (EXIT_FAILURE, errno, "size reduction with %s failed",
110            STRINGIFY (TRUNCATE));
111
112   if (fstat (fd, &st) < 0 || st.st_size != 800)
113     error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
114            STRINGIFY (TRUNCATE));
115
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));
122
123   if (fstat (fd, &st) < 0 || st.st_size != 1200)
124     error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
125            STRINGIFY (TRUNCATE));
126
127
128   close (fd);
129   unlink (name);
130
131   return 0;
132 }