]>
Commit | Line | Data |
---|---|---|
cddf254d | 1 | /* Tests for ftruncate and truncate. |
d4697bc9 | 2 | Copyright (C) 2000-2014 Free Software Foundation, Inc. |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
cddf254d UD |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
cddf254d UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
cddf254d | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
cddf254d UD |
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 | { | |
30aa5785 | 56 | size_t name_len; |
cddf254d | 57 | |
0a550321 UD |
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); | |
cddf254d UD |
70 | } |
71 | ||
72 | ||
73 | int | |
74 | do_test (int argc, char *argv[]) | |
75 | { | |
76 | struct stat st; | |
77 | char buf[1000]; | |
cddf254d UD |
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) | |
0a550321 UD |
89 | error (EXIT_FAILURE, errno, "size reduction with %s failed", |
90 | STRINGIFY (FTRUNCATE)); | |
cddf254d UD |
91 | |
92 | if (fstat (fd, &st) < 0 || st.st_size != 800) | |
0a550321 UD |
93 | error (EXIT_FAILURE, 0, "size after reduction with %s incorrect", |
94 | STRINGIFY (FTRUNCATE)); | |
cddf254d UD |
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) | |
0a550321 UD |
100 | error (EXIT_FAILURE, errno, "size increase with %s failed", |
101 | STRINGIFY (FTRUNCATE)); | |
cddf254d UD |
102 | |
103 | if (fstat (fd, &st) < 0 || st.st_size != 1200) | |
0a550321 UD |
104 | error (EXIT_FAILURE, 0, "size after increase with %s incorrect", |
105 | STRINGIFY (FTRUNCATE)); | |
cddf254d UD |
106 | |
107 | ||
108 | if (TRUNCATE (name, 800) < 0) | |
0a550321 UD |
109 | error (EXIT_FAILURE, errno, "size reduction with %s failed", |
110 | STRINGIFY (TRUNCATE)); | |
cddf254d UD |
111 | |
112 | if (fstat (fd, &st) < 0 || st.st_size != 800) | |
0a550321 UD |
113 | error (EXIT_FAILURE, 0, "size after reduction with %s incorrect", |
114 | STRINGIFY (TRUNCATE)); | |
cddf254d UD |
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) | |
0a550321 UD |
120 | error (EXIT_FAILURE, errno, "size increase with %s failed", |
121 | STRINGIFY (TRUNCATE)); | |
cddf254d UD |
122 | |
123 | if (fstat (fd, &st) < 0 || st.st_size != 1200) | |
0a550321 UD |
124 | error (EXIT_FAILURE, 0, "size after increase with %s incorrect", |
125 | STRINGIFY (TRUNCATE)); | |
cddf254d UD |
126 | |
127 | ||
128 | close (fd); | |
129 | unlink (name); | |
130 | ||
131 | return 0; | |
132 | } |