Aquire serializer before calling ioctl
[dragonfly.git] / contrib / tar / lib / fileblocks.c
1 /* Convert file size to number of blocks on System V-like machines.
2    Copyright (C) 1990, 1997, 1998, 1999 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Brian L. Matthews, blm@6sceng.UUCP. */
19 \f
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
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 # if HAVE_UNISTD_H
33 #  include <unistd.h>
34 # endif
35
36 # ifndef NINDIR
37
38 #  if defined (__DJGPP__)
39 typedef long daddr_t; /* for disk address */
40 #  endif
41
42 /* Some SysV's, like Irix, seem to lack this.  Hope it's correct. */
43 /* Number of inode pointers per indirect block. */
44 #  define NINDIR (BSIZE / sizeof (daddr_t))
45 # endif /* !NINDIR */
46
47 /* Number of direct block addresses in an inode. */
48 # define NDIR   10
49
50 /* Return the number of 512-byte blocks in a file of SIZE bytes. */
51
52 off_t
53 st_blocks (off_t size)
54 {
55   off_t datablks = size / 512 + (size % 512 != 0);
56   off_t indrblks = 0;
57
58   if (datablks > NDIR)
59     {
60       indrblks = (datablks - NDIR - 1) / NINDIR + 1;
61
62       if (datablks > NDIR + NINDIR)
63         {
64           indrblks += (datablks - NDIR - NINDIR - 1) / (NINDIR * NINDIR) + 1;
65
66           if (datablks > NDIR + NINDIR + NINDIR * NINDIR)
67             indrblks++;
68         }
69     }
70
71   return datablks + indrblks;
72 }
73 #else
74 /* This declaration is solely to ensure that after preprocessing
75    this file is never empty.  */
76 extern int textutils_fileblocks_unused;
77 #endif