|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1980, 1990 The Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted provided ! 6: * that: (1) source distributions retain this entire copyright notice and ! 7: * comment, and (2) distributions including binaries display the following ! 8: * acknowledgement: ``This product includes software developed by the ! 9: * University of California, Berkeley and its contributors'' in the ! 10: * documentation or other materials provided with the distribution and in ! 11: * all advertising materials mentioning features or use of this software. ! 12: * Neither the name of the University nor the names of its contributors may ! 13: * be used to endorse or promote products derived from this software without ! 14: * specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: char copyright[] = ! 22: "@(#) Copyright (c) 1980, 1990 The Regents of the University of California.\n\ ! 23: All rights reserved.\n"; ! 24: #endif /* not lint */ ! 25: ! 26: #ifndef lint ! 27: static char sccsid[] = "@(#)df.c 5.21 (Berkeley) 6/24/90"; ! 28: #endif /* not lint */ ! 29: ! 30: /* ! 31: * df ! 32: */ ! 33: #include <sys/param.h> ! 34: #include <sys/stat.h> ! 35: #include <sys/mount.h> ! 36: #include <sys/file.h> ! 37: #include <stdio.h> ! 38: #include <string.h> ! 39: #include <unistd.h> ! 40: ! 41: char *getmntpt(); ! 42: int iflag, kflag, nflag; ! 43: struct ufs_args mdev; ! 44: ! 45: main(argc, argv) ! 46: int argc; ! 47: char **argv; ! 48: { ! 49: extern int errno, optind; ! 50: int err, ch, i; ! 51: long width, maxwidth, mntsize, getmntinfo(); ! 52: char *mntpt, *mktemp(); ! 53: struct stat stbuf; ! 54: struct statfs statfsbuf, *mntbuf; ! 55: ! 56: while ((ch = getopt(argc, argv, "ikn")) != EOF) ! 57: switch(ch) { ! 58: case 'i': ! 59: iflag = 1; ! 60: break; ! 61: case 'k': ! 62: kflag = 1; ! 63: break; ! 64: case 'n': ! 65: nflag = 1; ! 66: break; ! 67: case '?': ! 68: default: ! 69: fprintf(stderr, ! 70: "usage: df [-ikn] [file | file_system ...]\n"); ! 71: exit(1); ! 72: } ! 73: argc -= optind; ! 74: argv += optind; ! 75: ! 76: mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); ! 77: maxwidth = 0; ! 78: for (i = 0; i < mntsize; i++) { ! 79: width = strlen(mntbuf[i].f_mntfromname); ! 80: if (width > maxwidth) ! 81: maxwidth = width; ! 82: } ! 83: if (!*argv) { ! 84: mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT)); ! 85: for (i = 0; i < mntsize; i++) ! 86: prtstat(&mntbuf[i], maxwidth); ! 87: exit(0); ! 88: } ! 89: for (; *argv; argv++) { ! 90: if (stat(*argv, &stbuf) < 0) { ! 91: err = errno; ! 92: if ((mntpt = getmntpt(*argv)) == 0) { ! 93: fprintf(stderr, "df: %s: %s\n", *argv, ! 94: strerror(err)); ! 95: continue; ! 96: } ! 97: } else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) { ! 98: ufs_df(*argv, maxwidth); ! 99: continue; ! 100: } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) { ! 101: if ((mntpt = getmntpt(*argv)) == 0) { ! 102: mntpt = mktemp("/tmp/df.XXXXXX"); ! 103: mdev.fspec = *argv; ! 104: if (mkdir(mntpt) != 0) { ! 105: fprintf(stderr, "df: %s: %s\n", ! 106: mntpt, strerror(errno)); ! 107: continue; ! 108: } ! 109: if (mount(MOUNT_UFS, mntpt, MNT_RDONLY, ! 110: &mdev) != 0) { ! 111: ufs_df(*argv, maxwidth); ! 112: (void)rmdir(mntpt); ! 113: continue; ! 114: } else if (statfs(mntpt, &statfsbuf)) { ! 115: statfsbuf.f_mntonname[0] = '\0'; ! 116: prtstat(&statfsbuf, maxwidth); ! 117: } else ! 118: fprintf(stderr, "df: %s: %s\n", ! 119: *argv, strerror(errno)); ! 120: (void)unmount(mntpt, MNT_NOFORCE); ! 121: (void)rmdir(mntpt); ! 122: continue; ! 123: } ! 124: } else ! 125: mntpt = *argv; ! 126: /* ! 127: * Statfs does not take a `wait' flag, so we cannot ! 128: * implement nflag here. ! 129: */ ! 130: if (statfs(mntpt, &statfsbuf) < 0) { ! 131: fprintf(stderr, ! 132: "df: %s: %s\n", mntpt, strerror(errno)); ! 133: continue; ! 134: } ! 135: if (argc == 1) ! 136: maxwidth = strlen(statfsbuf.f_mntfromname) + 1; ! 137: prtstat(&statfsbuf, maxwidth); ! 138: } ! 139: exit(0); ! 140: } ! 141: ! 142: char * ! 143: getmntpt(name) ! 144: char *name; ! 145: { ! 146: long mntsize, i; ! 147: struct statfs *mntbuf; ! 148: ! 149: mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT)); ! 150: for (i = 0; i < mntsize; i++) { ! 151: if (!strcmp(mntbuf[i].f_mntfromname, name)) ! 152: return (mntbuf[i].f_mntonname); ! 153: } ! 154: return (0); ! 155: } ! 156: ! 157: /* ! 158: * Print out status about a filesystem. ! 159: */ ! 160: prtstat(sfsp, maxwidth) ! 161: register struct statfs *sfsp; ! 162: long maxwidth; ! 163: { ! 164: long used, availblks, inodes; ! 165: static int timesthrough; ! 166: ! 167: if (maxwidth < 11) ! 168: maxwidth = 11; ! 169: if (++timesthrough == 1) { ! 170: printf("%-*.*s%s used avail capacity", ! 171: maxwidth, maxwidth, "Filesystem", ! 172: kflag ? " kbytes" : "512-blks"); ! 173: if (iflag) ! 174: printf(" iused ifree %%iused"); ! 175: printf(" Mounted on\n"); ! 176: } ! 177: printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname); ! 178: used = sfsp->f_blocks - sfsp->f_bfree; ! 179: availblks = sfsp->f_bavail + used; ! 180: printf("%8ld%8ld%8ld", ! 181: sfsp->f_blocks * sfsp->f_fsize / (kflag ? 1024 : 512), ! 182: used * sfsp->f_fsize / (kflag ? 1024 : 512), ! 183: sfsp->f_bavail * sfsp->f_fsize / (kflag ? 1024 : 512)); ! 184: printf("%6.0f%%", ! 185: availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); ! 186: if (iflag) { ! 187: inodes = sfsp->f_files; ! 188: used = inodes - sfsp->f_ffree; ! 189: printf("%8ld%8ld%6.0f%% ", used, sfsp->f_ffree, ! 190: inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0); ! 191: } else ! 192: printf(" "); ! 193: printf(" %s\n", sfsp->f_mntonname); ! 194: } ! 195: ! 196: /* ! 197: * This code constitutes the old df code for extracting ! 198: * information from filesystem superblocks. ! 199: */ ! 200: #include <ufs/fs.h> ! 201: #include <errno.h> ! 202: #include <fstab.h> ! 203: ! 204: union { ! 205: struct fs iu_fs; ! 206: char dummy[SBSIZE]; ! 207: } sb; ! 208: #define sblock sb.iu_fs ! 209: ! 210: int fi; ! 211: char *strcpy(); ! 212: ! 213: ufs_df(file, maxwidth) ! 214: char *file; ! 215: long maxwidth; ! 216: { ! 217: extern int errno; ! 218: struct stat stbuf; ! 219: struct statfs statfsbuf; ! 220: register struct statfs *sfsp; ! 221: struct fstab *fsp; ! 222: char *mntpt; ! 223: static int synced; ! 224: ! 225: if (synced++ == 0) ! 226: sync(); ! 227: ! 228: if ((fi = open(file, O_RDONLY)) < 0) { ! 229: fprintf(stderr, "df: %s: %s\n", file, strerror(errno)); ! 230: return; ! 231: } ! 232: if (bread((long)SBOFF, (char *)&sblock, SBSIZE) == 0) { ! 233: (void) close(fi); ! 234: return; ! 235: } ! 236: sfsp = &statfsbuf; ! 237: sfsp->f_type = MOUNT_UFS; ! 238: sfsp->f_flags = 0; ! 239: sfsp->f_fsize = sblock.fs_fsize; ! 240: sfsp->f_bsize = sblock.fs_bsize; ! 241: sfsp->f_blocks = sblock.fs_dsize; ! 242: sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + ! 243: sblock.fs_cstotal.cs_nffree; ! 244: sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) - ! 245: (sblock.fs_dsize - sfsp->f_bfree); ! 246: if (sfsp->f_bavail < 0) ! 247: sfsp->f_bavail = 0; ! 248: sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg; ! 249: sfsp->f_ffree = sblock.fs_cstotal.cs_nifree; ! 250: sfsp->f_fsid.val[0] = 0; ! 251: sfsp->f_fsid.val[1] = 0; ! 252: if ((mntpt = getmntpt(file)) == 0) ! 253: mntpt = ""; ! 254: bcopy((caddr_t)mntpt, (caddr_t)&sfsp->f_mntonname[0], MNAMELEN); ! 255: bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN); ! 256: prtstat(sfsp, maxwidth); ! 257: (void) close(fi); ! 258: } ! 259: ! 260: long lseek(); ! 261: ! 262: bread(off, buf, cnt) ! 263: long off; ! 264: char *buf; ! 265: { ! 266: int n; ! 267: extern errno; ! 268: ! 269: (void) lseek(fi, off, SEEK_SET); ! 270: if ((n=read(fi, buf, cnt)) != cnt) { ! 271: /* probably a dismounted disk if errno == EIO */ ! 272: if (errno != EIO) { ! 273: printf("\nread error off = %ld\n", off); ! 274: printf("count = %d; errno = %d\n", n, errno); ! 275: } ! 276: return (0); ! 277: } ! 278: return (1); ! 279: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.