exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / fileblocks.c
blob1be34d9085becf8184c397e7edacd81063533a17
1 /* Convert file size to number of blocks on System V-like machines.
3 Copyright (C) 1990, 1997-1999, 2004-2006, 2009-2024 Free Software
4 Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* Written by Brian L. Matthews, blm@6sceng.UUCP. */
21 #include <config.h>
23 #include <sys/types.h>
25 #if HAVE_SYS_PARAM_H
26 # include <sys/param.h>
27 #endif
29 #if !HAVE_STRUCT_STAT_ST_BLOCKS && !defined _POSIX_SOURCE && defined BSIZE
31 # include <unistd.h>
33 # ifndef NINDIR
35 # if defined __DJGPP__
36 typedef long daddr_t; /* for disk address */
37 # endif
39 /* Some SysV's, like Irix, seem to lack this. Hope it's correct. */
40 /* Number of inode pointers per indirect block. */
41 # define NINDIR (BSIZE / sizeof (daddr_t))
42 # endif /* !NINDIR */
44 /* Number of direct block addresses in an inode. */
45 # define NDIR 10
47 /* Return the number of 512-byte blocks in a file of SIZE bytes. */
49 off_t
50 st_blocks (off_t size)
52 off_t datablks = size / 512 + (size % 512 != 0);
53 off_t indrblks = 0;
55 if (datablks > NDIR)
57 indrblks = (datablks - NDIR - 1) / NINDIR + 1;
59 if (datablks > NDIR + NINDIR)
61 indrblks += (datablks - NDIR - NINDIR - 1) / (NINDIR * NINDIR) + 1;
63 if (datablks > NDIR + NINDIR + NINDIR * NINDIR)
64 indrblks++;
68 return datablks + indrblks;
70 #else
71 /* This declaration is solely to ensure that after preprocessing
72 this file is never empty. */
73 typedef int textutils_fileblocks_unused;
74 #endif