xref: /onnv-gate/usr/src/lib/libast/common/port/iblocks.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * aux function for <ls.h> iblocks() macro
254887Schin  *
264887Schin  * return number of blocks, including indirect block count
274887Schin  * given stat info
284887Schin  *
294887Schin  * mail gsf@research.att.com when you figure out the stat.st_blocks units
304887Schin  * until then we assume LS_BLOCKSIZE (512)
314887Schin  */
324887Schin 
334887Schin #include <ast.h>
344887Schin #if _AIX /* XXX */
354887Schin #undef	major
364887Schin #undef	minor
374887Schin #undef	makedev
384887Schin #endif
394887Schin #include <ast_param.h>
404887Schin #include <ls.h>
414887Schin 
424887Schin #if !_mem_st_blocks_stat
434887Schin 
444887Schin #ifndef B_DIRECT
454887Schin #define B_DIRECT	10
464887Schin #endif
474887Schin 
484887Schin #ifdef BITFS
494887Schin 
504887Schin #define B_SIZE		BSIZE(st->st_dev)
514887Schin #define B_INDIRECT	NINDIR(st->st_dev)
524887Schin 
534887Schin #else
544887Schin 
554887Schin #ifdef BSIZE
564887Schin #define B_SIZE		BSIZE
574887Schin #else
584887Schin #define B_SIZE		1024
594887Schin #endif
604887Schin 
614887Schin #ifdef NINDIR
624887Schin #define B_INDIRECT	NINDIR
634887Schin #else
644887Schin #define B_INDIRECT	128
654887Schin #endif
664887Schin 
674887Schin #endif
684887Schin 
694887Schin #endif
704887Schin 
714887Schin off_t
_iblocks(register struct stat * st)724887Schin _iblocks(register struct stat* st)
734887Schin {
744887Schin #if _mem_st_blocks_stat
754887Schin 
764887Schin 	return (st->st_blocks <= 0 || st->st_size <= 0) ? 0 : st->st_blocks;
774887Schin 
784887Schin #else
794887Schin 	unsigned long	b;
804887Schin 	unsigned long	t;
814887Schin 
824887Schin 	t = b = (st->st_size + B_SIZE - 1) / B_SIZE;
834887Schin 	if ((b -= B_DIRECT) > 0)
844887Schin 	{
854887Schin 		t += (b - 1) / B_INDIRECT + 1;
864887Schin 		if ((b -= B_INDIRECT) > 0)
874887Schin 		{
884887Schin 			t += (b - 1) / (B_INDIRECT * B_INDIRECT) + 1;
894887Schin 			if (b > B_INDIRECT * B_INDIRECT)
904887Schin 				t++;
914887Schin 		}
924887Schin 	}
934887Schin 	return t * B_SIZE / LS_BLOCKSIZE;
944887Schin #endif
954887Schin }
96