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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <sys/types.h>
24 #ifdef HAVE_SYS_PARAM_H
25 #include <sys/param.h>
28 #ifdef HAVE_SYS_MOUNT_H
29 #include <sys/mount.h>
36 #ifdef HAVE_SYS_FILSYS_H
37 #include <sys/filsys.h> /* SVR2. */
46 #ifdef HAVE_SYS_STATFS_H
47 #include <sys/statfs.h>
50 #ifdef HAVE_DUSTAT_H /* AIX PS/2. */
51 #include <sys/dustat.h>
54 #ifdef HAVE_SYS_STATVFS_H /* SVR4. */
55 #include <sys/statvfs.h>
64 /* Return the number of TOSIZE-byte blocks used by
65 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
66 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
69 fs_adjust_blocks (long blocks
, int fromsize
, int tosize
)
76 if (fromsize
== tosize
) /* E.g., from 512 to 512. */
78 else if (fromsize
> tosize
) /* E.g., from 2048 to 512. */
79 return blocks
* (fromsize
/ tosize
);
80 else /* E.g., from 256 to 512. */
81 return (blocks
+ (blocks
< 0 ? -1 : 1)) / (tosize
/ fromsize
);
84 #if defined(_AIX) && defined(_I386)
85 /* AIX PS/2 does not supply statfs. */
86 static int aix_statfs (char *path
, struct statfs
*fsb
)
91 if (stat (path
, &stats
))
93 if (dustat (stats
.st_dev
, 0, &fsd
, sizeof (fsd
)))
96 fsb
->f_bsize
= fsd
.du_bsize
;
97 fsb
->f_blocks
= fsd
.du_fsize
- fsd
.du_isize
;
98 fsb
->f_bfree
= fsd
.du_tfree
;
99 fsb
->f_bavail
= fsd
.du_tfree
;
100 fsb
->f_files
= (fsd
.du_isize
- 2) * fsd
.du_inopb
;
101 fsb
->f_ffree
= fsd
.du_tinode
;
102 fsb
->f_fsid
.val
[0] = fsd
.du_site
;
103 fsb
->f_fsid
.val
[1] = fsd
.du_pckno
;
106 #define statfs(path,fsb) aix_statfs(path,fsb)
107 #endif /* _AIX && _I386 */
109 /* Fill in the fields of FSP with information about space usage for
110 the filesystem on which PATH resides.
111 Return 0 if successful, -1 if not. */
113 int get_fs_usage (char *path
, struct fs_usage
*fsp
)
115 #ifdef STAT_STATFS3_OSF1
118 if (statfs (path
, &fsd
, sizeof (struct statfs
)) != 0)
120 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
121 #endif /* STAT_STATFS3_OSF1 */
123 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
126 if (statfs (path
, &fsd
) != 1)
128 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
129 fsp
->fsu_blocks
= CONVERT_BLOCKS (fsd
.fd_req
.btot
);
130 fsp
->fsu_bfree
= CONVERT_BLOCKS (fsd
.fd_req
.bfree
);
131 fsp
->fsu_bavail
= CONVERT_BLOCKS (fsd
.fd_req
.bfreen
);
132 fsp
->fsu_files
= fsd
.fd_req
.gtot
;
133 fsp
->fsu_ffree
= fsd
.fd_req
.gfree
;
136 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
139 if (statfs (path
, &fsd
) < 0)
141 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
144 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
147 if (statfs (path
, &fsd
) < 0)
149 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
152 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */
155 if (statfs (path
, &fsd
, sizeof fsd
, 0) < 0)
157 /* Empirically, the block counts on most SVR3 and SVR3-derived
158 systems seem to always be in terms of 512-byte blocks,
159 no matter what value f_bsize has. */
161 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
163 #define CONVERT_BLOCKS(b) (b)
164 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
165 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
166 #define f_bavail f_bfree
172 #ifdef STAT_STATVFS /* SVR4. */
175 if (statvfs (path
, &fsd
) < 0)
177 /* f_frsize isn't guaranteed to be supported. */
178 #define CONVERT_BLOCKS(b) \
179 fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
182 #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */
183 fsp
->fsu_blocks
= CONVERT_BLOCKS (fsd
.f_blocks
);
184 fsp
->fsu_bfree
= CONVERT_BLOCKS (fsd
.f_bfree
);
185 fsp
->fsu_bavail
= CONVERT_BLOCKS (fsd
.f_bavail
);
186 fsp
->fsu_files
= fsd
.f_files
;
187 fsp
->fsu_ffree
= fsd
.f_ffree
;
193 #endif /* HAVE_INFOMOUNT */