1 /* fsusage.c -- return space usage of mounted filesystems
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
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)
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.
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. */
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22 (which it would do because it found this file in $srcdir). */
29 #include <sys/types.h>
34 #if defined (STAT_STATFS3_OSF1) /* DEC Alpha running OSF/1 */
35 # include <sys/mount.h>
38 #if defined(STAT_STATFS2_BSIZE) && !defined(_IBMR2) /* 4.3BSD, SunOS 4, HP-UX, AIX PS/2. */
42 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
43 #include <sys/mount.h>
46 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
47 #include <sys/param.h>
48 #include <sys/mount.h>
51 #ifdef STAT_READ /* SVR2. */
52 #include <sys/param.h>
53 #include <sys/filsys.h>
57 #if defined(STAT_STATFS4) || (defined(_AIX) && defined(_IBMR2)) /* SVR3, Dynix, Irix, AIX RS6000. */
58 #include <sys/statfs.h>
61 #if defined(_AIX) && defined(_I386) /* AIX PS/2. */
63 #include <sys/dustat.h>
66 #ifdef STAT_STATVFS /* SVR4. */
67 #include <sys/statvfs.h>
71 /* Return the number of TOSIZE-byte blocks used by
72 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
73 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
76 adjust_blocks (blocks, fromsize, tosize)
85 if (fromsize == tosize) /* E.g., from 512 to 512. */
87 else if (fromsize > tosize) /* E.g., from 2048 to 512. */
88 return blocks * (fromsize / tosize);
89 else /* E.g., from 256 to 512. */
90 return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
93 /* Fill in the fields of FSP with information about space usage for
94 the filesystem on which PATH resides.
95 DISK is the device on which PATH is mounted, for space-getting
96 methods that need to know it.
97 Return 0 if successful, -1 if not. */
100 get_fs_usage (path, disk, fsp)
102 struct fs_usage *fsp;
104 #if defined (STAT_STATFS3_OSF1)
107 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
109 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
110 #endif /* STAT_STATFS3_OSF1 */
112 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
115 if (statfs (path, &fsd) != 1)
117 #define CONVERT_BLOCKS(b) adjust_blocks ((b), 1024, 512)
118 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
119 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
120 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
121 fsp->fsu_files = fsd.fd_req.gtot;
122 fsp->fsu_ffree = fsd.fd_req.gfree;
125 #ifdef STAT_READ /* SVR2. */
127 #define SUPERBOFF (SUPERB * 512)
132 fd = open (disk, O_RDONLY);
135 lseek (fd, (long) SUPERBOFF, 0);
136 if (read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
142 #define CONVERT_BLOCKS(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
143 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.s_fsize);
144 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.s_tfree);
145 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.s_tfree);
146 fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1);
147 fsp->fsu_ffree = fsd.s_tinode;
150 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
153 if (statfs (path, &fsd) < 0)
155 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
158 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
161 if (statfs (path, &fsd) < 0)
163 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
166 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix. */
169 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
171 /* Empirically, the block counts on most SVR3 and SVR3-derived
172 systems seem to always be in terms of 512-byte blocks,
173 no matter what value f_bsize has. */
174 #define CONVERT_BLOCKS(b) (b)
175 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
176 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
177 #define f_bavail f_bfree
182 #ifdef STAT_STATVFS /* SVR4. */
185 if (statvfs (path, &fsd) < 0)
187 /* f_frsize isn't guaranteed to be supported. */
188 #define CONVERT_BLOCKS(b) \
189 adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
192 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ) /* !Ultrix && !SVR2. */
193 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
194 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
195 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
196 fsp->fsu_files = fsd.f_files;
197 fsp->fsu_ffree = fsd.f_ffree;
203 #if defined(_AIX) && defined(_I386)
204 /* AIX PS/2 does not supply statfs. */
214 if (stat (path, &stats))
216 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
219 fsb->f_bsize = fsd.du_bsize;
220 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
221 fsb->f_bfree = fsd.du_tfree;
222 fsb->f_bavail = fsd.du_tfree;
223 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
224 fsb->f_ffree = fsd.du_tinode;
225 fsb->f_fsid.val[0] = fsd.du_site;
226 fsb->f_fsid.val[1] = fsd.du_pckno;
229 #endif /* _AIX && _I386 */