Tizen 2.0 Release
[external/tizen-coreutils.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, 2005, 2006 Free Software
4    Foundation, Inc.
5
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 2, or (at your option)
9    any later version.
10
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.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by Brian L. Matthews, blm@6sceng.UUCP. */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25
26 #if HAVE_SYS_PARAM_H
27 # include <sys/param.h>
28 #endif
29
30 #if !HAVE_STRUCT_STAT_ST_BLOCKS && !defined _POSIX_SOURCE && defined BSIZE
31
32 # include <unistd.h>
33
34 # ifndef NINDIR
35
36 #  if defined __DJGPP__
37 typedef long daddr_t; /* for disk address */
38 #  endif
39
40 /* Some SysV's, like Irix, seem to lack this.  Hope it's correct. */
41 /* Number of inode pointers per indirect block. */
42 #  define NINDIR (BSIZE / sizeof (daddr_t))
43 # endif /* !NINDIR */
44
45 /* Number of direct block addresses in an inode. */
46 # define NDIR   10
47
48 /* Return the number of 512-byte blocks in a file of SIZE bytes. */
49
50 off_t
51 st_blocks (off_t size)
52 {
53   off_t datablks = size / 512 + (size % 512 != 0);
54   off_t indrblks = 0;
55
56   if (datablks > NDIR)
57     {
58       indrblks = (datablks - NDIR - 1) / NINDIR + 1;
59
60       if (datablks > NDIR + NINDIR)
61         {
62           indrblks += (datablks - NDIR - NINDIR - 1) / (NINDIR * NINDIR) + 1;
63
64           if (datablks > NDIR + NINDIR + NINDIR * NINDIR)
65             indrblks++;
66         }
67     }
68
69   return datablks + indrblks;
70 }
71 #else
72 /* This declaration is solely to ensure that after preprocessing
73    this file is never empty.  */
74 typedef int textutils_fileblocks_unused;
75 #endif