.
[coreutils.git] / lib / fileblocks.c
blob6a421cf51a2d4c6cd932b8a3d7f7e06caf8b6262
1 /* Convert file size to number of blocks on System V-like machines.
2 Copyright (C) 1990, 1997 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Brian L. Matthews, blm@6sceng.UUCP. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #if !HAVE_ST_BLOCKS && !defined _POSIX_SOURCE && defined BSIZE
25 # include <sys/types.h>
26 # include <sys/param.h>
28 # if HAVE_UNISTD_H
29 # include <unistd.h>
30 # endif
32 # ifndef NINDIR
33 /* Some SysV's, like Irix, seem to lack this. Hope it's correct. */
34 /* Number of inode pointers per indirect block. */
35 # define NINDIR (BSIZE/sizeof(daddr_t))
36 # endif /* !NINDIR */
38 /* Number of direct block addresses in an inode. */
39 # define NDIR 10
41 /* Return the number of 512-byte blocks in a file of SIZE bytes. */
43 off_t
44 st_blocks (size)
45 off_t size;
47 off_t datablks = size / 512 + (size % 512 != 0);
48 off_t indrblks = 0;
50 if (datablks > NDIR)
52 indrblks = (datablks - NDIR - 1) / NINDIR + 1;
54 if (datablks > NDIR + NINDIR)
56 indrblks += (datablks - NDIR - NINDIR - 1) / (NINDIR * NINDIR) + 1;
58 if (datablks > NDIR + NINDIR + NINDIR * NINDIR)
59 indrblks++;
63 return datablks + indrblks;
65 #else
66 /* This declaration is solely to ensure that after preprocessing
67 this file is never empty. */
68 extern int textutils_fileblocks_unused;
69 #endif