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