xref: /original-bsd/bin/df/df.c (revision fbed46ce)
1 #ifndef lint
2 static	char *sccsid = "@(#)df.c	4.12 05/07/82";
3 #endif
4 
5 #include <stdio.h>
6 #include <fstab.h>
7 #include <sys/param.h>
8 #include <sys/fs.h>
9 #include <sys/stat.h>
10 
11 /*
12  * df
13  */
14 #define NFS	20	/* Max number of filesystems */
15 
16 struct {
17 	char path[32];
18 	char spec[32];
19 } mtab[NFS];
20 char	root[32];
21 char	*mpath();
22 
23 int	iflag;
24 
25 union {
26 	struct fs iu_fs;
27 	char dummy[SBSIZE];
28 } sb;
29 #define sblock sb.iu_fs
30 
31 int	fi;
32 daddr_t	alloc();
33 char	*strcpy();
34 
35 main(argc, argv)
36 	int argc;
37 	char **argv;
38 {
39 	int i;
40 
41 	while (argc >= 1 && argv[1][0]=='-') {
42 	switch (argv[1][1]) {
43 
44 	case 'i':
45 		iflag++;
46 		break;
47 
48 	default:
49 		fprintf(stderr, "usage: df [ -i ] [ filsys... ]\n");
50 		exit(0);
51 	}
52 	argc--, argv++;
53 	}
54 	i = open("/etc/mtab", 0);
55 	if (i >= 0) {
56 		(void) read(i, (char *)mtab, sizeof mtab);
57 		(void) close(i);
58 	}
59 	printf("Filesystem    kbytes    used   avail capacity");
60 	if (iflag)
61 		printf(" iused   ifree  %%iused");
62 	printf("  Mounted on\n");
63 	if (argc <= 1) {
64 		struct fstab *fsp;
65 
66 		if (setfsent() == 0)
67 			perror(FSTAB), exit(1);
68 		while (fsp = getfsent()) {
69 			if (!strcmp(fsp->fs_type, FSTAB_RW) &&
70 			    !(strcmp(fsp->fs_type, FSTAB_RO)))
71 				continue;
72 			if (root[0] == 0)
73 				(void) strcpy(root, fsp->fs_spec);
74 			dfree(fsp->fs_spec, 1);
75 		}
76 		endfsent();
77 		exit(0);
78 	}
79 	for (i=1; i<argc; i++)
80 		dfree(argv[i], 0);
81 }
82 
83 dfree(file, infsent)
84 	char *file;
85 	int infsent;
86 {
87 	long totalblks, availblks, avail, free, used;
88 	struct stat stbuf;
89 	struct fstab *fsp;
90 
91 	if (stat(file, &stbuf) == 0 &&
92 	    (stbuf.st_mode&S_IFMT) != S_IFCHR &&
93 	    (stbuf.st_mode&S_IFMT) != S_IFBLK) {
94 		if (infsent) {
95 			fprintf(stderr, "%s: screwy /etc/fstab entry\n", file);
96 			return;
97 		}
98 		setfsent();
99 		while (fsp = getfsent()) {
100 			struct stat stb;
101 
102 			if (stat(fsp->fs_spec, &stb) == 0 &&
103 			    stb.st_rdev == stbuf.st_dev) {
104 				file = fsp->fs_spec;
105 				endfsent();
106 				goto found;
107 			}
108 		}
109 		endfsent();
110 		fprintf(stderr, "%s: mounted on unknown device\n", file);
111 		return;
112 	}
113 found:
114 	fi = open(file, 0);
115 	if (fi < 0) {
116 		perror(file);
117 		return;
118 	}
119 	bread(SBLOCK, (char *)&sblock, SBSIZE);
120 	printf("%-12.12s", file);
121 	totalblks = sblock.fs_dsize;
122 	free = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
123 	    sblock.fs_cstotal.cs_nffree;
124 	used = totalblks - free;
125 	availblks = totalblks * (100 - sblock.fs_minfree) / 100;
126 	avail = availblks > used ? availblks - used : 0;
127 	printf("%8d%8d%8d", totalblks * sblock.fs_fsize / 1024,
128 	    used * sblock.fs_fsize / 1024, avail * sblock.fs_fsize / 1024);
129 	printf("%6.0f%%",
130 	    availblks == 0 ? 0.0 : (double) used / (double) availblks * 100.0);
131 	if (iflag) {
132 		int inodes = sblock.fs_ncg * sblock.fs_ipg;
133 		used = inodes - sblock.fs_cstotal.cs_nifree;
134 		printf("%8ld%8ld%6.0f%% ", used, sblock.fs_cstotal.cs_nifree,
135 		    inodes == 0 ? 0.0 : (double)used / (double)inodes * 100.0);
136 	} else
137 		printf("  ");
138 	printf("  %s\n", mpath(file));
139 	(void) close(fi);
140 }
141 
142 long lseek();
143 
144 bread(bno, buf, cnt)
145 	daddr_t bno;
146 	char *buf;
147 {
148 	int n;
149 	extern errno;
150 
151 	(void) lseek(fi, (long)(bno * DEV_BSIZE), 0);
152 	if ((n=read(fi, buf, cnt)) != cnt) {
153 		printf("\nread error bno = %ld\n", bno);
154 		printf("count = %d; errno = %d\n", n, errno);
155 		exit(0);
156 	}
157 }
158 
159 /*
160  * Given a name like /dev/rrp0h, returns the mounted path, like /usr.
161  */
162 char *mpath(file)
163 	char *file;
164 {
165 	register int i;
166 
167 	if (eq(file, root))
168 		return "/";
169 	for (i=0; i<NFS; i++)
170 		if (eq(file, mtab[i].spec))
171 			return (mtab[i].path);
172 	return "";
173 }
174 
175 eq(f1, f2)
176 	char *f1, *f2;
177 {
178 
179 	if (strncmp(f1, "/dev/", 5) == 0)
180 		f1 += 5;
181 	if (strncmp(f2, "/dev/", 5) == 0)
182 		f2 += 5;
183 	if (!strcmp(f1, f2))
184 		return (1);
185 	if (*f1 == 'r' && !strcmp(f1+1, f2))
186 		return (1);
187 	if (*f2 == 'r' && !strcmp(f1, f2+1))
188 		return (1);
189 	if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
190 		return (1);
191 	return (0);
192 }
193