xref: /titanic_51/usr/src/lib/libast/common/port/iblocks.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * aux function for <ls.h> iblocks() macro
25  *
26  * return number of blocks, including indirect block count
27  * given stat info
28  *
29  * mail gsf@research.att.com when you figure out the stat.st_blocks units
30  * until then we assume LS_BLOCKSIZE (512)
31  */
32 
33 #include <ast.h>
34 #if _AIX /* XXX */
35 #undef	major
36 #undef	minor
37 #undef	makedev
38 #endif
39 #include <ast_param.h>
40 #include <ls.h>
41 
42 #if !_mem_st_blocks_stat
43 
44 #ifndef B_DIRECT
45 #define B_DIRECT	10
46 #endif
47 
48 #ifdef BITFS
49 
50 #define B_SIZE		BSIZE(st->st_dev)
51 #define B_INDIRECT	NINDIR(st->st_dev)
52 
53 #else
54 
55 #ifdef BSIZE
56 #define B_SIZE		BSIZE
57 #else
58 #define B_SIZE		1024
59 #endif
60 
61 #ifdef NINDIR
62 #define B_INDIRECT	NINDIR
63 #else
64 #define B_INDIRECT	128
65 #endif
66 
67 #endif
68 
69 #endif
70 
71 off_t
72 _iblocks(register struct stat* st)
73 {
74 #if _mem_st_blocks_stat
75 
76 	return (st->st_blocks <= 0 || st->st_size <= 0) ? 0 : st->st_blocks;
77 
78 #else
79 	unsigned long	b;
80 	unsigned long	t;
81 
82 	t = b = (st->st_size + B_SIZE - 1) / B_SIZE;
83 	if ((b -= B_DIRECT) > 0)
84 	{
85 		t += (b - 1) / B_INDIRECT + 1;
86 		if ((b -= B_INDIRECT) > 0)
87 		{
88 			t += (b - 1) / (B_INDIRECT * B_INDIRECT) + 1;
89 			if (b > B_INDIRECT * B_INDIRECT)
90 				t++;
91 		}
92 	}
93 	return t * B_SIZE / LS_BLOCKSIZE;
94 #endif
95 }
96