specify MNT_NOWAIT when getting filesystem information
[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
93e6cf82 25static char sccsid[] = "@(#)df.c 5.15 (Berkeley) %G%";
7e8aa4e0 26#endif /* not lint */
a98f5f82 27
8d335f7d
BJ
28/*
29 * df
30 */
fc2797e7 31#include <sys/param.h>
1e568652 32#include <sys/stat.h>
704aadd2 33#include <sys/mount.h>
fc2797e7
KB
34#include <sys/file.h>
35#include <stdio.h>
36#include <strings.h>
37#include <unistd.h>
c7b72342 38
1e568652 39char *getmntpt();
93e6cf82 40int iflag, kflag, nflag;
704aadd2
KM
41#ifdef COMPAT_43
42int oflag;
43#endif /* COMPAT_43 */
c7b72342
BJ
44
45main(argc, argv)
a98f5f82
BJ
46 int argc;
47 char **argv;
c7b72342 48{
dbdc06d4 49 extern int errno, optind;
cf537218 50 int err, ch, i;
fc2797e7
KB
51 long mntsize, getmntinfo();
52 char *mntpt, *mktemp();
1e568652 53 struct stat stbuf;
704aadd2 54 struct statfs statfsbuf, *mntbuf;
439847aa 55 struct ufs_args mdev;
c7b72342 56
93e6cf82 57 while ((ch = getopt(argc, argv, "ikon")) != EOF)
dbdc06d4 58 switch(ch) {
8a7d0cad 59 case 'i':
fc2797e7
KB
60 iflag = 1;
61 break;
62 case 'k':
63 kflag = 1;
8a7d0cad 64 break;
93e6cf82
KM
65 case 'n':
66 nflag = 1;
67 break;
704aadd2
KM
68#ifdef COMPAT_43
69 case 'o':
fc2797e7 70 oflag = 1;
704aadd2
KM
71 break;
72#endif /* COMPAT_43 */
dbdc06d4 73 case '?':
8a7d0cad 74 default:
fc2797e7 75 fprintf(stderr,
93e6cf82 76 "usage: df [-ikn] [file | file_system ...]\n");
dbdc06d4 77 exit(1);
8a7d0cad 78 }
dbdc06d4
KB
79 argc -= optind;
80 argv += optind;
81
fc2797e7
KB
82 printf("Filesystem %s used avail capacity",
83 kflag ? " kbytes" : "512-blks");
704aadd2
KM
84 if (iflag)
85 printf(" iused ifree %%iused");
86 printf(" Mounted on\n");
87#ifdef COMPAT_43
88 if (oflag) {
89 olddf(argv);
90 exit(0);
91 }
92#endif /* COMPAT_43 */
93 if (!*argv) {
93e6cf82 94 mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
704aadd2
KM
95 for (i = 0; i < mntsize; i++)
96 prtstat(&mntbuf[i]);
97 exit(0);
98 }
99 for (; *argv; argv++) {
1e568652 100 if (stat(*argv, &stbuf) < 0) {
cf537218
KM
101 err = errno;
102 if ((mntpt = getmntpt(*argv)) == 0) {
fc2797e7
KB
103 fprintf(stderr, "df: %s: %s\n", *argv,
104 strerror(err));
1e568652 105 continue;
cf537218 106 }
1e568652
KM
107 } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
108 if ((mntpt = getmntpt(*argv)) == 0)
93e6cf82 109 mntpt = mktemp("/tmp/df.XXXXXX");
439847aa
KM
110 mdev.fspec = *argv;
111 if (!mkdir(mntpt) &&
112 !mount(MOUNT_UFS, mntpt, M_RDONLY, &mdev) &&
113 !statfs(mntpt, &statfsbuf)) {
114 statfsbuf.f_mntonname[0] = '\0';
115 prtstat(&statfsbuf);
fc2797e7
KB
116 } else
117 fprintf(stderr, "df: %s: %s\n",
118 *argv, strerror(errno));
adc92dc2 119 (void)unmount(mntpt, MNT_NOFORCE);
fc2797e7 120 (void)rmdir(mntpt);
1e568652
KM
121 continue;
122 } else
123 mntpt = *argv;
93e6cf82
KM
124 /*
125 * Statfs does not take a `wait' flag, so we cannot
126 * implement nflag here.
127 */
1e568652 128 if (statfs(mntpt, &statfsbuf) < 0) {
fc2797e7
KB
129 fprintf(stderr,
130 "df: %s: %s\n", mntpt, strerror(errno));
704aadd2
KM
131 continue;
132 }
133 prtstat(&statfsbuf);
134 }
135 exit(0);
136}
137
1e568652
KM
138char *
139getmntpt(name)
140 char *name;
141{
142 long mntsize, i;
143 struct statfs *mntbuf;
144
93e6cf82 145 mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
1e568652
KM
146 for (i = 0; i < mntsize; i++) {
147 if (!strcmp(mntbuf[i].f_mntfromname, name))
148 return (mntbuf[i].f_mntonname);
149 }
150 return (0);
151}
152
704aadd2
KM
153/*
154 * Print out status about a filesystem.
155 */
156prtstat(sfsp)
157 register struct statfs *sfsp;
158{
159 long used, availblks, inodes;
160
161 printf("%-12.12s", sfsp->f_mntfromname);
162 used = sfsp->f_blocks - sfsp->f_bfree;
163 availblks = sfsp->f_bavail + used;
fc2797e7
KB
164 printf("%8ld%8ld%8ld",
165 sfsp->f_blocks * sfsp->f_fsize / (kflag ? 1024 : 512),
166 used * sfsp->f_fsize / (kflag ? 1024 : 512),
167 sfsp->f_bavail * sfsp->f_fsize / (kflag ? 1024 : 512));
704aadd2
KM
168 printf("%6.0f%%",
169 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
170 if (iflag) {
171 inodes = sfsp->f_files;
172 used = inodes - sfsp->f_ffree;
173 printf("%8ld%8ld%6.0f%% ", used, sfsp->f_ffree,
174 inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
175 } else
176 printf(" ");
177 printf(" %s\n", sfsp->f_mntonname);
178}
179
180#ifdef COMPAT_43
181/*
182 * This code constitutes the old df code for extracting
183 * information from filesystem superblocks.
184 */
704aadd2 185#include <ufs/fs.h>
704aadd2
KM
186#include <errno.h>
187#include <fstab.h>
704aadd2 188
fc2797e7 189char root[MAXPATHLEN];
704aadd2
KM
190
191union {
192 struct fs iu_fs;
193 char dummy[SBSIZE];
194} sb;
195#define sblock sb.iu_fs
196
197int fi;
198char *strcpy();
199
200olddf(argv)
201 char *argv[];
202{
203 struct fstab *fsp;
704aadd2 204
dda19894 205 sync();
dbdc06d4 206 if (!*argv) {
8d335f7d 207 if (setfsent() == 0)
3b72df1c 208 perror(_PATH_FSTAB), exit(1);
a98f5f82 209 while (fsp = getfsent()) {
1f65a82f 210 if (strcmp(fsp->fs_type, FSTAB_RW) &&
351ef297
SL
211 strcmp(fsp->fs_type, FSTAB_RO) &&
212 strcmp(fsp->fs_type, FSTAB_RQ))
f0861f40 213 continue;
c7b72342 214 if (root[0] == 0)
f4d092e2 215 (void) strcpy(root, fsp->fs_spec);
a98f5f82 216 dfree(fsp->fs_spec, 1);
c7b72342 217 }
6c5c8ac3 218 (void)endfsent();
c7b72342
BJ
219 exit(0);
220 }
dbdc06d4
KB
221 while (*argv)
222 dfree(*argv++, 0);
704aadd2 223 exit(0);
c7b72342
BJ
224}
225
a98f5f82
BJ
226dfree(file, infsent)
227 char *file;
228 int infsent;
c7b72342 229{
dbdc06d4 230 extern int errno;
a98f5f82 231 struct stat stbuf;
704aadd2
KM
232 struct statfs statfsbuf;
233 register struct statfs *sfsp;
a98f5f82 234 struct fstab *fsp;
fc2797e7 235 char *mntpt;
a98f5f82
BJ
236
237 if (stat(file, &stbuf) == 0 &&
238 (stbuf.st_mode&S_IFMT) != S_IFCHR &&
239 (stbuf.st_mode&S_IFMT) != S_IFBLK) {
240 if (infsent) {
fc2797e7 241 fprintf(stderr, "df: %s: screwy fstab entry\n", file);
8d335f7d
BJ
242 return;
243 }
6c5c8ac3 244 (void)setfsent();
a98f5f82
BJ
245 while (fsp = getfsent()) {
246 struct stat stb;
247
248 if (stat(fsp->fs_spec, &stb) == 0 &&
249 stb.st_rdev == stbuf.st_dev) {
250 file = fsp->fs_spec;
6c5c8ac3 251 (void)endfsent();
a98f5f82
BJ
252 goto found;
253 }
254 }
6c5c8ac3 255 (void)endfsent();
fc2797e7 256 fprintf(stderr, "df: %s: mounted on unknown device\n", file);
a98f5f82 257 return;
8d335f7d 258 }
a98f5f82 259found:
fc2797e7 260 if ((fi = open(file, O_RDONLY)) < 0) {
dbdc06d4 261 fprintf(stderr, "df: %s: %s\n", file, strerror(errno));
c7b72342
BJ
262 return;
263 }
6c5c8ac3 264 if (bread((long)SBOFF, (char *)&sblock, SBSIZE) == 0) {
f5064bf4
RC
265 (void) close(fi);
266 return;
267 }
704aadd2
KM
268 sfsp = &statfsbuf;
269 sfsp->f_type = MOUNT_UFS;
270 sfsp->f_flags = 0;
271 sfsp->f_fsize = sblock.fs_fsize;
272 sfsp->f_bsize = sblock.fs_bsize;
273 sfsp->f_blocks = sblock.fs_dsize;
274 sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
275 sblock.fs_cstotal.cs_nffree;
276 sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
277 (sblock.fs_dsize - sfsp->f_bfree);
278 if (sfsp->f_bavail < 0)
279 sfsp->f_bavail = 0;
280 sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg;
281 sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
282 sfsp->f_fsid.val[0] = 0;
283 sfsp->f_fsid.val[1] = 0;
1e568652
KM
284 if ((mntpt = getmntpt(file)) == 0)
285 mntpt = "";
286 bcopy((caddr_t)mntpt, (caddr_t)&sfsp->f_mntonname[0], MNAMELEN);
704aadd2
KM
287 bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN);
288 prtstat(sfsp);
f4d092e2 289 (void) close(fi);
c7b72342
BJ
290}
291
f4d092e2
KM
292long lseek();
293
6c5c8ac3
KM
294bread(off, buf, cnt)
295 long off;
a98f5f82 296 char *buf;
c7b72342
BJ
297{
298 int n;
299 extern errno;
300
fc2797e7 301 (void) lseek(fi, off, SEEK_SET);
a98f5f82 302 if ((n=read(fi, buf, cnt)) != cnt) {
f5064bf4
RC
303 /* probably a dismounted disk if errno == EIO */
304 if (errno != EIO) {
6c5c8ac3 305 printf("\nread error off = %ld\n", off);
f5064bf4
RC
306 printf("count = %d; errno = %d\n", n, errno);
307 }
308 return (0);
c7b72342 309 }
f5064bf4 310 return (1);
c7b72342 311}
704aadd2 312#endif /* COMPAT_43 */