Esempio n. 1
0
static float
find_disk_space(double *total, double *tot_avail)
{
	struct statfs *mntbuf;
	const char *fstype;
	const char **vfslist;
	char *netvfslist;
	size_t i, mntsize;
	size_t used, availblks;
	const double reported_units = 1e9;
	double toru;
	float pct;
	float most_full = 0.0;

	*total = 0.0;
	*tot_avail = 0.0;

	fstype = "ufs";

	netvfslist = makenetvfslist();
	vfslist = makevfslist(netvfslist);
	free(netvfslist);

	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
	mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
	for (i = 0; i < mntsize; i++) {
		if ((mntbuf[i].f_flags & MNT_IGNORE) == 0) {
			used = mntbuf[i].f_blocks - mntbuf[i].f_bfree;
			availblks = mntbuf[i].f_bavail + used;
			pct = (availblks == 0 ? 100.0 :
			    (double)used / (double)availblks * 100.0);
			if (pct > most_full)
				most_full = pct;

			toru = reported_units/mntbuf[i].f_bsize;
			*total += mntbuf[i].f_blocks / toru;
			*tot_avail += mntbuf[i].f_bavail / toru;
		}
	}

	return most_full;
}
Esempio n. 2
0
int
main(int argc, char *argv[])
{
	struct stat stbuf;
	struct statfs statfsbuf, totalbuf;
	struct maxwidths maxwidths;
	struct statfs *mntbuf;
	const char *fstype;
	char *mntpath, *mntpt;
	const char **vfslist;
	int i, mntsize;
	int ch, rv;

	fstype = "ufs";

	memset(&totalbuf, 0, sizeof(totalbuf));
	totalbuf.f_bsize = DEV_BSIZE;
	strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
	vfslist = NULL;
	while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T")) != -1)
		switch (ch) {
		case 'a':
			aflag = 1;
			break;
		case 'b':
				/* FALLTHROUGH */
		case 'P':
			/*
			 * POSIX specifically discusses the behavior of
			 * both -k and -P. It states that the blocksize should
			 * be set to 1024. Thus, if this occurs, simply break
			 * rather than clobbering the old blocksize.
			 */
			if (kflag)
				break;
			setenv("BLOCKSIZE", "512", 1);
			hflag = 0;
			break;
		case 'c':
			cflag = 1;
			break;
		case 'g':
			setenv("BLOCKSIZE", "1g", 1);
			hflag = 0;
			break;
		case 'H':
			hflag = UNITS_SI;
			break;
		case 'h':
			hflag = UNITS_2;
			break;
		case 'i':
			iflag = 1;
			break;
		case 'k':
			kflag++;
			setenv("BLOCKSIZE", "1024", 1);
			hflag = 0;
			break;
		case 'l':
			if (vfslist != NULL)
				errx(1, "-l and -t are mutually exclusive.");
			vfslist = makevfslist(makenetvfslist());
			lflag = 1;
			break;
		case 'm':
			setenv("BLOCKSIZE", "1m", 1);
			hflag = 0;
			break;
		case 'n':
			nflag = 1;
			break;
		case 't':
			if (lflag)
				errx(1, "-l and -t are mutually exclusive.");
			if (vfslist != NULL)
				errx(1, "only one -t option may be specified");
			fstype = optarg;
			vfslist = makevfslist(optarg);
			break;
		case 'T':
			Tflag = 1;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	rv = 0;
	if (!*argv) {
		/* everything (modulo -t) */
		mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
	} else {
		/* just the filesystems specified on the command line */
		mntbuf = malloc(argc * sizeof(*mntbuf));
		if (mntbuf == 0)
			err(1, "malloc()");
		mntsize = 0;
		/* continued in for loop below */
	}

	/* iterate through specified filesystems */
	for (; *argv; argv++) {
		if (stat(*argv, &stbuf) < 0) {
			if ((mntpt = getmntpt(*argv)) == 0) {
				warn("%s", *argv);
				rv = 1;
				continue;
			}
		} else if (S_ISCHR(stbuf.st_mode)) {
			if ((mntpt = getmntpt(*argv)) == 0) {
				mdev.fspec = *argv;
				mntpath = strdup("/tmp/df.XXXXXX");
				if (mntpath == NULL) {
					warn("strdup failed");
					rv = 1;
					continue;
				}
				mntpt = mkdtemp(mntpath);
				if (mntpt == NULL) {
					warn("mkdtemp(\"%s\") failed", mntpath);
					rv = 1;
					free(mntpath);
					continue;
				}
				if (mount(fstype, mntpt, MNT_RDONLY,
				    &mdev) != 0) {
					warn("%s", *argv);
					rv = 1;
					(void)rmdir(mntpt);
					free(mntpath);
					continue;
				} else if (statfs(mntpt, &statfsbuf) == 0) {
					statfsbuf.f_mntonname[0] = '\0';
					prtstat(&statfsbuf, &maxwidths);
					if (cflag)
						addstat(&totalbuf, &statfsbuf);
				} else {
					warn("%s", *argv);
					rv = 1;
				}
				(void)unmount(mntpt, 0);
				(void)rmdir(mntpt);
				free(mntpath);
				continue;
			}
		} else
			mntpt = *argv;

		/*
		 * Statfs does not take a `wait' flag, so we cannot
		 * implement nflag here.
		 */
		if (statfs(mntpt, &statfsbuf) < 0) {
			warn("%s", mntpt);
			rv = 1;
			continue;
		}

		/*
		 * Check to make sure the arguments we've been given are
		 * satisfied.  Return an error if we have been asked to
		 * list a mount point that does not match the other args
		 * we've been given (-l, -t, etc.).
		 */
		if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
			rv = 1;
			continue;
		}

		/* the user asked for it, so ignore the ignore flag */
		statfsbuf.f_flags &= ~MNT_IGNORE;

		/* add to list */
		mntbuf[mntsize++] = statfsbuf;
	}

	bzero(&maxwidths, sizeof(maxwidths));
	for (i = 0; i < mntsize; i++) {
		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
			update_maxwidths(&maxwidths, &mntbuf[i]);
			if (cflag)
				addstat(&totalbuf, &mntbuf[i]);
		}
	}
	for (i = 0; i < mntsize; i++)
		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
			prtstat(&mntbuf[i], &maxwidths);
	if (cflag)
		prtstat(&totalbuf, &maxwidths);
	return (rv);
}
Esempio n. 3
0
int
main(int argc, char *argv[])
{
	struct stat stbuf;
	struct statfs statfsbuf, *mntbuf;
	struct maxwidths maxwidths;
	char *mntpt, **vfslist;
	long mntsize;
	int ch, i, rv, tflag = 0, kludge_tflag = 0;
	int kflag = 0;
	const char *options = "abgHhiklmnPt:T:";
	if (COMPAT_MODE("bin/df", "unix2003")) {
		/* Unix2003 requires -t be "include total capacity". which df
		  already does, but it conflicts with the old -t so we need to
		  *not* expect a string after -t (we provide -T in both cases
		  to cover the old use of -t) */
		options = "abgHhiklmnPtT:";
		iflag = 1;
	}

	vfslist = NULL;
	while ((ch = getopt(argc, argv, options)) != -1)
		switch (ch) {
		case 'a':
			aflag = 1;
			break;
		case 'b':
				/* FALLTHROUGH */
		case 'P':
			if (COMPAT_MODE("bin/df", "unix2003")) {
				if (!kflag) {
					/* -k overrides -P */
					putenv("BLOCKSIZE=512");
				}
				iflag = 0;
			} else {
				putenv("BLOCKSIZE=512");
			}
			hflag = 0;
			break;
		case 'g':
			putenv("BLOCKSIZE=1g");
			hflag = 0;
			break;
		case 'H':
			hflag = UNITS_SI;
			valp = vals_si;
			break;
		case 'h':
			hflag = UNITS_2;
			valp = vals_base2;
			break;
		case 'i':
			iflag = 1;
			break;
		case 'k':
			if (COMPAT_MODE("bin/df", "unix2003")) {
				putenv("BLOCKSIZE=1024");
			} else {
				putenv("BLOCKSIZE=1k");
			}
			kflag = 1;
			hflag = 0;
			break;
		case 'l':
			if (tflag)
				errx(1, "-l and -T are mutually exclusive.");
			if (vfslist != NULL)
				break;
			vfslist = makevfslist(makenetvfslist());
			break;
		case 'm':
			putenv("BLOCKSIZE=1m");
			hflag = 0;
			break;
		case 'n':
			nflag = 1;
			break;
		case 't':
			/* Unix2003 uses -t for something we do by default */
			if (COMPAT_MODE("bin/df", "unix2003")) {
			    kludge_tflag = 1;
			    break;
			}
		case 'T':
			if (vfslist != NULL) {
				if (tflag)
					errx(1, "only one -%c option may be specified", ch);
				else
					errx(1, "-l and -%c are mutually exclusive.", ch);
			}
			tflag++;
			vfslist = makevfslist(optarg);
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	/* If we are in unix2003 mode, have seen a -t but no -T and the first
	  non switch arg isn't a file, let's pretend they used -T on it.
	  This makes the Lexmark printer installer happy (PR-3918471) */
	if (tflag == 0 && kludge_tflag && *argv && stat(*argv, &stbuf) < 0
	  && errno == ENOENT) {
	    vfslist = makevfslist(*argv++);
	}

	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
	bzero(&maxwidths, sizeof(maxwidths));
	for (i = 0; i < mntsize; i++)
		update_maxwidths(&maxwidths, &mntbuf[i]);

	rv = 0;
	if (!*argv) {
		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
		bzero(&maxwidths, sizeof(maxwidths));
		for (i = 0; i < mntsize; i++)
			update_maxwidths(&maxwidths, &mntbuf[i]);
		for (i = 0; i < mntsize; i++) {
			if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
				prtstat(&mntbuf[i], &maxwidths);
		}
		exit(rv);
	}

	for (; *argv; argv++) {
		if (stat(*argv, &stbuf) < 0) {
			if ((mntpt = getmntpt(*argv)) == 0) {
				warn("%s", *argv);
				rv = 1;
				continue;
			}
		} else if (S_ISCHR(stbuf.st_mode) || S_ISBLK(stbuf.st_mode)) {
			warnx("%s: Raw devices not supported", *argv);
			rv = 1;
			continue;
		} else
			mntpt = *argv;
		/*
		 * Statfs does not take a `wait' flag, so we cannot
		 * implement nflag here.
		 */
		if (statfs(mntpt, &statfsbuf) < 0) {
			warn("%s", mntpt);
			rv = 1;
			continue;
		}
		/* Check to make sure the arguments we've been
		 * given are satisfied.  Return an error if we
		 * have been asked to list a mount point that does
		 * not match the other args we've been given (-l, -t, etc.)
		 */
		if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
			rv++;
			continue;
		}

		if (argc == 1) {
		        bzero(&maxwidths, sizeof(maxwidths));
			update_maxwidths(&maxwidths, &statfsbuf);
		}
		prtstat(&statfsbuf, &maxwidths);
	}
	return (rv);
}
Esempio n. 4
0
int
main(int argc, char **argv)
{
    struct stat stbuf;
    struct statfs statfsbuf, *mntbuf;
    struct statvfs statvfsbuf, *mntvbuf;
    struct maxwidths maxwidths;
    const char *fstype;
    char *mntpath, *mntpt, **vfslist;
    long mntsize;
    int ch, i, rv;

    fstype = "ufs";

    vfslist = NULL;
    while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1)
        switch (ch) {
        case 'a':
            aflag = 1;
            break;
        case 'b':
        /* FALLTHROUGH */
        case 'P':
            if (setenv("BLOCKSIZE", "512", 1) != 0)
                warn("setenv: cannot set BLOCKSIZE=512");
            hflag = 0;
            break;
        case 'g':
            if (setenv("BLOCKSIZE", "1g", 1) != 0)
                warn("setenv: cannot set BLOCKSIZE=1g");
            hflag = 0;
            break;
        case 'H':
            hflag = UNITS_SI;
            break;
        case 'h':
            hflag = UNITS_2;
            break;
        case 'i':
            iflag = 1;
            break;
        case 'k':
            if (setenv("BLOCKSIZE", "1k", 1) != 0)
                warn("setenv: cannot set BLOCKSIZE=1k");
            hflag = 0;
            break;
        case 'l':
            if (vfslist != NULL)
                errx(1, "-l and -t are mutually exclusive.");
            vfslist = makevfslist(makenetvfslist());
            break;
        case 'm':
            if (setenv("BLOCKSIZE", "1m", 1) != 0)
                warn("setenv: cannot set BLOCKSIZE=1m");
            hflag = 0;
            break;
        case 'n':
            nflag = 1;
            break;
        case 't':
            if (vfslist != NULL)
                errx(1, "only one -t option may be specified");
            fstype = optarg;
            vfslist = makevfslist(optarg);
            break;
        case '?':
        default:
            usage();
        }
    argc -= optind;
    argv += optind;

    mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
    bzero(&maxwidths, sizeof(maxwidths));
    for (i = 0; i < mntsize; i++)
        update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);

    rv = 0;
    if (!*argv) {
        mntsize = regetmntinfo(&mntbuf, &mntvbuf, mntsize, vfslist);
        bzero(&maxwidths, sizeof(maxwidths));
        for (i = 0; i < mntsize; i++)
            update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);
        for (i = 0; i < mntsize; i++) {
            if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
                prtstat(&mntbuf[i], &mntvbuf[i], &maxwidths);
        }
        exit(rv);
    }

    for (; *argv; argv++) {
        if (stat(*argv, &stbuf) < 0) {
            if ((mntpt = getmntpt(*argv)) == NULL) {
                warn("%s", *argv);
                rv = 1;
                continue;
            }
        } else if (S_ISCHR(stbuf.st_mode)) {
            if ((mntpt = getmntpt(*argv)) == NULL) {
                mdev.fspec = *argv;
                mntpath = strdup("/tmp/df.XXXXXX");
                if (mntpath == NULL) {
                    warn("strdup failed");
                    rv = 1;
                    continue;
                }
                mntpt = mkdtemp(mntpath);
                if (mntpt == NULL) {
                    warn("mkdtemp(\"%s\") failed", mntpath);
                    rv = 1;
                    free(mntpath);
                    continue;
                }
                if (mount(fstype, mntpt, MNT_RDONLY,
                          &mdev) != 0) {
                    rv = ufs_df(*argv, &maxwidths) || rv;
                    rmdir(mntpt);
                    free(mntpath);
                    continue;
                } else if (statfs(mntpt, &statfsbuf) == 0 &&
                           statvfs(mntpt, &statvfsbuf) == 0) {
                    statfsbuf.f_mntonname[0] = '\0';
                    prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
                } else {
                    warn("%s", *argv);
                    rv = 1;
                }
                unmount(mntpt, 0);
                rmdir(mntpt);
                free(mntpath);
                continue;
            }
        } else
            mntpt = *argv;
        /*
         * Statfs does not take a `wait' flag, so we cannot
         * implement nflag here.
         */
        if (statfs(mntpt, &statfsbuf) < 0) {
            warn("%s", mntpt);
            rv = 1;
            continue;
        }
        if (statvfs(mntpt, &statvfsbuf) < 0) {
            warn("%s", mntpt);
            rv = 1;
            continue;
        }
        /*
         * Check to make sure the arguments we've been given are
         * satisfied. Return an error if we have been asked to
         * list a mount point that does not match the other args
         * we've been given (-l, -t, etc.).
         */
        if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
            rv = 1;
            continue;
        }

        if (argc == 1) {
            bzero(&maxwidths, sizeof(maxwidths));
            update_maxwidths(&maxwidths, &statfsbuf, &statvfsbuf);
        }
        prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
    }
    return (rv);
}