Commit | Line | Data |
---|---|---|
bcf1365c | 1 | /* |
afddea3e | 2 | * Copyright (c) 1980, 1990 The Regents of the University of California. |
704aadd2 KM |
3 | * All rights reserved. |
4 | * | |
27c71911 | 5 | * %sccs.include.redist.c% |
bcf1365c DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
afddea3e | 10 | "@(#) Copyright (c) 1980, 1990 The Regents of the University of California.\n\ |
bcf1365c | 11 | All rights reserved.\n"; |
7e8aa4e0 | 12 | #endif /* not lint */ |
bcf1365c | 13 | |
97dd7000 | 14 | #ifndef lint |
cc173077 | 15 | static char sccsid[] = "@(#)df.c 5.27 (Berkeley) %G%"; |
7e8aa4e0 | 16 | #endif /* not lint */ |
a98f5f82 | 17 | |
fc2797e7 | 18 | #include <sys/param.h> |
1e568652 | 19 | #include <sys/stat.h> |
704aadd2 | 20 | #include <sys/mount.h> |
d08ff5c3 | 21 | #include <errno.h> |
2f339969 | 22 | #include <fcntl.h> |
fc2797e7 | 23 | #include <stdio.h> |
aae74429 | 24 | #include <stdlib.h> |
6ebcb998 | 25 | #include <string.h> |
fc2797e7 | 26 | #include <unistd.h> |
c7b72342 | 27 | |
d08ff5c3 KB |
28 | int bread __P((long, char *, int)); |
29 | char *getmntpt __P((char *)); | |
30 | void prtstat __P((struct statfs *, long)); | |
31 | void ufs_df __P((char *, long)); | |
32 | void usage __P((void)); | |
33 | ||
93e6cf82 | 34 | int iflag, kflag, nflag; |
afddea3e | 35 | struct ufs_args mdev; |
c7b72342 | 36 | |
aae74429 | 37 | int |
c7b72342 | 38 | main(argc, argv) |
a98f5f82 BJ |
39 | int argc; |
40 | char **argv; | |
c7b72342 | 41 | { |
1e568652 | 42 | struct stat stbuf; |
704aadd2 | 43 | struct statfs statfsbuf, *mntbuf; |
d08ff5c3 KB |
44 | long width, maxwidth, mntsize; |
45 | int err, ch, i; | |
46 | char *mntpt; | |
c7b72342 | 47 | |
afddea3e | 48 | while ((ch = getopt(argc, argv, "ikn")) != EOF) |
dbdc06d4 | 49 | switch(ch) { |
8a7d0cad | 50 | case 'i': |
fc2797e7 KB |
51 | iflag = 1; |
52 | break; | |
53 | case 'k': | |
54 | kflag = 1; | |
8a7d0cad | 55 | break; |
93e6cf82 KM |
56 | case 'n': |
57 | nflag = 1; | |
58 | break; | |
dbdc06d4 | 59 | case '?': |
8a7d0cad | 60 | default: |
d08ff5c3 | 61 | usage(); |
8a7d0cad | 62 | } |
dbdc06d4 KB |
63 | argc -= optind; |
64 | argv += optind; | |
65 | ||
b6f929c6 KM |
66 | mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); |
67 | maxwidth = 0; | |
68 | for (i = 0; i < mntsize; i++) { | |
69 | width = strlen(mntbuf[i].f_mntfromname); | |
70 | if (width > maxwidth) | |
71 | maxwidth = width; | |
72 | } | |
704aadd2 | 73 | if (!*argv) { |
93e6cf82 | 74 | mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT)); |
704aadd2 | 75 | for (i = 0; i < mntsize; i++) |
b6f929c6 | 76 | prtstat(&mntbuf[i], maxwidth); |
704aadd2 KM |
77 | exit(0); |
78 | } | |
79 | for (; *argv; argv++) { | |
1e568652 | 80 | if (stat(*argv, &stbuf) < 0) { |
cf537218 KM |
81 | err = errno; |
82 | if ((mntpt = getmntpt(*argv)) == 0) { | |
fc2797e7 KB |
83 | fprintf(stderr, "df: %s: %s\n", *argv, |
84 | strerror(err)); | |
1e568652 | 85 | continue; |
cf537218 | 86 | } |
afddea3e MK |
87 | } else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) { |
88 | ufs_df(*argv, maxwidth); | |
89 | continue; | |
1e568652 | 90 | } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) { |
819bc686 | 91 | if ((mntpt = getmntpt(*argv)) == 0) { |
23efcbc7 | 92 | mntpt = mktemp(strdup("/tmp/df.XXXXXX")); |
439847aa | 93 | mdev.fspec = *argv; |
aae74429 | 94 | if (mkdir(mntpt, DEFFILEMODE) != 0) { |
afddea3e MK |
95 | fprintf(stderr, "df: %s: %s\n", |
96 | mntpt, strerror(errno)); | |
97 | continue; | |
98 | } | |
99 | if (mount(MOUNT_UFS, mntpt, MNT_RDONLY, | |
100 | &mdev) != 0) { | |
101 | ufs_df(*argv, maxwidth); | |
102 | (void)rmdir(mntpt); | |
103 | continue; | |
104 | } else if (statfs(mntpt, &statfsbuf)) { | |
439847aa | 105 | statfsbuf.f_mntonname[0] = '\0'; |
b6f929c6 | 106 | prtstat(&statfsbuf, maxwidth); |
fc2797e7 KB |
107 | } else |
108 | fprintf(stderr, "df: %s: %s\n", | |
109 | *argv, strerror(errno)); | |
adc92dc2 | 110 | (void)unmount(mntpt, MNT_NOFORCE); |
fc2797e7 | 111 | (void)rmdir(mntpt); |
1e568652 | 112 | continue; |
819bc686 | 113 | } |
1e568652 KM |
114 | } else |
115 | mntpt = *argv; | |
93e6cf82 KM |
116 | /* |
117 | * Statfs does not take a `wait' flag, so we cannot | |
118 | * implement nflag here. | |
119 | */ | |
1e568652 | 120 | if (statfs(mntpt, &statfsbuf) < 0) { |
fc2797e7 KB |
121 | fprintf(stderr, |
122 | "df: %s: %s\n", mntpt, strerror(errno)); | |
704aadd2 KM |
123 | continue; |
124 | } | |
b6f929c6 KM |
125 | if (argc == 1) |
126 | maxwidth = strlen(statfsbuf.f_mntfromname) + 1; | |
127 | prtstat(&statfsbuf, maxwidth); | |
704aadd2 | 128 | } |
aae74429 | 129 | return (0); |
704aadd2 KM |
130 | } |
131 | ||
1e568652 KM |
132 | char * |
133 | getmntpt(name) | |
134 | char *name; | |
135 | { | |
136 | long mntsize, i; | |
137 | struct statfs *mntbuf; | |
138 | ||
93e6cf82 | 139 | mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT)); |
1e568652 KM |
140 | for (i = 0; i < mntsize; i++) { |
141 | if (!strcmp(mntbuf[i].f_mntfromname, name)) | |
142 | return (mntbuf[i].f_mntonname); | |
143 | } | |
144 | return (0); | |
145 | } | |
146 | ||
704aadd2 KM |
147 | /* |
148 | * Print out status about a filesystem. | |
149 | */ | |
aae74429 | 150 | void |
b6f929c6 | 151 | prtstat(sfsp, maxwidth) |
704aadd2 | 152 | register struct statfs *sfsp; |
b6f929c6 | 153 | long maxwidth; |
704aadd2 KM |
154 | { |
155 | long used, availblks, inodes; | |
b6f929c6 | 156 | static int timesthrough; |
704aadd2 | 157 | |
b6f929c6 KM |
158 | if (maxwidth < 11) |
159 | maxwidth = 11; | |
160 | if (++timesthrough == 1) { | |
d08ff5c3 | 161 | printf("%-*.*s%s Used Avail Capacity", |
b6f929c6 | 162 | maxwidth, maxwidth, "Filesystem", |
d08ff5c3 | 163 | kflag ? "1024-blocks" : " 512-blocks"); |
b6f929c6 KM |
164 | if (iflag) |
165 | printf(" iused ifree %%iused"); | |
166 | printf(" Mounted on\n"); | |
167 | } | |
168 | printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname); | |
704aadd2 KM |
169 | used = sfsp->f_blocks - sfsp->f_bfree; |
170 | availblks = sfsp->f_bavail + used; | |
d08ff5c3 | 171 | printf(" %8ld%8ld%8ld", |
cc173077 KM |
172 | sfsp->f_blocks * sfsp->f_bsize / (kflag ? 1024 : 512), |
173 | used * sfsp->f_bsize / (kflag ? 1024 : 512), | |
174 | sfsp->f_bavail * sfsp->f_bsize / (kflag ? 1024 : 512)); | |
704aadd2 KM |
175 | printf("%6.0f%%", |
176 | availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); | |
177 | if (iflag) { | |
178 | inodes = sfsp->f_files; | |
179 | used = inodes - sfsp->f_ffree; | |
180 | printf("%8ld%8ld%6.0f%% ", used, sfsp->f_ffree, | |
181 | inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0); | |
182 | } else | |
183 | printf(" "); | |
184 | printf(" %s\n", sfsp->f_mntonname); | |
185 | } | |
186 | ||
704aadd2 KM |
187 | /* |
188 | * This code constitutes the old df code for extracting | |
189 | * information from filesystem superblocks. | |
190 | */ | |
e70d7509 | 191 | #include <ufs/ffs/fs.h> |
704aadd2 KM |
192 | #include <errno.h> |
193 | #include <fstab.h> | |
704aadd2 | 194 | |
704aadd2 KM |
195 | union { |
196 | struct fs iu_fs; | |
197 | char dummy[SBSIZE]; | |
198 | } sb; | |
199 | #define sblock sb.iu_fs | |
200 | ||
201 | int fi; | |
704aadd2 | 202 | |
aae74429 | 203 | void |
afddea3e | 204 | ufs_df(file, maxwidth) |
a98f5f82 | 205 | char *file; |
b6f929c6 | 206 | long maxwidth; |
c7b72342 | 207 | { |
dbdc06d4 | 208 | extern int errno; |
704aadd2 KM |
209 | struct statfs statfsbuf; |
210 | register struct statfs *sfsp; | |
fc2797e7 | 211 | char *mntpt; |
afddea3e | 212 | static int synced; |
a98f5f82 | 213 | |
afddea3e MK |
214 | if (synced++ == 0) |
215 | sync(); | |
a98f5f82 | 216 | |
fc2797e7 | 217 | if ((fi = open(file, O_RDONLY)) < 0) { |
dbdc06d4 | 218 | fprintf(stderr, "df: %s: %s\n", file, strerror(errno)); |
c7b72342 BJ |
219 | return; |
220 | } | |
6c5c8ac3 | 221 | if (bread((long)SBOFF, (char *)&sblock, SBSIZE) == 0) { |
f5064bf4 RC |
222 | (void) close(fi); |
223 | return; | |
224 | } | |
704aadd2 KM |
225 | sfsp = &statfsbuf; |
226 | sfsp->f_type = MOUNT_UFS; | |
227 | sfsp->f_flags = 0; | |
cc173077 KM |
228 | sfsp->f_bsize = sblock.fs_fsize; |
229 | sfsp->f_iosize = sblock.fs_bsize; | |
704aadd2 KM |
230 | sfsp->f_blocks = sblock.fs_dsize; |
231 | sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + | |
232 | sblock.fs_cstotal.cs_nffree; | |
233 | sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) - | |
234 | (sblock.fs_dsize - sfsp->f_bfree); | |
235 | if (sfsp->f_bavail < 0) | |
236 | sfsp->f_bavail = 0; | |
237 | sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg; | |
238 | sfsp->f_ffree = sblock.fs_cstotal.cs_nifree; | |
239 | sfsp->f_fsid.val[0] = 0; | |
240 | sfsp->f_fsid.val[1] = 0; | |
1e568652 KM |
241 | if ((mntpt = getmntpt(file)) == 0) |
242 | mntpt = ""; | |
243 | bcopy((caddr_t)mntpt, (caddr_t)&sfsp->f_mntonname[0], MNAMELEN); | |
704aadd2 | 244 | bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN); |
b6f929c6 | 245 | prtstat(sfsp, maxwidth); |
f4d092e2 | 246 | (void) close(fi); |
c7b72342 BJ |
247 | } |
248 | ||
aae74429 | 249 | int |
6c5c8ac3 KM |
250 | bread(off, buf, cnt) |
251 | long off; | |
a98f5f82 | 252 | char *buf; |
aae74429 | 253 | int cnt; |
c7b72342 BJ |
254 | { |
255 | int n; | |
c7b72342 | 256 | |
fc2797e7 | 257 | (void) lseek(fi, off, SEEK_SET); |
a98f5f82 | 258 | if ((n=read(fi, buf, cnt)) != cnt) { |
f5064bf4 RC |
259 | /* probably a dismounted disk if errno == EIO */ |
260 | if (errno != EIO) { | |
6c5c8ac3 | 261 | printf("\nread error off = %ld\n", off); |
f5064bf4 RC |
262 | printf("count = %d; errno = %d\n", n, errno); |
263 | } | |
264 | return (0); | |
c7b72342 | 265 | } |
f5064bf4 | 266 | return (1); |
c7b72342 | 267 | } |
d08ff5c3 KB |
268 | |
269 | void | |
270 | usage() | |
271 | { | |
272 | (void)fprintf(stderr, "usage: df [-ikn] [file | file_system ...]\n"); | |
273 | exit(1); | |
274 | } |