# 2.11BSD#431 - /usr/src/bin/df.c

##### Functions

    bread             X
    fsbtoblk          X
    getmntpt          X
    main              X
    prtstat           X
    ufs_df            X
    usage             X

##### Variables

    iflag             X
    rfd               X

##### Macros

    sblock            X

       1: /*
       2:  * Copyright (c) 1980, 1990, 1993, 1994
       3:  *	The Regents of the University of California.  All rights reserved.
       4:  * (c) UNIX System Laboratories, Inc.
       5:  * All or some portions of this file are derived from material licensed
       6:  * to the University of California by American Telephone and Telegraph
       7:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
       8:  * the permission of UNIX System Laboratories, Inc.
       9:  *
      10:  * Redistribution and use in source and binary forms, with or without
      11:  * modification, are permitted provided that the following conditions
      12:  * are met:
      13:  * 1. Redistributions of source code must retain the above copyright
      14:  *    notice, this list of conditions and the following disclaimer.
      15:  * 2. Redistributions in binary form must reproduce the above copyright
      16:  *    notice, this list of conditions and the following disclaimer in the
      17:  *    documentation and/or other materials provided with the distribution.
      18:  * 3. All advertising materials mentioning features or use of this software
      19:  *    must display the following acknowledgement:
      20:  *	This product includes software developed by the University of
      21:  *	California, Berkeley and its contributors.
      22:  * 4. Neither the name of the University nor the names of its contributors
      23:  *    may be used to endorse or promote products derived from this software
      24:  *    without specific prior written permission.
      25:  *
      26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
      27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
      30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      36:  * SUCH DAMAGE.
      37:  */
      38:
      39: #if !defined(lint) && defined(DOSCCS)
      40: static char copyright[] =
      41: "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
      42: 	The Regents of the University of California.  All rights reserved.\n";
      43:
      44: static char sccsid[] = "@(#)df.c	8.7.2 (2.11BSD) 1996/1/18";
      45: #endif
      46:
      47: #include <sys/param.h>
      48: #include <sys/stat.h>
      49: #include <sys/mount.h>
      50: #include <sys/file.h>
      51:
      52: #include <errno.h>
      53: #include <fcntl.h>
      54: #include <stdio.h>
      55: #include <stdlib.h>
      56: #include <string.h>
      57:
      58: int  bread();
      59: char    *getmntpt();
      60: void     prtstat();
      61: void     ufs_df();
      62: void     usage();
      63:
      64: int iflag;
      65:
      66: int
      67: main(argc, argv)
      68:     int argc;
      69:     register char *argv[];
      70: {
      71:     struct stat stbuf;
      72:     struct statfs statfsbuf, *mntbuf;
      73:     int mntsize;
      74:     int ch, err, i, maxwidth, width;
      75:     char *mntpt;
      76:
      77:     while ((ch = getopt(argc, argv, "i")) != EOF)
      78:         switch (ch) {
      79:         case 'i':
      80:             iflag = 1;
      81:             break;
      82:         case '?':
      83:         default:
      84:             usage();
      85:         }
      86:     argc -= optind;
      87:     argv += optind;
      88:
      89:     mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
      90:     maxwidth = 0;
      91:     for (i = 0; i < mntsize; i++) {
      92:         width = strlen(mntbuf[i].f_mntfromname);
      93:         if (width > maxwidth)
      94:             maxwidth = width;
      95:     }
      96:
      97:     if (!*argv) {
      98:         for (i = 0; i < mntsize; i++)
      99:             prtstat(&mntbuf[i], maxwidth);
     100:         exit(0);
     101:     }
     102:
     103:     for (; *argv; argv++) {
     104:         if (stat(*argv, &stbuf) < 0) {
     105:             err = errno;
     106:             if ((mntpt = getmntpt(*argv)) == 0) {
     107:                 warn("%s", *argv);
     108:                 continue;
     109:             }
     110:         } else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) {
     111:             ufs_df(*argv, maxwidth);
     112:             continue;
     113:         } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
     114:             if ((mntpt = getmntpt(*argv)) == 0) {
     115:                 ufs_df(*argv, maxwidth);
     116:                 continue;
     117:             }
     118:         } else
     119:             mntpt = *argv;
     120:         /*
     121: 		 * Statfs does not take a `wait' flag, so we cannot
     122: 		 * implement nflag here.
     123: 		 */
     124:         if (statfs(mntpt, &statfsbuf) < 0) {
     125:             warn("%s", mntpt);
     126:             continue;
     127:         }
     128:         if (argc == 1)
     129:             maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
     130:         prtstat(&statfsbuf, maxwidth);
     131:     }
     132:     return (0);
     133: }
     134:
     135: char *
     136: getmntpt(name)
     137:     char *name;
     138: {
     139:     register int i;
     140:     int mntsize;
     141:     struct statfs *mntbuf;
     142:
     143:     mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
     144:     for (i = 0; i < mntsize; i++) {
     145:         if (!strcmp(mntbuf[i].f_mntfromname, name))
     146:             return (mntbuf[i].f_mntonname);
     147:     }
     148:     return (0);
     149: }
     150:
     151: /*
     152:  * Convert statfs returned filesystem size into BLOCKSIZE units.
     153:  * Attempts to avoid overflow for large filesystems.
     154:  */
     155: long
     156: fsbtoblk(num, fsbs, bs)
     157:     long    num;
     158:     register int    fsbs, bs;
     159:     {
     160:     return((fsbs != 0 && fsbs < bs) ?
     161:         num / (bs / fsbs) : (num) * (fsbs / bs));
     162:     }
     163:
     164: /*
     165:  * Print out status about a filesystem.
     166:  */
     167: void
     168: prtstat(sfsp, maxwidth)
     169:     register struct statfs *sfsp;
     170:     register int maxwidth;
     171: {
     172:     static int blocksize;
     173:     static int headerlen, timesthrough;
     174:     static char *header;
     175:     long used, availblks;
     176:     ino_t   inodes, iused;
     177:
     178:     if (maxwidth < 11)
     179:         maxwidth = 11;
     180:     if (++timesthrough == 1) {
     181: /*		header = getbsize(&headerlen, &blocksize); */
     182:         header = "1K-blocks";
     183:         blocksize = 1024;
     184:         headerlen = 9;
     185:
     186:         (void)printf("%-*.*s %s     Used    Avail Capacity",
     187:             maxwidth, maxwidth, "Filesystem", header);
     188:         if (iflag)
     189:             (void)printf(" iused   ifree  %%iused");
     190:         (void)printf("  Mounted on\n");
     191:     }
     192:     (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
     193:     used = sfsp->f_blocks - sfsp->f_bfree;
     194:     availblks = sfsp->f_bavail + used;
     195:     (void)printf(" %*ld %8ld %8ld", headerlen,
     196:         fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
     197:         fsbtoblk(used, sfsp->f_bsize, blocksize),
     198:         fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
     199:     (void)printf(" %5.0f%%",
     200:         availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
     201:     if (iflag) {
     202:         inodes = sfsp->f_files;
     203:         iused = inodes - sfsp->f_ffree;
     204:         (void)printf(" %7u %7u %5.0f%% ", iused, sfsp->f_ffree,
     205:            inodes == 0 ? 100.0 : (double)iused / (double)inodes * 100.0);
     206:     } else
     207:         (void)printf("  ");
     208:     (void)printf("  %s\n", sfsp->f_mntonname);
     209: }
     210:
     211: /*
     212:  * This code constitutes the pre-system call Berkeley df code for extracting
     213:  * information from filesystem superblocks.
     214:  */
     215: #include <sys/fs.h>
     216: #include <fstab.h>
     217:
     218: union {
     219:     struct fs fs;
     220:     char dummy[SBSIZE];
     221: } sb;
     222: #define sblock sb.fs
     223:
     224: int rfd;
     225:
     226: void
     227: ufs_df(file, maxwidth)
     228:     char *file;
     229:     int maxwidth;
     230: {
     231:     struct statfs statfsbuf;
     232:     register struct statfs *sfsp;
     233:     register char *mntpt;
     234:     static int synced;
     235:
     236:     if (synced++ == 0)
     237:         sync();
     238:
     239:     if ((rfd = open(file, O_RDONLY)) < 0) {
     240:         warn("%s", file);
     241:         return;
     242:     }
     243:     if (bread((off_t)SBLOCK * DEV_BSIZE, &sblock, SBSIZE) == 0) {
     244:         (void)close(rfd);
     245:         return;
     246:     }
     247:     sfsp = &statfsbuf;
     248:     sfsp->f_type = MOUNT_UFS;
     249:     sfsp->f_flags = 0;
     250:     sfsp->f_bsize = MAXBSIZE;
     251:     sfsp->f_iosize = MAXBSIZE;
     252:     sfsp->f_blocks = sblock.fs_fsize - sblock.fs_isize;
     253:     sfsp->f_bfree = sblock.fs_tfree;
     254:     sfsp->f_bavail = sblock.fs_tfree;
     255:     if (sfsp->f_bavail < 0)
     256:         sfsp->f_bavail = 0;
     257:     sfsp->f_files =  (sblock.fs_isize - 2) * INOPB;
     258:     sfsp->f_ffree = sblock.fs_tinode;
     259:     sfsp->f_fsid[0] = 0;
     260:     sfsp->f_fsid[1] = 0;
     261:     if ((mntpt = getmntpt(file)) == 0)
     262:         mntpt = "";
     263:     bcopy(mntpt, &sfsp->f_mntonname[0], MNAMELEN);
     264:     bcopy(file, &sfsp->f_mntfromname[0], MNAMELEN);
     265:     prtstat(sfsp, maxwidth);
     266:     (void)close(rfd);
     267: }
     268:
     269: int
     270: bread(off, buf, cnt)
     271:     off_t off;
     272:     void *buf;
     273:     register int cnt;
     274: {
     275:     register int nr;
     276:
     277:     (void)lseek(rfd, off, L_SET);
     278:     if ((nr = read(rfd, buf, cnt)) != cnt) {
     279:         /* Probably a dismounted disk if errno == EIO. */
     280:         if (errno != EIO)
     281:             (void)fprintf(stderr, "\ndf: %ld: %s\n",
     282:                 off, strerror(nr > 0 ? EIO : errno));
     283:         return (0);
     284:     }
     285:     return (1);
     286: }
     287:
     288: void
     289: usage()
     290: {
     291:     (void)fprintf(stderr, "usage: df [-i] [file | file_system ...]\n");
     292:     exit(1);
     293: }

#### Defined functions

bread defined in line 269; used 2 times

- in line 58, 243

fsbtoblk defined in line 155; used 3 times

- in line 196-198(3)

getmntpt defined in line 135; used 4 times

- in line 59, 106, 114, 261

main defined in line 66; never used
prtstat defined in line 167; used 4 times

- in line 60, 99, 130, 265

ufs_df defined in line 226; used 3 times

- in line 61, 111-115(2)

usage defined in line 288; used 2 times

- in line 62, 84

#### Defined variables

copyright defined in line 40; never used
iflag defined in line 64; used 3 times

- in line 80, 188, 201

rfd defined in line 224; used 5 times

- in line 239-244(2), 266, 277-278(2)

sccsid defined in line 44; never used

#### Defined macros

sblock defined in line 222; used 7 times

- in line 243, 252-258(6)

Last modified: 1996-01-19 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1637 |