1 /* $NetBSD: df.c,v 1.65 2004/07/07 01:14:13 enami Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __COPYRIGHT( 40 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ 41 The Regents of the University of California. All rights reserved.\n"); 42 #endif /* not lint */ 43 44 #ifndef lint 45 #if 0 46 static char sccsid[] = "@(#)df.c 8.7 (Berkeley) 4/2/94"; 47 #else 48 __RCSID("$NetBSD: df.c,v 1.65 2004/07/07 01:14:13 enami Exp $"); 49 #endif 50 #endif /* not lint */ 51 52 #include <sys/param.h> 53 #include <sys/stat.h> 54 #include <sys/mount.h> 55 56 #include <err.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <util.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 extern char *strpct(u_long, u_long, u_int); 66 67 int main(int, char *[]); 68 int bread(off_t, void *, int); 69 char *getmntpt(char *); 70 void prtstat(struct statvfs *, int); 71 int selected(const char *); 72 void maketypelist(char *); 73 long regetmntinfo(struct statvfs **, long); 74 void usage(void); 75 void prthumanval(int64_t, char *); 76 void prthuman(struct statvfs *, int64_t, int64_t); 77 78 int aflag, gflag, hflag, iflag, kflag, lflag, mflag, nflag, Pflag; 79 char **typelist = NULL; 80 81 int 82 main(int argc, char *argv[]) 83 { 84 struct stat stbuf; 85 struct statvfs *mntbuf; 86 long mntsize; 87 int ch, i, maxwidth, width; 88 char *mntpt; 89 90 while ((ch = getopt(argc, argv, "aghiklmnPt:")) != -1) 91 switch (ch) { 92 case 'a': 93 aflag = 1; 94 break; 95 case 'g': 96 gflag = 1; 97 break; 98 case 'h': 99 hflag = 1; 100 break; 101 case 'i': 102 iflag = 1; 103 break; 104 case 'k': 105 kflag = 1; 106 break; 107 case 'l': 108 lflag = 1; 109 break; 110 case 'm': 111 mflag = 1; 112 break; 113 case 'n': 114 nflag = 1; 115 break; 116 case 'P': 117 Pflag = 1; 118 break; 119 case 't': 120 if (typelist != NULL) 121 errx(1, "only one -t option may be specified."); 122 maketypelist(optarg); 123 break; 124 case '?': 125 default: 126 usage(); 127 } 128 argc -= optind; 129 argv += optind; 130 131 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 132 if (mntsize == 0) 133 err(1, "retrieving information on mounted file systems"); 134 135 if (*argv == NULL) { 136 mntsize = regetmntinfo(&mntbuf, mntsize); 137 } else { 138 mntbuf = malloc(argc * sizeof(*mntbuf)); 139 mntsize = 0; 140 for (; *argv != NULL; argv++) { 141 if (stat(*argv, &stbuf) < 0) { 142 if ((mntpt = getmntpt(*argv)) == 0) { 143 warn("%s", *argv); 144 continue; 145 } 146 } else if (S_ISBLK(stbuf.st_mode)) { 147 if ((mntpt = getmntpt(*argv)) == 0) 148 mntpt = *argv; 149 } else 150 mntpt = *argv; 151 /* 152 * Statfs does not take a `wait' flag, so we cannot 153 * implement nflag here. 154 */ 155 if (!statvfs(mntpt, &mntbuf[mntsize])) 156 if (lflag && 157 (mntbuf[mntsize].f_flag & MNT_LOCAL) == 0) 158 warnx("Warning: %s is not a local %s", 159 *argv, "file system"); 160 else if 161 (!selected(mntbuf[mntsize].f_fstypename)) 162 warnx("Warning: %s mounted as a %s %s", 163 *argv, 164 mntbuf[mntsize].f_fstypename, 165 "file system"); 166 else 167 ++mntsize; 168 else 169 warn("%s", *argv); 170 } 171 } 172 173 maxwidth = 0; 174 for (i = 0; i < mntsize; i++) { 175 width = strlen(mntbuf[i].f_mntfromname); 176 if (width > maxwidth) 177 maxwidth = width; 178 } 179 for (i = 0; i < mntsize; i++) 180 prtstat(&mntbuf[i], maxwidth); 181 exit(0); 182 /* NOTREACHED */ 183 } 184 185 char * 186 getmntpt(char *name) 187 { 188 long mntsize, i; 189 struct statvfs *mntbuf; 190 191 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 192 for (i = 0; i < mntsize; i++) { 193 if (!strcmp(mntbuf[i].f_mntfromname, name)) 194 return (mntbuf[i].f_mntonname); 195 } 196 return (0); 197 } 198 199 static enum { IN_LIST, NOT_IN_LIST } which; 200 201 int 202 selected(const char *type) 203 { 204 char **av; 205 206 /* If no type specified, it's always selected. */ 207 if (typelist == NULL) 208 return (1); 209 for (av = typelist; *av != NULL; ++av) 210 if (!strncmp(type, *av, MFSNAMELEN)) 211 return (which == IN_LIST ? 1 : 0); 212 return (which == IN_LIST ? 0 : 1); 213 } 214 215 void 216 maketypelist(char *fslist) 217 { 218 int i; 219 char *nextcp, **av; 220 221 if ((fslist == NULL) || (fslist[0] == '\0')) 222 errx(1, "empty type list"); 223 224 /* 225 * XXX 226 * Note: the syntax is "noxxx,yyy" for no xxx's and 227 * no yyy's, not the more intuitive "noyyy,noyyy". 228 */ 229 if (fslist[0] == 'n' && fslist[1] == 'o') { 230 fslist += 2; 231 which = NOT_IN_LIST; 232 } else 233 which = IN_LIST; 234 235 /* Count the number of types. */ 236 for (i = 1, nextcp = fslist; 237 (nextcp = strchr(nextcp, ',')) != NULL; i++) 238 ++nextcp; 239 240 /* Build an array of that many types. */ 241 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL) 242 err(1, "can't allocate type array"); 243 av[0] = fslist; 244 for (i = 1, nextcp = fslist; 245 (nextcp = strchr(nextcp, ',')) != NULL; i++) { 246 *nextcp = '\0'; 247 av[i] = ++nextcp; 248 } 249 /* Terminate the array. */ 250 av[i] = NULL; 251 } 252 253 /* 254 * Make a pass over the filesystem info in ``mntbuf'' filtering out 255 * filesystem types not in ``fsmask'' and possibly re-stating to get 256 * current (not cached) info. Returns the new count of valid statvfs bufs. 257 */ 258 long 259 regetmntinfo(struct statvfs **mntbufp, long mntsize) 260 { 261 int i, j; 262 struct statvfs *mntbuf; 263 264 if (!lflag && typelist == NULL && aflag) 265 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 266 267 mntbuf = *mntbufp; 268 j = 0; 269 for (i = 0; i < mntsize; i++) { 270 if (!aflag && (mntbuf[i].f_flag & MNT_IGNORE) != 0) 271 continue; 272 if (lflag && (mntbuf[i].f_flag & MNT_LOCAL) == 0) 273 continue; 274 if (!selected(mntbuf[i].f_fstypename)) 275 continue; 276 if (nflag) 277 mntbuf[j] = mntbuf[i]; 278 else { 279 struct statvfs layerbuf = mntbuf[i]; 280 (void)statvfs(mntbuf[i].f_mntonname, &mntbuf[j]); 281 /* 282 * If the FS name changed, then new data is for 283 * a different layer and we don't want it. 284 */ 285 if (memcmp(layerbuf.f_mntfromname, 286 mntbuf[j].f_mntfromname, MNAMELEN)) 287 mntbuf[j] = layerbuf; 288 } 289 j++; 290 } 291 return (j); 292 } 293 294 void 295 prthumanval(int64_t bytes, char *pad) 296 { 297 char buf[6]; 298 299 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 300 bytes, "", HN_AUTOSCALE, 301 HN_B | HN_NOSPACE | HN_DECIMAL); 302 303 (void)printf("%s %6s", pad, buf); 304 } 305 306 void 307 prthuman(struct statvfs *sfsp, int64_t used, int64_t bavail) 308 { 309 310 prthumanval(sfsp->f_blocks * sfsp->f_frsize, ""); 311 prthumanval(used * sfsp->f_frsize, " "); 312 prthumanval(bavail * sfsp->f_frsize, " "); 313 } 314 315 /* 316 * Convert statvfs returned filesystem size into BLOCKSIZE units. 317 * Attempts to avoid overflow for large filesystems. 318 */ 319 #define fsbtoblk(num, fsbs, bs) \ 320 (((fsbs) != 0 && (fsbs) < (bs)) ? \ 321 (int64_t)(num) / (int64_t)((bs) / (fsbs)) : \ 322 (int64_t)(num) * ((fsbs) / (bs))) 323 324 /* 325 * Print out status about a filesystem. 326 */ 327 void 328 prtstat(struct statvfs *sfsp, int maxwidth) 329 { 330 static long blocksize; 331 static int headerlen, timesthrough; 332 static char *header; 333 static const char full[] = "100%"; 334 static const char empty[] = " 0%"; 335 int64_t used, availblks, inodes; 336 int64_t bavail; 337 338 if (maxwidth < 11) 339 maxwidth = 11; 340 if (++timesthrough == 1) { 341 if (kflag) { 342 blocksize = 1024; 343 header = Pflag ? "1024-blocks" : "1K-blocks"; 344 headerlen = strlen(header); 345 } else if (mflag) { 346 blocksize = 1024 * 1024; 347 header = Pflag ? "1048576-blocks" : "1M-blocks"; 348 headerlen = strlen(header); 349 } else if (gflag) { 350 blocksize = 1024 * 1024 * 1024; 351 header = Pflag ? "1073741824-blocks" : "1G-blocks"; 352 headerlen = strlen(header); 353 } else if (hflag) { 354 header = " Size"; 355 headerlen = strlen(header); 356 } else 357 header = getbsize(&headerlen, &blocksize); 358 (void)printf("%-*.*s %s Used %9s Capacity", 359 maxwidth, maxwidth, "Filesystem", header, 360 Pflag ? "Available" : "Avail"); 361 if (iflag) 362 (void)printf(" iused ifree %%iused"); 363 (void)printf(" Mounted on\n"); 364 } 365 (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname); 366 used = sfsp->f_blocks - sfsp->f_bfree; 367 bavail = sfsp->f_bfree - sfsp->f_bresvd; 368 availblks = bavail + used; 369 if (hflag) 370 prthuman(sfsp, used, bavail); 371 else 372 (void)printf(" %*" PRId64 " %8" PRId64 " %9" PRId64, headerlen, 373 fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize), 374 fsbtoblk(used, sfsp->f_frsize, blocksize), 375 fsbtoblk(bavail, sfsp->f_frsize, blocksize)); 376 (void)printf("%7s", 377 availblks == 0 ? full : strpct((u_long)used, (u_long)availblks, 0)); 378 if (iflag) { 379 inodes = sfsp->f_files; 380 used = inodes - sfsp->f_ffree; 381 (void)printf(" %8ld %8ld %6s ", 382 (u_long)used, (u_long)sfsp->f_ffree, 383 inodes == 0 ? (used == 0 ? empty : full) : 384 strpct((u_long)used, (u_long)inodes, 0)); 385 } else 386 (void)printf(" "); 387 (void)printf(" %s\n", sfsp->f_mntonname); 388 } 389 390 void 391 usage(void) 392 { 393 394 (void)fprintf(stderr, 395 "usage: %s [-aghiklmnP] [-t type] [file | file_system ...]\n", 396 getprogname()); 397 exit(1); 398 /* NOTREACHED */ 399 } 400