use new ioctl's
[unix-history] / usr / src / bin / df / df.c
CommitLineData
97dd7000 1#ifndef lint
dda19894 2static char *sccsid = "@(#)df.c 4.13 %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 }
dda19894 59 sync();
f4d092e2 60 printf("Filesystem kbytes used avail capacity");
c7b72342 61 if (iflag)
f4d092e2
KM
62 printf(" iused ifree %%iused");
63 printf(" Mounted on\n");
a98f5f82
BJ
64 if (argc <= 1) {
65 struct fstab *fsp;
66
8d335f7d 67 if (setfsent() == 0)
c7b72342 68 perror(FSTAB), exit(1);
a98f5f82 69 while (fsp = getfsent()) {
f4d092e2
KM
70 if (!strcmp(fsp->fs_type, FSTAB_RW) &&
71 !(strcmp(fsp->fs_type, FSTAB_RO)))
f0861f40 72 continue;
c7b72342 73 if (root[0] == 0)
f4d092e2 74 (void) strcpy(root, fsp->fs_spec);
a98f5f82 75 dfree(fsp->fs_spec, 1);
c7b72342 76 }
8d335f7d 77 endfsent();
c7b72342
BJ
78 exit(0);
79 }
a98f5f82
BJ
80 for (i=1; i<argc; i++)
81 dfree(argv[i], 0);
c7b72342
BJ
82}
83
a98f5f82
BJ
84dfree(file, infsent)
85 char *file;
86 int infsent;
c7b72342 87{
f4d092e2 88 long totalblks, availblks, avail, free, used;
a98f5f82
BJ
89 struct stat stbuf;
90 struct fstab *fsp;
91
92 if (stat(file, &stbuf) == 0 &&
93 (stbuf.st_mode&S_IFMT) != S_IFCHR &&
94 (stbuf.st_mode&S_IFMT) != S_IFBLK) {
95 if (infsent) {
96 fprintf(stderr, "%s: screwy /etc/fstab entry\n", file);
8d335f7d
BJ
97 return;
98 }
a98f5f82
BJ
99 setfsent();
100 while (fsp = getfsent()) {
101 struct stat stb;
102
103 if (stat(fsp->fs_spec, &stb) == 0 &&
104 stb.st_rdev == stbuf.st_dev) {
105 file = fsp->fs_spec;
106 endfsent();
107 goto found;
108 }
109 }
110 endfsent();
111 fprintf(stderr, "%s: mounted on unknown device\n", file);
112 return;
8d335f7d 113 }
a98f5f82 114found:
c7b72342 115 fi = open(file, 0);
a98f5f82
BJ
116 if (fi < 0) {
117 perror(file);
c7b72342
BJ
118 return;
119 }
f4d092e2
KM
120 bread(SBLOCK, (char *)&sblock, SBSIZE);
121 printf("%-12.12s", file);
122 totalblks = sblock.fs_dsize;
123 free = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
124 sblock.fs_cstotal.cs_nffree;
125 used = totalblks - free;
126 availblks = totalblks * (100 - sblock.fs_minfree) / 100;
127 avail = availblks > used ? availblks - used : 0;
128 printf("%8d%8d%8d", totalblks * sblock.fs_fsize / 1024,
129 used * sblock.fs_fsize / 1024, avail * sblock.fs_fsize / 1024);
130 printf("%6.0f%%",
131 availblks == 0 ? 0.0 : (double) used / (double) availblks * 100.0);
c7b72342 132 if (iflag) {
f4d092e2
KM
133 int inodes = sblock.fs_ncg * sblock.fs_ipg;
134 used = inodes - sblock.fs_cstotal.cs_nifree;
135 printf("%8ld%8ld%6.0f%% ", used, sblock.fs_cstotal.cs_nifree,
136 inodes == 0 ? 0.0 : (double)used / (double)inodes * 100.0);
137 } else
138 printf(" ");
139 printf(" %s\n", mpath(file));
140 (void) close(fi);
c7b72342
BJ
141}
142
f4d092e2
KM
143long lseek();
144
c7b72342 145bread(bno, buf, cnt)
a98f5f82
BJ
146 daddr_t bno;
147 char *buf;
c7b72342
BJ
148{
149 int n;
150 extern errno;
151
f4d092e2 152 (void) lseek(fi, (long)(bno * DEV_BSIZE), 0);
a98f5f82 153 if ((n=read(fi, buf, cnt)) != cnt) {
c7b72342
BJ
154 printf("\nread error bno = %ld\n", bno);
155 printf("count = %d; errno = %d\n", n, errno);
156 exit(0);
157 }
158}
159
160/*
161 * Given a name like /dev/rrp0h, returns the mounted path, like /usr.
162 */
163char *mpath(file)
a98f5f82 164 char *file;
c7b72342
BJ
165{
166 register int i;
167
168 if (eq(file, root))
169 return "/";
170 for (i=0; i<NFS; i++)
171 if (eq(file, mtab[i].spec))
a98f5f82 172 return (mtab[i].path);
c7b72342
BJ
173 return "";
174}
175
176eq(f1, f2)
a98f5f82 177 char *f1, *f2;
c7b72342 178{
a98f5f82 179
c7b72342
BJ
180 if (strncmp(f1, "/dev/", 5) == 0)
181 f1 += 5;
182 if (strncmp(f2, "/dev/", 5) == 0)
183 f2 += 5;
a98f5f82
BJ
184 if (!strcmp(f1, f2))
185 return (1);
186 if (*f1 == 'r' && !strcmp(f1+1, f2))
187 return (1);
188 if (*f2 == 'r' && !strcmp(f1, f2+1))
189 return (1);
c7b72342 190 if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
a98f5f82
BJ
191 return (1);
192 return (0);
c7b72342 193}