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>
31 #include "safe-stat.h"
35 #if defined (STAT_STATFS3_OSF1) /* DEC Alpha running OSF/1 */
36 # include <sys/mount.h>
39 #if defined(STAT_STATFS2_BSIZE) && !defined(_IBMR2) /* 4.3BSD, SunOS 4, HP-UX, AIX PS/2. */
43 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
44 #include <sys/mount.h>
47 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
48 #include <sys/param.h>
49 #include <sys/mount.h>
52 #ifdef STAT_READ /* SVR2. */
53 #include <sys/param.h>
54 #include <sys/filsys.h>
58 #if defined(STAT_STATFS4) || (defined(_AIX) && defined(_IBMR2)) /* SVR3, Dynix, Irix, AIX RS6000. */
59 #include <sys/statfs.h>
62 #if defined(_AIX) && defined(_I386) /* AIX PS/2. */
64 #include <sys/dustat.h>
67 #ifdef STAT_STATVFS /* SVR4. */
68 #include <sys/statvfs.h>
72 /* Return the number of TOSIZE-byte blocks used by
73 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
74 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
77 adjust_blocks (blocks, fromsize, tosize)
86 if (fromsize == tosize) /* E.g., from 512 to 512. */
88 else if (fromsize > tosize) /* E.g., from 2048 to 512. */
89 return blocks * (fromsize / tosize);
90 else /* E.g., from 256 to 512. */
91 return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
94 /* Fill in the fields of FSP with information about space usage for
95 the filesystem on which PATH resides.
96 DISK is the device on which PATH is mounted, for space-getting
97 methods that need to know it.
98 Return 0 if successful, -1 if not. */
101 get_fs_usage (path, disk, fsp)
103 struct fs_usage *fsp;
105 #if defined (STAT_STATFS3_OSF1)
108 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
110 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
111 #endif /* STAT_STATFS3_OSF1 */
113 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
116 if (statfs (path, &fsd) != 1)
118 #define CONVERT_BLOCKS(b) adjust_blocks ((b), 1024, 512)
119 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
120 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
121 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
122 fsp->fsu_files = fsd.fd_req.gtot;
123 fsp->fsu_ffree = fsd.fd_req.gfree;
126 #ifdef STAT_READ /* SVR2. */
128 #define SUPERBOFF (SUPERB * 512)
133 fd = open (disk, O_RDONLY);
136 lseek (fd, (long) SUPERBOFF, 0);
137 if (read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
143 #define CONVERT_BLOCKS(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
144 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.s_fsize);
145 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.s_tfree);
146 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.s_tfree);
147 fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1);
148 fsp->fsu_ffree = fsd.s_tinode;
151 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
154 if (statfs (path, &fsd) < 0)
156 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
159 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
162 if (statfs (path, &fsd) < 0)
164 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
167 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix. */
170 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
172 /* Empirically, the block counts on most SVR3 and SVR3-derived
173 systems seem to always be in terms of 512-byte blocks,
174 no matter what value f_bsize has. */
175 #define CONVERT_BLOCKS(b) (b)
176 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
177 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
178 #define f_bavail f_bfree
183 #ifdef STAT_STATVFS /* SVR4. */
186 if (statvfs (path, &fsd) < 0)
188 /* f_frsize isn't guaranteed to be supported. */
189 #define CONVERT_BLOCKS(b) \
190 adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
193 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ) /* !Ultrix && !SVR2. */
194 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
195 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
196 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
197 fsp->fsu_files = fsd.f_files;
198 fsp->fsu_ffree = fsd.f_ffree;
204 #if defined(_AIX) && defined(_I386)
205 /* AIX PS/2 does not supply statfs. */
215 if (SAFE_STAT (path, &stats))
217 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
220 fsb->f_bsize = fsd.du_bsize;
221 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
222 fsb->f_bfree = fsd.du_tfree;
223 fsb->f_bavail = fsd.du_tfree;
224 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
225 fsb->f_ffree = fsd.du_tinode;
226 fsb->f_fsid.val[0] = fsd.du_site;
227 fsb->f_fsid.val[1] = fsd.du_pckno;
230 #endif /* _AIX && _I386 */