openat: don’t close (-1)
[gnulib.git] / tests / test-fseeko-largefile.c
blob7cb7edc264beb4874f19c390fce73e3915435fdc
1 /* Test of fseeko() function on large files.
2 Copyright (C) 2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
19 #include <config.h>
21 /* Specification. */
22 #include <stdio.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "macros.h"
30 #define TESTFILE "test-fseeko.data"
31 #define TESTFILE_ZEROES 3000000000LL
32 #define TESTFILE_DATA "GNU"
33 #define TESTFILE_DATA_LEN 3
35 int
36 main (void)
38 remove (TESTFILE);
40 if (sizeof (off_t) <= 4)
42 fprintf (stderr, "off_t is only 32 bits.\n");
43 return 77;
45 /* off_t is larger than 32-bit. */
47 /* Create a file that is larger than 2 GiB.
48 On file systems such as ext2, the file will have "holes" and thus
49 not consume much disk space. */
51 int fd = open (TESTFILE, O_CREAT | O_TRUNC | O_RDWR, 0600);
52 ASSERT (fd >= 0);
53 if (ftruncate (fd, TESTFILE_ZEROES) < 0
54 || lseek (fd, TESTFILE_ZEROES, SEEK_SET) < 0
55 || write (fd, TESTFILE_DATA, TESTFILE_DATA_LEN) < 0
56 || fsync (fd) < 0)
58 close (fd);
59 remove (TESTFILE);
60 fprintf (stderr, "Could not create 3 GB large file.\n");
61 return 77;
63 close (fd);
66 /* Check that fseeko() works. */
68 FILE *fp = fopen (TESTFILE, "r");
69 ASSERT (fp != NULL);
71 int ret;
72 ret = fseeko (fp, TESTFILE_ZEROES, SEEK_SET);
73 ASSERT (ret == 0);
75 char buf[TESTFILE_DATA_LEN];
76 ASSERT (fread (buf, 1, TESTFILE_DATA_LEN, fp) == TESTFILE_DATA_LEN);
77 ASSERT (memcmp (buf, TESTFILE_DATA, TESTFILE_DATA_LEN) == 0);
79 ret = fclose (fp);
80 ASSERT (ret == 0);
83 remove (TESTFILE);
85 return test_exit_status;