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 /* Set B_NEW to the number of TOSIZE-byte blocks used by B FROMSIZE-byte
72 blocks, rounding away from zero. TOSIZE must be positive. Return -1
73 from invoking function if FROMSIZE is not positive. */
75 #define ADJUST_BLOCKS(b_new, b, fromsize, tosize) \
80 if ((fromsize) <= 0) \
83 if ((fromsize) == (tosize)) /* E.g., from 512 to 512. */ \
85 else if ((fromsize) > (tosize)) /* E.g., from 2048 to 512. */ \
86 (b_new) = (b) * ((fromsize) / (tosize)); \
87 else /* E.g., from 256 to 512. */ \
88 (b_new) = ((b) + ((b) < 0 ? -1 : 1)) / ((tosize) / (fromsize)); \
92 /* Fill in the fields of FSP with information about space usage for
93 the filesystem on which PATH resides.
94 DISK is the device on which PATH is mounted, for space-getting
95 methods that need to know it.
96 Return 0 if successful, -1 if not. */
99 get_fs_usage (path, disk, fsp)
101 struct fs_usage *fsp;
103 #if defined (STAT_STATFS3_OSF1)
106 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
108 #define CONVERT_BLOCKS(bnew, b) ADJUST_BLOCKS ((bnew), (b), fsd.f_fsize, 512)
109 #endif /* STAT_STATFS3_OSF1 */
111 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
114 if (statfs (path, &fsd) != 1)
116 #define CONVERT_BLOCKS(bnew, b) ADJUST_BLOCKS ((bnew), (b), 1024, 512)
117 CONVERT_BLOCKS (fsp->fsu_blocks, fsd.fd_req.btot);
118 CONVERT_BLOCKS (fsp->fsu_bfree, fsd.fd_req.bfree);
119 CONVERT_BLOCKS (fsp->fsu_bavail, fsd.fd_req.bfreen);
120 fsp->fsu_files = fsd.fd_req.gtot;
121 fsp->fsu_ffree = fsd.fd_req.gfree;
124 #ifdef STAT_READ /* SVR2. */
126 #define SUPERBOFF (SUPERB * 512)
131 fd = open (disk, O_RDONLY);
134 lseek (fd, (long) SUPERBOFF, 0);
135 if (read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
141 #define CONVERT_BLOCKS(bnew, b) \
142 ADJUST_BLOCKS ((bnew), (b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
143 CONVERT_BLOCKS (fsp->fsu_blocks, fsd.s_fsize);
144 CONVERT_BLOCKS (fsp->fsu_bfree, fsd.s_tfree);
145 CONVERT_BLOCKS (fsp->fsu_bavail, 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(bnew, b) ADJUST_BLOCKS ((bnew), (b), fsd.f_bsize, 512)
158 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
161 if (statfs (path, &fsd) < 0)
163 #define CONVERT_BLOCKS(bnew, b) ADJUST_BLOCKS ((bnew), (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(bnew, b) \
181 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
182 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
183 #define f_bavail f_bfree
188 #ifdef STAT_STATVFS /* SVR4. */
191 if (statvfs (path, &fsd) < 0)
193 /* f_frsize isn't guaranteed to be supported. */
194 #define CONVERT_BLOCKS(bnew, b) \
195 ADJUST_BLOCKS ((bnew), (b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
198 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ) /* !Ultrix && !SVR2. */
199 CONVERT_BLOCKS (fsp->fsu_blocks, fsd.f_blocks);
200 CONVERT_BLOCKS (fsp->fsu_bfree, fsd.f_bfree);
201 CONVERT_BLOCKS (fsp->fsu_bavail, fsd.f_bavail);
202 fsp->fsu_files = fsd.f_files;
203 fsp->fsu_ffree = fsd.f_ffree;
209 #if defined(_AIX) && defined(_I386)
210 /* AIX PS/2 does not supply statfs. */
220 if (stat (path, &stats))
222 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
225 fsb->f_bsize = fsd.du_bsize;
226 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
227 fsb->f_bfree = fsd.du_tfree;
228 fsb->f_bavail = fsd.du_tfree;
229 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
230 fsb->f_ffree = fsd.du_tinode;
231 fsb->f_fsid.val[0] = fsd.du_site;
232 fsb->f_fsid.val[1] = fsd.du_pckno;
235 #endif /* _AIX && _I386 */