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