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