date and time created 82/05/07 15:43:29 by sam
[unix-history] / usr / src / bin / df / df.c
CommitLineData
97dd7000
BJ
1#ifndef lint
2static char *sccsid = "@(#)df.c 4.11 %G%";
3#endif
a98f5f82 4
c7b72342
BJ
5#include <stdio.h>
6#include <fstab.h>
7#include <sys/param.h>
8#include <sys/filsys.h>
9#include <sys/fblk.h>
8d335f7d 10#include <sys/stat.h>
a98f5f82 11
8d335f7d
BJ
12/*
13 * df
14 */
c7b72342
BJ
15#define NFS 20 /* Max number of filesystems */
16
c7b72342
BJ
17struct {
18 char path[32];
19 char spec[32];
20} mtab[NFS];
a98f5f82
BJ
21char root[32];
22char *mpath();
c7b72342
BJ
23daddr_t blkno = 1;
24
c7b72342
BJ
25int iflag;
26
27struct filsys sblock;
28
29int fi;
30daddr_t alloc();
31
32main(argc, argv)
a98f5f82
BJ
33 int argc;
34 char **argv;
c7b72342
BJ
35{
36 int i;
c7b72342
BJ
37
38 while (argc >= 1 && argv[1][0]=='-') {
a98f5f82 39 switch (argv[1][1]) {
c7b72342 40
a98f5f82
BJ
41 case 'i':
42 iflag++;
43 break;
c7b72342 44
a98f5f82 45 default:
fa230e15 46 fprintf(stderr, "usage: df [ -i ] [ filsys... ]\n");
a98f5f82 47 exit(0);
c7b72342 48 }
a98f5f82
BJ
49 argc--, argv++;
50 }
51 i = open("/etc/mtab", 0);
52 if (i >= 0) {
c7b72342
BJ
53 read(i, mtab, sizeof mtab); /* Probably returns short */
54 close(i);
55 }
a98f5f82
BJ
56 printf("Filesystem Mounted on kbytes used free");
57 printf(" %% used");
c7b72342 58 if (iflag)
a98f5f82 59 printf(" iused ifree %%iused");
c7b72342 60 putchar('\n');
a98f5f82
BJ
61 if (argc <= 1) {
62 struct fstab *fsp;
63
8d335f7d 64 if (setfsent() == 0)
c7b72342 65 perror(FSTAB), exit(1);
a98f5f82 66 while (fsp = getfsent()) {
626fe6f7
BJ
67 if (strcmp(fsp->fs_type, FSTAB_RW) &&
68 strcmp(fsp->fs_type, FSTAB_RO))
f0861f40 69 continue;
c7b72342 70 if (root[0] == 0)
8d335f7d 71 strcpy(root, fsp->fs_spec);
a98f5f82 72 dfree(fsp->fs_spec, 1);
c7b72342 73 }
8d335f7d 74 endfsent();
c7b72342
BJ
75 exit(0);
76 }
a98f5f82
BJ
77 for (i=1; i<argc; i++)
78 dfree(argv[i], 0);
c7b72342
BJ
79}
80
a98f5f82
BJ
81dfree(file, infsent)
82 char *file;
83 int infsent;
c7b72342
BJ
84{
85 daddr_t i;
a98f5f82
BJ
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);
8d335f7d
BJ
96 return;
97 }
a98f5f82
BJ
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;
8d335f7d 112 }
a98f5f82 113found:
c7b72342 114 fi = open(file, 0);
a98f5f82
BJ
115 if (fi < 0) {
116 perror(file);
c7b72342
BJ
117 return;
118 }
a98f5f82
BJ
119 bread((long) 1, (char *)&sblock, sizeof (sblock));
120 printf("%-12.12s%-14.14s", file, mp = mpath(file));
c7b72342
BJ
121 blocks = (long) sblock.s_fsize - (long)sblock.s_isize;
122 free = sblock.s_tfree;
123 used = blocks - free;
a98f5f82
BJ
124 printf("%8d%8d%8d", blocks, used, free);
125 printf("%8.0f%%",
df3e00cc 126 blocks == 0 ? 0.0 : (double) used / (double)blocks * 100.0);
c7b72342
BJ
127 if (iflag) {
128 int inodes = (sblock.s_isize - 2) * INOPB;
129 used = inodes - sblock.s_tinode;
a98f5f82 130 printf("%8ld%8ld%8.0f%%", used, sblock.s_tinode,
df3e00cc 131 inodes == 0 ? 0.0 : (double)used/(double)inodes*100.0);
c7b72342
BJ
132 }
133 printf("\n");
134 close(fi);
135}
136
c7b72342 137bread(bno, buf, cnt)
a98f5f82
BJ
138 daddr_t bno;
139 char *buf;
c7b72342
BJ
140{
141 int n;
142 extern errno;
143
144 lseek(fi, bno<<BSHIFT, 0);
a98f5f82 145 if ((n=read(fi, buf, cnt)) != cnt) {
c7b72342
BJ
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 */
155char *mpath(file)
a98f5f82 156 char *file;
c7b72342
BJ
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))
a98f5f82 164 return (mtab[i].path);
c7b72342
BJ
165 return "";
166}
167
168eq(f1, f2)
a98f5f82 169 char *f1, *f2;
c7b72342 170{
a98f5f82 171
c7b72342
BJ
172 if (strncmp(f1, "/dev/", 5) == 0)
173 f1 += 5;
174 if (strncmp(f2, "/dev/", 5) == 0)
175 f2 += 5;
a98f5f82
BJ
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);
c7b72342 182 if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
a98f5f82
BJ
183 return (1);
184 return (0);
c7b72342 185}