update to new fs
[unix-history] / usr / src / bin / df / df.c
CommitLineData
97dd7000 1#ifndef lint
f4d092e2 2static char *sccsid = "@(#)df.c 4.12 %G%";
97dd7000 3#endif
a98f5f82 4
c7b72342
BJ
5#include <stdio.h>
6#include <fstab.h>
7#include <sys/param.h>
f4d092e2 8#include <sys/fs.h>
8d335f7d 9#include <sys/stat.h>
a98f5f82 10
8d335f7d
BJ
11/*
12 * df
13 */
c7b72342
BJ
14#define NFS 20 /* Max number of filesystems */
15
c7b72342
BJ
16struct {
17 char path[32];
18 char spec[32];
19} mtab[NFS];
a98f5f82
BJ
20char root[32];
21char *mpath();
c7b72342 22
c7b72342
BJ
23int iflag;
24
f4d092e2
KM
25union {
26 struct fs iu_fs;
27 char dummy[SBSIZE];
28} sb;
29#define sblock sb.iu_fs
c7b72342
BJ
30
31int fi;
32daddr_t alloc();
f4d092e2 33char *strcpy();
c7b72342
BJ
34
35main(argc, argv)
a98f5f82
BJ
36 int argc;
37 char **argv;
c7b72342
BJ
38{
39 int i;
c7b72342
BJ
40
41 while (argc >= 1 && argv[1][0]=='-') {
a98f5f82 42 switch (argv[1][1]) {
c7b72342 43
a98f5f82
BJ
44 case 'i':
45 iflag++;
46 break;
c7b72342 47
a98f5f82 48 default:
fa230e15 49 fprintf(stderr, "usage: df [ -i ] [ filsys... ]\n");
a98f5f82 50 exit(0);
c7b72342 51 }
a98f5f82
BJ
52 argc--, argv++;
53 }
54 i = open("/etc/mtab", 0);
55 if (i >= 0) {
f4d092e2
KM
56 (void) read(i, (char *)mtab, sizeof mtab);
57 (void) close(i);
c7b72342 58 }
f4d092e2 59 printf("Filesystem kbytes used avail capacity");
c7b72342 60 if (iflag)
f4d092e2
KM
61 printf(" iused ifree %%iused");
62 printf(" Mounted on\n");
a98f5f82
BJ
63 if (argc <= 1) {
64 struct fstab *fsp;
65
8d335f7d 66 if (setfsent() == 0)
c7b72342 67 perror(FSTAB), exit(1);
a98f5f82 68 while (fsp = getfsent()) {
f4d092e2
KM
69 if (!strcmp(fsp->fs_type, FSTAB_RW) &&
70 !(strcmp(fsp->fs_type, FSTAB_RO)))
f0861f40 71 continue;
c7b72342 72 if (root[0] == 0)
f4d092e2 73 (void) strcpy(root, fsp->fs_spec);
a98f5f82 74 dfree(fsp->fs_spec, 1);
c7b72342 75 }
8d335f7d 76 endfsent();
c7b72342
BJ
77 exit(0);
78 }
a98f5f82
BJ
79 for (i=1; i<argc; i++)
80 dfree(argv[i], 0);
c7b72342
BJ
81}
82
a98f5f82
BJ
83dfree(file, infsent)
84 char *file;
85 int infsent;
c7b72342 86{
f4d092e2 87 long totalblks, availblks, avail, free, used;
a98f5f82
BJ
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 }
f4d092e2
KM
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);
c7b72342 131 if (iflag) {
f4d092e2
KM
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);
c7b72342
BJ
140}
141
f4d092e2
KM
142long lseek();
143
c7b72342 144bread(bno, buf, cnt)
a98f5f82
BJ
145 daddr_t bno;
146 char *buf;
c7b72342
BJ
147{
148 int n;
149 extern errno;
150
f4d092e2 151 (void) lseek(fi, (long)(bno * DEV_BSIZE), 0);
a98f5f82 152 if ((n=read(fi, buf, cnt)) != cnt) {
c7b72342
BJ
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 */
162char *mpath(file)
a98f5f82 163 char *file;
c7b72342
BJ
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))
a98f5f82 171 return (mtab[i].path);
c7b72342
BJ
172 return "";
173}
174
175eq(f1, f2)
a98f5f82 176 char *f1, *f2;
c7b72342 177{
a98f5f82 178
c7b72342
BJ
179 if (strncmp(f1, "/dev/", 5) == 0)
180 f1 += 5;
181 if (strncmp(f2, "/dev/", 5) == 0)
182 f2 += 5;
a98f5f82
BJ
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);
c7b72342 189 if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
a98f5f82
BJ
190 return (1);
191 return (0);
c7b72342 192}