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