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