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
28 /* Allow testing of the 64-bit versions as well. */
30 # define TRUNCATE truncate
31 # define FTRUNCATE ftruncate
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. */
55 do_prepare (int argc
, char *argv
[])
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
));
67 /* Open our test file. */
70 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
75 do_test (int argc
, char *argv
[])
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
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
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
));