Commit | Line | Data |
---|---|---|
97dd7000 | 1 | #ifndef lint |
8a7d0cad | 2 | static char *sccsid = "@(#)df.c 4.17 %G%"; |
97dd7000 | 3 | #endif |
a98f5f82 | 4 | |
c7b72342 | 5 | #include <sys/param.h> |
f4d092e2 | 6 | #include <sys/fs.h> |
8d335f7d | 7 | #include <sys/stat.h> |
a98f5f82 | 8 | |
8a7d0cad SL |
9 | #include <stdio.h> |
10 | #include <fstab.h> | |
11 | #include <mtab.h> | |
12 | ||
8d335f7d BJ |
13 | /* |
14 | * df | |
15 | */ | |
8a7d0cad | 16 | struct mtab mtab[NMOUNT]; |
a98f5f82 BJ |
17 | char root[32]; |
18 | char *mpath(); | |
c7b72342 | 19 | |
c7b72342 BJ |
20 | int iflag; |
21 | ||
f4d092e2 KM |
22 | union { |
23 | struct fs iu_fs; | |
24 | char dummy[SBSIZE]; | |
25 | } sb; | |
26 | #define sblock sb.iu_fs | |
c7b72342 BJ |
27 | |
28 | int fi; | |
29 | daddr_t alloc(); | |
f4d092e2 | 30 | char *strcpy(); |
c7b72342 BJ |
31 | |
32 | main(argc, argv) | |
a98f5f82 BJ |
33 | int argc; |
34 | char **argv; | |
c7b72342 BJ |
35 | { |
36 | int i; | |
c7b72342 | 37 | |
64c52cbb | 38 | while (argc > 1 && argv[1][0]=='-') { |
8a7d0cad | 39 | switch (argv[1][1]) { |
c7b72342 | 40 | |
8a7d0cad SL |
41 | case 'i': |
42 | iflag++; | |
43 | break; | |
c7b72342 | 44 | |
8a7d0cad SL |
45 | default: |
46 | fprintf(stderr, "usage: df [ -i ] [ filsys... ]\n"); | |
47 | exit(0); | |
48 | } | |
49 | argc--, argv++; | |
a98f5f82 BJ |
50 | } |
51 | i = open("/etc/mtab", 0); | |
52 | if (i >= 0) { | |
8a7d0cad | 53 | (void) read(i, (char *)mtab, sizeof (mtab)); |
f4d092e2 | 54 | (void) close(i); |
c7b72342 | 55 | } |
dda19894 | 56 | sync(); |
f4d092e2 | 57 | printf("Filesystem kbytes used avail capacity"); |
c7b72342 | 58 | if (iflag) |
f4d092e2 KM |
59 | printf(" iused ifree %%iused"); |
60 | printf(" Mounted on\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()) { |
1f65a82f | 67 | if (strcmp(fsp->fs_type, FSTAB_RW) && |
351ef297 SL |
68 | strcmp(fsp->fs_type, FSTAB_RO) && |
69 | strcmp(fsp->fs_type, FSTAB_RQ)) | |
f0861f40 | 70 | continue; |
c7b72342 | 71 | if (root[0] == 0) |
f4d092e2 | 72 | (void) strcpy(root, fsp->fs_spec); |
a98f5f82 | 73 | dfree(fsp->fs_spec, 1); |
c7b72342 | 74 | } |
8d335f7d | 75 | endfsent(); |
c7b72342 BJ |
76 | exit(0); |
77 | } | |
a98f5f82 BJ |
78 | for (i=1; i<argc; i++) |
79 | dfree(argv[i], 0); | |
c7b72342 BJ |
80 | } |
81 | ||
a98f5f82 BJ |
82 | dfree(file, infsent) |
83 | char *file; | |
84 | int infsent; | |
c7b72342 | 85 | { |
f4d092e2 | 86 | long totalblks, availblks, avail, free, used; |
a98f5f82 BJ |
87 | struct stat stbuf; |
88 | struct fstab *fsp; | |
89 | ||
90 | if (stat(file, &stbuf) == 0 && | |
91 | (stbuf.st_mode&S_IFMT) != S_IFCHR && | |
92 | (stbuf.st_mode&S_IFMT) != S_IFBLK) { | |
93 | if (infsent) { | |
94 | fprintf(stderr, "%s: screwy /etc/fstab entry\n", file); | |
8d335f7d BJ |
95 | return; |
96 | } | |
a98f5f82 BJ |
97 | setfsent(); |
98 | while (fsp = getfsent()) { | |
99 | struct stat stb; | |
100 | ||
101 | if (stat(fsp->fs_spec, &stb) == 0 && | |
102 | stb.st_rdev == stbuf.st_dev) { | |
103 | file = fsp->fs_spec; | |
104 | endfsent(); | |
105 | goto found; | |
106 | } | |
107 | } | |
108 | endfsent(); | |
109 | fprintf(stderr, "%s: mounted on unknown device\n", file); | |
110 | return; | |
8d335f7d | 111 | } |
a98f5f82 | 112 | found: |
c7b72342 | 113 | fi = open(file, 0); |
a98f5f82 BJ |
114 | if (fi < 0) { |
115 | perror(file); | |
c7b72342 BJ |
116 | return; |
117 | } | |
f4d092e2 KM |
118 | bread(SBLOCK, (char *)&sblock, SBSIZE); |
119 | printf("%-12.12s", file); | |
120 | totalblks = sblock.fs_dsize; | |
121 | free = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + | |
122 | sblock.fs_cstotal.cs_nffree; | |
123 | used = totalblks - free; | |
124 | availblks = totalblks * (100 - sblock.fs_minfree) / 100; | |
125 | avail = availblks > used ? availblks - used : 0; | |
126 | printf("%8d%8d%8d", totalblks * sblock.fs_fsize / 1024, | |
127 | used * sblock.fs_fsize / 1024, avail * sblock.fs_fsize / 1024); | |
128 | printf("%6.0f%%", | |
129 | availblks == 0 ? 0.0 : (double) used / (double) availblks * 100.0); | |
c7b72342 | 130 | if (iflag) { |
f4d092e2 KM |
131 | int inodes = sblock.fs_ncg * sblock.fs_ipg; |
132 | used = inodes - sblock.fs_cstotal.cs_nifree; | |
133 | printf("%8ld%8ld%6.0f%% ", used, sblock.fs_cstotal.cs_nifree, | |
134 | inodes == 0 ? 0.0 : (double)used / (double)inodes * 100.0); | |
135 | } else | |
136 | printf(" "); | |
137 | printf(" %s\n", mpath(file)); | |
138 | (void) close(fi); | |
c7b72342 BJ |
139 | } |
140 | ||
f4d092e2 KM |
141 | long lseek(); |
142 | ||
c7b72342 | 143 | bread(bno, buf, cnt) |
a98f5f82 BJ |
144 | daddr_t bno; |
145 | char *buf; | |
c7b72342 BJ |
146 | { |
147 | int n; | |
148 | extern errno; | |
149 | ||
f4d092e2 | 150 | (void) lseek(fi, (long)(bno * DEV_BSIZE), 0); |
a98f5f82 | 151 | if ((n=read(fi, buf, cnt)) != cnt) { |
c7b72342 BJ |
152 | printf("\nread error bno = %ld\n", bno); |
153 | printf("count = %d; errno = %d\n", n, errno); | |
154 | exit(0); | |
155 | } | |
156 | } | |
157 | ||
158 | /* | |
159 | * Given a name like /dev/rrp0h, returns the mounted path, like /usr. | |
160 | */ | |
8a7d0cad SL |
161 | char * |
162 | mpath(file) | |
a98f5f82 | 163 | char *file; |
c7b72342 | 164 | { |
8a7d0cad | 165 | register struct mtab *mp; |
c7b72342 BJ |
166 | |
167 | if (eq(file, root)) | |
8a7d0cad SL |
168 | return ("/"); |
169 | for (mp = mtab; mp < mtab + NMOUNT; mp++) | |
170 | if (eq(file, mp->m_dname)) | |
171 | return (mp->m_path); | |
c7b72342 BJ |
172 | return ""; |
173 | } | |
174 | ||
175 | eq(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 | } |