manual: Add Descriptor-Relative Access section
[glibc.git] / posix / tst-truncate-common.c
blobb8c561ffdb2b2903f8b1025eaddee0adc3bbb0d8
1 /* Common f{truncate} tests definitions.
2 Copyright (C) 2016-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
24 #include <support/check.h>
26 static void do_prepare (void);
27 #define PREPARE(argc, argv) do_prepare ()
28 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
31 #include <test-skeleton.c>
33 static char *temp_filename;
34 static int temp_fd;
36 static void
37 do_prepare (void)
39 temp_fd = create_temp_file ("tst-trucate.", &temp_filename);
40 if (temp_fd == -1)
42 printf ("cannot create temporary file: %m\n");
43 exit (1);
47 static int
48 do_test_with_offset (off_t offset)
50 struct stat st;
51 char buf[1000];
53 memset (buf, 0xcf, sizeof (buf));
55 if (pwrite (temp_fd, buf, sizeof (buf), offset) != sizeof (buf))
56 FAIL_RET ("write failed");
57 if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + sizeof (buf)))
58 FAIL_RET ("initial size wrong");
60 if (ftruncate (temp_fd, offset + 800) < 0)
61 FAIL_RET ("size reduction with ftruncate failed");
62 if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800))
63 FAIL_RET ("size after reduction with ftruncate is incorrect");
65 /* The following test covers more than POSIX. POSIX does not require
66 that ftruncate() can increase the file size. But we are testing
67 Unix systems. */
68 if (ftruncate (temp_fd, offset + 1200) < 0)
69 FAIL_RET ("size increate with ftruncate failed");
70 if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200))
71 FAIL_RET ("size after increase is incorrect");
73 if (truncate (temp_filename, offset + 800) < 0)
74 FAIL_RET ("size reduction with truncate failed");
75 if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800))
76 FAIL_RET ("size after reduction with truncate incorrect");
78 /* The following test covers more than POSIX. POSIX does not require
79 that truncate() can increase the file size. But we are testing
80 Unix systems. */
81 if (truncate (temp_filename, (offset + 1200)) < 0)
82 FAIL_RET ("size increase with truncate failed");
83 if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200))
84 FAIL_RET ("size increase with truncate is incorrect");
86 return 0;