use definition S_IFBLK from stat.h rather than IFBLK from inode.h
[unix-history] / usr / src / bin / df / df.c
CommitLineData
bcf1365c 1/*
704aadd2
KM
2 * Copyright (c) 1980 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
bcf1365c
DF
16 */
17
18#ifndef lint
19char copyright[] =
704aadd2 20"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
bcf1365c 21 All rights reserved.\n";
7e8aa4e0 22#endif /* not lint */
bcf1365c 23
97dd7000 24#ifndef lint
704aadd2 25static char sccsid[] = "@(#)df.c 5.7 (Berkeley) %G%";
7e8aa4e0 26#endif /* not lint */
a98f5f82 27
8d335f7d
BJ
28/*
29 * df
30 */
704aadd2
KM
31#include <stdio.h>
32#include <sys/types.h>
33#include <sys/mount.h>
34#include "pathnames.h"
c7b72342 35
c7b72342 36int iflag;
704aadd2
KM
37#ifdef COMPAT_43
38int oflag;
39#endif /* COMPAT_43 */
c7b72342
BJ
40
41main(argc, argv)
a98f5f82
BJ
42 int argc;
43 char **argv;
c7b72342 44{
dbdc06d4
KB
45 extern int errno, optind;
46 int ch, i;
704aadd2
KM
47 long mntsize;
48 struct statfs statfsbuf, *mntbuf;
c7b72342 49
704aadd2 50 while ((ch = getopt(argc, argv, "io")) != EOF)
dbdc06d4 51 switch(ch) {
8a7d0cad
SL
52 case 'i':
53 iflag++;
54 break;
704aadd2
KM
55#ifdef COMPAT_43
56 case 'o':
57 oflag++;
58 break;
59#endif /* COMPAT_43 */
dbdc06d4 60 case '?':
8a7d0cad 61 default:
dbdc06d4
KB
62 fprintf(stderr, "usage: df [-i] [filsys ...]\n");
63 exit(1);
8a7d0cad 64 }
dbdc06d4
KB
65 argc -= optind;
66 argv += optind;
67
704aadd2
KM
68 printf("Filesystem kbytes used avail capacity");
69 if (iflag)
70 printf(" iused ifree %%iused");
71 printf(" Mounted on\n");
72#ifdef COMPAT_43
73 if (oflag) {
74 olddf(argv);
75 exit(0);
76 }
77#endif /* COMPAT_43 */
78 if (!*argv) {
79 if ((mntsize = getfsstat(0, 0)) < 0) {
80 perror("df");
81 exit(1);
82 }
83 mntbuf = 0;
84 do {
85 if (mntbuf)
86 free(mntbuf);
87 i = (mntsize + 1) * sizeof(struct statfs);
88 if ((mntbuf = (struct statfs *)malloc(i)) == 0) {
89 fprintf(stderr,
90 "no space for mount table buffer\n");
91 exit(1);
92 }
93 if ((mntsize = getfsstat(mntbuf, i)) < 0) {
94 perror("df");
95 exit(1);
96 }
97 } while (i == mntsize * sizeof(struct statfs));
98 for (i = 0; i < mntsize; i++)
99 prtstat(&mntbuf[i]);
100 exit(0);
101 }
102 for (; *argv; argv++) {
103 if (statfs(*argv, &statfsbuf) < 0) {
104 perror(*argv);
105 continue;
106 }
107 prtstat(&statfsbuf);
108 }
109 exit(0);
110}
111
112/*
113 * Print out status about a filesystem.
114 */
115prtstat(sfsp)
116 register struct statfs *sfsp;
117{
118 long used, availblks, inodes;
119
120 printf("%-12.12s", sfsp->f_mntfromname);
121 used = sfsp->f_blocks - sfsp->f_bfree;
122 availblks = sfsp->f_bavail + used;
123 printf("%8ld%8ld%8ld", sfsp->f_blocks * sfsp->f_fsize / 1024,
124 used * sfsp->f_fsize / 1024, sfsp->f_bavail * sfsp->f_fsize / 1024);
125 printf("%6.0f%%",
126 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
127 if (iflag) {
128 inodes = sfsp->f_files;
129 used = inodes - sfsp->f_ffree;
130 printf("%8ld%8ld%6.0f%% ", used, sfsp->f_ffree,
131 inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
132 } else
133 printf(" ");
134 printf(" %s\n", sfsp->f_mntonname);
135}
136
137#ifdef COMPAT_43
138/*
139 * This code constitutes the old df code for extracting
140 * information from filesystem superblocks.
141 */
142#include <sys/param.h>
143#include <ufs/fs.h>
144#include <sys/stat.h>
145#include <errno.h>
146#include <fstab.h>
147#include <mtab.h>
148
149struct mtab mtab[NMOUNT];
150char root[32];
151char *mpath();
152
153union {
154 struct fs iu_fs;
155 char dummy[SBSIZE];
156} sb;
157#define sblock sb.iu_fs
158
159int fi;
160char *strcpy();
161
162olddf(argv)
163 char *argv[];
164{
165 struct fstab *fsp;
166 char *strerror();
167 int i;
168
dbdc06d4 169 if ((i = open(_PATH_MTAB, 0)) < 0) {
704aadd2
KM
170 fprintf(stderr, "df: %s: %s\n", _PATH_MTAB,
171 strerror(errno));
dbdc06d4 172 exit(1);
c7b72342 173 }
dbdc06d4
KB
174 (void) read(i, (char *)mtab, sizeof (mtab));
175 (void) close(i);
dda19894 176 sync();
dbdc06d4 177 if (!*argv) {
8d335f7d 178 if (setfsent() == 0)
3b72df1c 179 perror(_PATH_FSTAB), exit(1);
a98f5f82 180 while (fsp = getfsent()) {
1f65a82f 181 if (strcmp(fsp->fs_type, FSTAB_RW) &&
351ef297
SL
182 strcmp(fsp->fs_type, FSTAB_RO) &&
183 strcmp(fsp->fs_type, FSTAB_RQ))
f0861f40 184 continue;
c7b72342 185 if (root[0] == 0)
f4d092e2 186 (void) strcpy(root, fsp->fs_spec);
a98f5f82 187 dfree(fsp->fs_spec, 1);
c7b72342 188 }
6c5c8ac3 189 (void)endfsent();
c7b72342
BJ
190 exit(0);
191 }
dbdc06d4
KB
192 while (*argv)
193 dfree(*argv++, 0);
704aadd2 194 exit(0);
c7b72342
BJ
195}
196
a98f5f82
BJ
197dfree(file, infsent)
198 char *file;
199 int infsent;
c7b72342 200{
dbdc06d4 201 extern int errno;
a98f5f82 202 struct stat stbuf;
704aadd2
KM
203 struct statfs statfsbuf;
204 register struct statfs *sfsp;
a98f5f82 205 struct fstab *fsp;
dbdc06d4 206 char *strerror();
a98f5f82
BJ
207
208 if (stat(file, &stbuf) == 0 &&
209 (stbuf.st_mode&S_IFMT) != S_IFCHR &&
210 (stbuf.st_mode&S_IFMT) != S_IFBLK) {
211 if (infsent) {
f6c7ec9d 212 fprintf(stderr, "%s: screwy fstab entry\n", file);
8d335f7d
BJ
213 return;
214 }
6c5c8ac3 215 (void)setfsent();
a98f5f82
BJ
216 while (fsp = getfsent()) {
217 struct stat stb;
218
219 if (stat(fsp->fs_spec, &stb) == 0 &&
220 stb.st_rdev == stbuf.st_dev) {
221 file = fsp->fs_spec;
6c5c8ac3 222 (void)endfsent();
a98f5f82
BJ
223 goto found;
224 }
225 }
6c5c8ac3 226 (void)endfsent();
a98f5f82
BJ
227 fprintf(stderr, "%s: mounted on unknown device\n", file);
228 return;
8d335f7d 229 }
a98f5f82 230found:
dbdc06d4
KB
231 if ((fi = open(file, 0)) < 0) {
232 fprintf(stderr, "df: %s: %s\n", file, strerror(errno));
c7b72342
BJ
233 return;
234 }
6c5c8ac3 235 if (bread((long)SBOFF, (char *)&sblock, SBSIZE) == 0) {
f5064bf4
RC
236 (void) close(fi);
237 return;
238 }
704aadd2
KM
239 sfsp = &statfsbuf;
240 sfsp->f_type = MOUNT_UFS;
241 sfsp->f_flags = 0;
242 sfsp->f_fsize = sblock.fs_fsize;
243 sfsp->f_bsize = sblock.fs_bsize;
244 sfsp->f_blocks = sblock.fs_dsize;
245 sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
246 sblock.fs_cstotal.cs_nffree;
247 sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
248 (sblock.fs_dsize - sfsp->f_bfree);
249 if (sfsp->f_bavail < 0)
250 sfsp->f_bavail = 0;
251 sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg;
252 sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
253 sfsp->f_fsid.val[0] = 0;
254 sfsp->f_fsid.val[1] = 0;
255 bcopy((caddr_t)mpath(file), (caddr_t)&sfsp->f_mntonname[0], MNAMELEN);
256 bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN);
257 prtstat(sfsp);
f4d092e2 258 (void) close(fi);
c7b72342
BJ
259}
260
f4d092e2
KM
261long lseek();
262
6c5c8ac3
KM
263bread(off, buf, cnt)
264 long off;
a98f5f82 265 char *buf;
c7b72342
BJ
266{
267 int n;
268 extern errno;
269
6c5c8ac3 270 (void) lseek(fi, off, 0);
a98f5f82 271 if ((n=read(fi, buf, cnt)) != cnt) {
f5064bf4
RC
272 /* probably a dismounted disk if errno == EIO */
273 if (errno != EIO) {
6c5c8ac3 274 printf("\nread error off = %ld\n", off);
f5064bf4
RC
275 printf("count = %d; errno = %d\n", n, errno);
276 }
277 return (0);
c7b72342 278 }
f5064bf4 279 return (1);
c7b72342
BJ
280}
281
282/*
283 * Given a name like /dev/rrp0h, returns the mounted path, like /usr.
284 */
8a7d0cad
SL
285char *
286mpath(file)
a98f5f82 287 char *file;
c7b72342 288{
8a7d0cad 289 register struct mtab *mp;
c7b72342
BJ
290
291 if (eq(file, root))
8a7d0cad
SL
292 return ("/");
293 for (mp = mtab; mp < mtab + NMOUNT; mp++)
294 if (eq(file, mp->m_dname))
295 return (mp->m_path);
c7b72342
BJ
296 return "";
297}
298
299eq(f1, f2)
a98f5f82 300 char *f1, *f2;
c7b72342 301{
a98f5f82 302
f6c7ec9d 303 if (strncmp(f1, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
c7b72342 304 f1 += 5;
f6c7ec9d 305 if (strncmp(f2, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
c7b72342 306 f2 += 5;
a98f5f82
BJ
307 if (!strcmp(f1, f2))
308 return (1);
309 if (*f1 == 'r' && !strcmp(f1+1, f2))
310 return (1);
311 if (*f2 == 'r' && !strcmp(f1, f2+1))
312 return (1);
c7b72342 313 if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
a98f5f82
BJ
314 return (1);
315 return (0);
c7b72342 316}
704aadd2 317#endif /* COMPAT_43 */