5
|
1 /* Convert file size to number of blocks on System V-like machines. |
|
2 Copyright (C) 1990 Free Software Foundation, Inc. |
|
3 |
|
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. |
|
8 |
|
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. |
|
13 |
|
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 |
|
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
17 |
|
18 /* Written by Brian L. Matthews, blm@6sceng.UUCP. */ |
|
19 |
|
20 #if !defined (HAVE_ST_BLOCKS) && !defined(_POSIX_SOURCE) |
|
21 #include <sys/types.h> |
|
22 #include <sys/param.h> |
|
23 |
|
24 #ifndef NINDIR |
|
25 /* Some SysV's, like Irix, seem to lack these. Hope they're correct. */ |
|
26 /* Size of a indirect block, in bytes. */ |
|
27 #define BSIZE 1024 |
|
28 |
|
29 /* Number of inode pointers per indirect block. */ |
|
30 #define NINDIR (BSIZE/sizeof(daddr_t)) |
|
31 #endif /* !NINDIR */ |
|
32 |
|
33 /* Number of direct block addresses in an inode. */ |
|
34 #define NDIR 10 |
|
35 |
|
36 /* Return the number of 512-byte blocks in a file of SIZE bytes. */ |
|
37 |
|
38 long |
|
39 st_blocks (size) |
|
40 long size; |
|
41 { |
|
42 long datablks = (size + 512 - 1) / 512; |
|
43 long indrblks = 0; |
|
44 |
|
45 if (datablks > NDIR) |
|
46 { |
|
47 indrblks = (datablks - NDIR - 1) / NINDIR + 1; |
|
48 |
|
49 if (datablks > NDIR + NINDIR) |
|
50 { |
|
51 indrblks += (datablks - NDIR - NINDIR - 1) / (NINDIR * NINDIR) + 1; |
|
52 |
|
53 if (datablks > NDIR + NINDIR + NINDIR * NINDIR) |
|
54 indrblks++; |
|
55 } |
|
56 } |
|
57 |
|
58 return datablks + indrblks; |
|
59 } |
|
60 #endif |