Use a consistent style for including <config.h>.
[gnulib.git] / lib / fileblocks.c
1 /* Convert file size to number of blocks on System V-like machines.
2
3    Copyright (C) 1990, 1997, 1998, 1999, 2004 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program 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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Brian L. Matthews, blm@6sceng.UUCP. */
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <sys/types.h>
26
27 #if HAVE_SYS_PARAM_H
28 # include <sys/param.h>
29 #endif
30
31 #if !HAVE_STRUCT_STAT_ST_BLOCKS && !defined _POSIX_SOURCE && defined BSIZE
32
33 # if HAVE_UNISTD_H
34 #  include <unistd.h>
35 # endif
36
37 # ifndef NINDIR
38
39 #  if defined (__DJGPP__)
40 typedef long daddr_t; /* for disk address */
41 #  endif
42
43 /* Some SysV's, like Irix, seem to lack this.  Hope it's correct. */
44 /* Number of inode pointers per indirect block. */
45 #  define NINDIR (BSIZE / sizeof (daddr_t))
46 # endif /* !NINDIR */
47
48 /* Number of direct block addresses in an inode. */
49 # define NDIR   10
50
51 /* Return the number of 512-byte blocks in a file of SIZE bytes. */
52
53 off_t
54 st_blocks (off_t size)
55 {
56   off_t datablks = size / 512 + (size % 512 != 0);
57   off_t indrblks = 0;
58
59   if (datablks > NDIR)
60     {
61       indrblks = (datablks - NDIR - 1) / NINDIR + 1;
62
63       if (datablks > NDIR + NINDIR)
64         {
65           indrblks += (datablks - NDIR - NINDIR - 1) / (NINDIR * NINDIR) + 1;
66
67           if (datablks > NDIR + NINDIR + NINDIR * NINDIR)
68             indrblks++;
69         }
70     }
71
72   return datablks + indrblks;
73 }
74 #else
75 /* This declaration is solely to ensure that after preprocessing
76    this file is never empty.  */
77 typedef int textutils_fileblocks_unused;
78 #endif