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