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. */
30 #define TESTFILE "test-fseeko.data"
31 #define TESTFILE_ZEROES 3000000000LL
32 #define TESTFILE_DATA "GNU"
33 #define TESTFILE_DATA_LEN 3
40 if (sizeof (off_t
) <= 4)
42 fprintf (stderr
, "off_t is only 32 bits.\n");
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);
53 if (ftruncate (fd
, TESTFILE_ZEROES
) < 0
54 || lseek (fd
, TESTFILE_ZEROES
, SEEK_SET
) < 0
55 || write (fd
, TESTFILE_DATA
, TESTFILE_DATA_LEN
) < 0
60 fprintf (stderr
, "Could not create 3 GB large file.\n");
66 /* Check that fseeko() works. */
68 FILE *fp
= fopen (TESTFILE
, "r");
72 ret
= fseeko (fp
, TESTFILE_ZEROES
, SEEK_SET
);
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);
85 return test_exit_status
;