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. */
42 #ifdef HAVE_SYS_STATFS_H
43 #include <sys/statfs.h>
46 #ifdef HAVE_DUSTAT_H /* AIX PS/2. */
47 #include <sys/dustat.h>
50 #ifdef HAVE_SYS_STATVFS_H /* SVR4. */
51 #include <sys/statvfs.h>
60 /* Return the number of TOSIZE-byte blocks used by
61 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
62 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
65 fs_adjust_blocks (long blocks
, int fromsize
, int tosize
)
72 if (fromsize
== tosize
) /* E.g., from 512 to 512. */
74 else if (fromsize
> tosize
) /* E.g., from 2048 to 512. */
75 return blocks
* (fromsize
/ tosize
);
76 else /* E.g., from 256 to 512. */
77 return (blocks
+ (blocks
< 0 ? -1 : 1)) / (tosize
/ fromsize
);
80 #if defined(_AIX) && defined(_I386)
81 /* AIX PS/2 does not supply statfs. */
82 static int aix_statfs (char *path
, struct statfs
*fsb
)
87 if (stat (path
, &stats
))
89 if (dustat (stats
.st_dev
, 0, &fsd
, sizeof (fsd
)))
92 fsb
->f_bsize
= fsd
.du_bsize
;
93 fsb
->f_blocks
= fsd
.du_fsize
- fsd
.du_isize
;
94 fsb
->f_bfree
= fsd
.du_tfree
;
95 fsb
->f_bavail
= fsd
.du_tfree
;
96 fsb
->f_files
= (fsd
.du_isize
- 2) * fsd
.du_inopb
;
97 fsb
->f_ffree
= fsd
.du_tinode
;
98 fsb
->f_fsid
.val
[0] = fsd
.du_site
;
99 fsb
->f_fsid
.val
[1] = fsd
.du_pckno
;
102 #define statfs(path,fsb) aix_statfs(path,fsb)
103 #endif /* _AIX && _I386 */
105 /* Fill in the fields of FSP with information about space usage for
106 the filesystem on which PATH resides.
107 Return 0 if successful, -1 if not. */
109 int get_fs_usage (char *path
, struct fs_usage
*fsp
)
111 #ifdef STAT_STATFS3_OSF1
114 if (statfs (path
, &fsd
, sizeof (struct statfs
)) != 0)
116 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
117 #endif /* STAT_STATFS3_OSF1 */
119 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
122 if (statfs (path
, &fsd
) != 1)
124 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
125 fsp
->fsu_blocks
= CONVERT_BLOCKS (fsd
.fd_req
.btot
);
126 fsp
->fsu_bfree
= CONVERT_BLOCKS (fsd
.fd_req
.bfree
);
127 fsp
->fsu_bavail
= CONVERT_BLOCKS (fsd
.fd_req
.bfreen
);
128 fsp
->fsu_files
= fsd
.fd_req
.gtot
;
129 fsp
->fsu_ffree
= fsd
.fd_req
.gfree
;
132 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
135 if (statfs (path
, &fsd
) < 0)
137 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
140 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
143 if (statfs (path
, &fsd
) < 0)
145 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
148 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */
151 if (statfs (path
, &fsd
, sizeof fsd
, 0) < 0)
153 /* Empirically, the block counts on most SVR3 and SVR3-derived
154 systems seem to always be in terms of 512-byte blocks,
155 no matter what value f_bsize has. */
157 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
159 #define CONVERT_BLOCKS(b) (b)
160 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
161 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
162 #define f_bavail f_bfree
168 #ifdef STAT_STATVFS /* SVR4. */
171 if (statvfs (path
, &fsd
) < 0)
173 /* f_frsize isn't guaranteed to be supported. */
174 #define CONVERT_BLOCKS(b) \
175 fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
178 #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */
179 fsp
->fsu_blocks
= CONVERT_BLOCKS (fsd
.f_blocks
);
180 fsp
->fsu_bfree
= CONVERT_BLOCKS (fsd
.f_bfree
);
181 fsp
->fsu_bavail
= CONVERT_BLOCKS (fsd
.f_bavail
);
182 fsp
->fsu_files
= fsd
.f_files
;
183 fsp
->fsu_ffree
= fsd
.f_ffree
;
189 #endif /* HAVE_INFOMOUNT */