xref: /original-bsd/bin/df/df.c (revision 76210d32)
1 /*
2  * Copyright (c) 1980, 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980, 1990 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)df.c	5.21 (Berkeley) 06/24/90";
16 #endif /* not lint */
17 
18 /*
19  * df
20  */
21 #include <sys/param.h>
22 #include <sys/stat.h>
23 #include <sys/mount.h>
24 #include <sys/file.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 char	*getmntpt();
30 int	iflag, kflag, nflag;
31 struct	ufs_args mdev;
32 
33 main(argc, argv)
34 	int argc;
35 	char **argv;
36 {
37 	extern int errno, optind;
38 	int err, ch, i;
39 	long width, maxwidth, mntsize, getmntinfo();
40 	char *mntpt, *mktemp();
41 	struct stat stbuf;
42 	struct statfs statfsbuf, *mntbuf;
43 
44 	while ((ch = getopt(argc, argv, "ikn")) != EOF)
45 		switch(ch) {
46 		case 'i':
47 			iflag = 1;
48 			break;
49 		case 'k':
50 			kflag = 1;
51 			break;
52 		case 'n':
53 			nflag = 1;
54 			break;
55 		case '?':
56 		default:
57 			fprintf(stderr,
58 			    "usage: df [-ikn] [file | file_system ...]\n");
59 			exit(1);
60 		}
61 	argc -= optind;
62 	argv += optind;
63 
64 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
65 	maxwidth = 0;
66 	for (i = 0; i < mntsize; i++) {
67 		width = strlen(mntbuf[i].f_mntfromname);
68 		if (width > maxwidth)
69 			maxwidth = width;
70 	}
71 	if (!*argv) {
72 		mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
73 		for (i = 0; i < mntsize; i++)
74 			prtstat(&mntbuf[i], maxwidth);
75 		exit(0);
76 	}
77 	for (; *argv; argv++) {
78 		if (stat(*argv, &stbuf) < 0) {
79 			err = errno;
80 			if ((mntpt = getmntpt(*argv)) == 0) {
81 				fprintf(stderr, "df: %s: %s\n", *argv,
82 				    strerror(err));
83 				continue;
84 			}
85 		} else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) {
86 			ufs_df(*argv, maxwidth);
87 			continue;
88 		} else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
89 			if ((mntpt = getmntpt(*argv)) == 0) {
90 				mntpt = mktemp("/tmp/df.XXXXXX");
91 				mdev.fspec = *argv;
92 				if (mkdir(mntpt) != 0) {
93 					fprintf(stderr, "df: %s: %s\n",
94 					    mntpt, strerror(errno));
95 					continue;
96 				}
97 				if (mount(MOUNT_UFS, mntpt, MNT_RDONLY,
98 				    &mdev) != 0) {
99 					ufs_df(*argv, maxwidth);
100 					(void)rmdir(mntpt);
101 					continue;
102 				} else if (statfs(mntpt, &statfsbuf)) {
103 					statfsbuf.f_mntonname[0] = '\0';
104 					prtstat(&statfsbuf, maxwidth);
105 				} else
106 					fprintf(stderr, "df: %s: %s\n",
107 					    *argv, strerror(errno));
108 				(void)unmount(mntpt, MNT_NOFORCE);
109 				(void)rmdir(mntpt);
110 				continue;
111 			}
112 		} else
113 			mntpt = *argv;
114 		/*
115 		 * Statfs does not take a `wait' flag, so we cannot
116 		 * implement nflag here.
117 		 */
118 		if (statfs(mntpt, &statfsbuf) < 0) {
119 			fprintf(stderr,
120 			    "df: %s: %s\n", mntpt, strerror(errno));
121 			continue;
122 		}
123 		if (argc == 1)
124 			maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
125 		prtstat(&statfsbuf, maxwidth);
126 	}
127 	exit(0);
128 }
129 
130 char *
131 getmntpt(name)
132 	char *name;
133 {
134 	long mntsize, i;
135 	struct statfs *mntbuf;
136 
137 	mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
138 	for (i = 0; i < mntsize; i++) {
139 		if (!strcmp(mntbuf[i].f_mntfromname, name))
140 			return (mntbuf[i].f_mntonname);
141 	}
142 	return (0);
143 }
144 
145 /*
146  * Print out status about a filesystem.
147  */
148 prtstat(sfsp, maxwidth)
149 	register struct statfs *sfsp;
150 	long maxwidth;
151 {
152 	long used, availblks, inodes;
153 	static int timesthrough;
154 
155 	if (maxwidth < 11)
156 		maxwidth = 11;
157 	if (++timesthrough == 1) {
158 		printf("%-*.*s%s    used   avail capacity",
159 		    maxwidth, maxwidth, "Filesystem",
160 		    kflag ? "  kbytes" : "512-blks");
161 		if (iflag)
162 			printf(" iused   ifree  %%iused");
163 		printf("  Mounted on\n");
164 	}
165 	printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
166 	used = sfsp->f_blocks - sfsp->f_bfree;
167 	availblks = sfsp->f_bavail + used;
168 	printf("%8ld%8ld%8ld",
169 	    sfsp->f_blocks * sfsp->f_fsize / (kflag ? 1024 : 512),
170 	    used * sfsp->f_fsize / (kflag ? 1024 : 512),
171 	    sfsp->f_bavail * sfsp->f_fsize / (kflag ? 1024 : 512));
172 	printf("%6.0f%%",
173 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
174 	if (iflag) {
175 		inodes = sfsp->f_files;
176 		used = inodes - sfsp->f_ffree;
177 		printf("%8ld%8ld%6.0f%% ", used, sfsp->f_ffree,
178 		   inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
179 	} else
180 		printf("  ");
181 	printf("  %s\n", sfsp->f_mntonname);
182 }
183 
184 /*
185  * This code constitutes the old df code for extracting
186  * information from filesystem superblocks.
187  */
188 #include <ufs/fs.h>
189 #include <errno.h>
190 #include <fstab.h>
191 
192 union {
193 	struct fs iu_fs;
194 	char dummy[SBSIZE];
195 } sb;
196 #define sblock sb.iu_fs
197 
198 int	fi;
199 char	*strcpy();
200 
201 ufs_df(file, maxwidth)
202 	char *file;
203 	long maxwidth;
204 {
205 	extern int errno;
206 	struct stat stbuf;
207 	struct statfs statfsbuf;
208 	register struct statfs *sfsp;
209 	struct fstab *fsp;
210 	char *mntpt;
211 	static int synced;
212 
213 	if (synced++ == 0)
214 		sync();
215 
216 	if ((fi = open(file, O_RDONLY)) < 0) {
217 		fprintf(stderr, "df: %s: %s\n", file, strerror(errno));
218 		return;
219 	}
220 	if (bread((long)SBOFF, (char *)&sblock, SBSIZE) == 0) {
221 		(void) close(fi);
222 		return;
223 	}
224 	sfsp = &statfsbuf;
225 	sfsp->f_type = MOUNT_UFS;
226 	sfsp->f_flags = 0;
227 	sfsp->f_fsize = sblock.fs_fsize;
228 	sfsp->f_bsize = sblock.fs_bsize;
229 	sfsp->f_blocks = sblock.fs_dsize;
230 	sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
231 		sblock.fs_cstotal.cs_nffree;
232 	sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
233 		(sblock.fs_dsize - sfsp->f_bfree);
234 	if (sfsp->f_bavail < 0)
235 		sfsp->f_bavail = 0;
236 	sfsp->f_files =  sblock.fs_ncg * sblock.fs_ipg;
237 	sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
238 	sfsp->f_fsid.val[0] = 0;
239 	sfsp->f_fsid.val[1] = 0;
240 	if ((mntpt = getmntpt(file)) == 0)
241 		mntpt = "";
242 	bcopy((caddr_t)mntpt, (caddr_t)&sfsp->f_mntonname[0], MNAMELEN);
243 	bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN);
244 	prtstat(sfsp, maxwidth);
245 	(void) close(fi);
246 }
247 
248 long lseek();
249 
250 bread(off, buf, cnt)
251 	long off;
252 	char *buf;
253 {
254 	int n;
255 	extern errno;
256 
257 	(void) lseek(fi, off, SEEK_SET);
258 	if ((n=read(fi, buf, cnt)) != cnt) {
259 		/* probably a dismounted disk if errno == EIO */
260 		if (errno != EIO) {
261 			printf("\nread error off = %ld\n", off);
262 			printf("count = %d; errno = %d\n", n, errno);
263 		}
264 		return (0);
265 	}
266 	return (1);
267 }
268