Commit | Line | Data |
---|---|---|
97dd7000 | 1 | #ifndef lint |
f5064bf4 | 2 | static char *sccsid = "@(#)df.c 4.18 %G%"; |
97dd7000 | 3 | #endif |
a98f5f82 | 4 | |
c7b72342 | 5 | #include <sys/param.h> |
f4d092e2 | 6 | #include <sys/fs.h> |
8d335f7d | 7 | #include <sys/stat.h> |
f5064bf4 | 8 | #include <errno.h> |
a98f5f82 | 9 | |
8a7d0cad SL |
10 | #include <stdio.h> |
11 | #include <fstab.h> | |
12 | #include <mtab.h> | |
13 | ||
8d335f7d BJ |
14 | /* |
15 | * df | |
16 | */ | |
8a7d0cad | 17 | struct mtab mtab[NMOUNT]; |
a98f5f82 BJ |
18 | char root[32]; |
19 | char *mpath(); | |
c7b72342 | 20 | |
c7b72342 BJ |
21 | int iflag; |
22 | ||
f4d092e2 KM |
23 | union { |
24 | struct fs iu_fs; | |
25 | char dummy[SBSIZE]; | |
26 | } sb; | |
27 | #define sblock sb.iu_fs | |
c7b72342 BJ |
28 | |
29 | int fi; | |
30 | daddr_t alloc(); | |
f4d092e2 | 31 | char *strcpy(); |
c7b72342 BJ |
32 | |
33 | main(argc, argv) | |
a98f5f82 BJ |
34 | int argc; |
35 | char **argv; | |
c7b72342 BJ |
36 | { |
37 | int i; | |
c7b72342 | 38 | |
64c52cbb | 39 | while (argc > 1 && argv[1][0]=='-') { |
8a7d0cad | 40 | switch (argv[1][1]) { |
c7b72342 | 41 | |
8a7d0cad SL |
42 | case 'i': |
43 | iflag++; | |
44 | break; | |
c7b72342 | 45 | |
8a7d0cad SL |
46 | default: |
47 | fprintf(stderr, "usage: df [ -i ] [ filsys... ]\n"); | |
48 | exit(0); | |
49 | } | |
50 | argc--, argv++; | |
a98f5f82 BJ |
51 | } |
52 | i = open("/etc/mtab", 0); | |
53 | if (i >= 0) { | |
8a7d0cad | 54 | (void) read(i, (char *)mtab, sizeof (mtab)); |
f4d092e2 | 55 | (void) close(i); |
c7b72342 | 56 | } |
dda19894 | 57 | sync(); |
f4d092e2 | 58 | printf("Filesystem kbytes used avail capacity"); |
c7b72342 | 59 | if (iflag) |
f4d092e2 KM |
60 | printf(" iused ifree %%iused"); |
61 | printf(" Mounted on\n"); | |
a98f5f82 BJ |
62 | if (argc <= 1) { |
63 | struct fstab *fsp; | |
64 | ||
8d335f7d | 65 | if (setfsent() == 0) |
c7b72342 | 66 | perror(FSTAB), exit(1); |
a98f5f82 | 67 | while (fsp = getfsent()) { |
1f65a82f | 68 | if (strcmp(fsp->fs_type, FSTAB_RW) && |
351ef297 SL |
69 | strcmp(fsp->fs_type, FSTAB_RO) && |
70 | strcmp(fsp->fs_type, FSTAB_RQ)) | |
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 |
83 | dfree(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 | 113 | found: |
c7b72342 | 114 | fi = open(file, 0); |
a98f5f82 BJ |
115 | if (fi < 0) { |
116 | perror(file); | |
c7b72342 BJ |
117 | return; |
118 | } | |
f5064bf4 RC |
119 | if (bread(SBLOCK, (char *)&sblock, SBSIZE) == 0) { |
120 | (void) close(fi); | |
121 | return; | |
122 | } | |
f4d092e2 KM |
123 | printf("%-12.12s", file); |
124 | totalblks = sblock.fs_dsize; | |
125 | free = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + | |
126 | sblock.fs_cstotal.cs_nffree; | |
127 | used = totalblks - free; | |
128 | availblks = totalblks * (100 - sblock.fs_minfree) / 100; | |
129 | avail = availblks > used ? availblks - used : 0; | |
130 | printf("%8d%8d%8d", totalblks * sblock.fs_fsize / 1024, | |
131 | used * sblock.fs_fsize / 1024, avail * sblock.fs_fsize / 1024); | |
132 | printf("%6.0f%%", | |
133 | availblks == 0 ? 0.0 : (double) used / (double) availblks * 100.0); | |
c7b72342 | 134 | if (iflag) { |
f4d092e2 KM |
135 | int inodes = sblock.fs_ncg * sblock.fs_ipg; |
136 | used = inodes - sblock.fs_cstotal.cs_nifree; | |
137 | printf("%8ld%8ld%6.0f%% ", used, sblock.fs_cstotal.cs_nifree, | |
138 | inodes == 0 ? 0.0 : (double)used / (double)inodes * 100.0); | |
139 | } else | |
140 | printf(" "); | |
141 | printf(" %s\n", mpath(file)); | |
142 | (void) close(fi); | |
c7b72342 BJ |
143 | } |
144 | ||
f4d092e2 KM |
145 | long lseek(); |
146 | ||
c7b72342 | 147 | bread(bno, buf, cnt) |
a98f5f82 BJ |
148 | daddr_t bno; |
149 | char *buf; | |
c7b72342 BJ |
150 | { |
151 | int n; | |
152 | extern errno; | |
153 | ||
f4d092e2 | 154 | (void) lseek(fi, (long)(bno * DEV_BSIZE), 0); |
a98f5f82 | 155 | if ((n=read(fi, buf, cnt)) != cnt) { |
f5064bf4 RC |
156 | /* probably a dismounted disk if errno == EIO */ |
157 | if (errno != EIO) { | |
158 | printf("\nread error bno = %ld\n", bno); | |
159 | printf("count = %d; errno = %d\n", n, errno); | |
160 | } | |
161 | return (0); | |
c7b72342 | 162 | } |
f5064bf4 | 163 | return (1); |
c7b72342 BJ |
164 | } |
165 | ||
166 | /* | |
167 | * Given a name like /dev/rrp0h, returns the mounted path, like /usr. | |
168 | */ | |
8a7d0cad SL |
169 | char * |
170 | mpath(file) | |
a98f5f82 | 171 | char *file; |
c7b72342 | 172 | { |
8a7d0cad | 173 | register struct mtab *mp; |
c7b72342 BJ |
174 | |
175 | if (eq(file, root)) | |
8a7d0cad SL |
176 | return ("/"); |
177 | for (mp = mtab; mp < mtab + NMOUNT; mp++) | |
178 | if (eq(file, mp->m_dname)) | |
179 | return (mp->m_path); | |
c7b72342 BJ |
180 | return ""; |
181 | } | |
182 | ||
183 | eq(f1, f2) | |
a98f5f82 | 184 | char *f1, *f2; |
c7b72342 | 185 | { |
a98f5f82 | 186 | |
c7b72342 BJ |
187 | if (strncmp(f1, "/dev/", 5) == 0) |
188 | f1 += 5; | |
189 | if (strncmp(f2, "/dev/", 5) == 0) | |
190 | f2 += 5; | |
a98f5f82 BJ |
191 | if (!strcmp(f1, f2)) |
192 | return (1); | |
193 | if (*f1 == 'r' && !strcmp(f1+1, f2)) | |
194 | return (1); | |
195 | if (*f2 == 'r' && !strcmp(f1, f2+1)) | |
196 | return (1); | |
c7b72342 | 197 | if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0) |
a98f5f82 BJ |
198 | return (1); |
199 | return (0); | |
c7b72342 | 200 | } |