/* * Print in traditional BSD format. */ static void bsdprint(struct statfs *mntbuf, long mntsize, int maxwidth) { int i; char *header; int headerlen; long blocksize; /* Print the header line */ if (hflag) { header = " Size"; headerlen = strlen(header); (void)printf("%-*.*s %s Used Avail Capacity", maxwidth, maxwidth, "Filesystem", header); } else { if (kflag) { blocksize = 1024; header = "1K-blocks"; headerlen = strlen(header); } else header = getbsize(&headerlen, &blocksize); (void)printf("%-*.*s %s Used Avail Capacity", maxwidth, maxwidth, "Filesystem", header); } if (iflag) (void)printf(" iused ifree %%iused"); (void)printf(" Mounted on\n"); for (i = 0; i < mntsize; i++) prtstat(&mntbuf[i], maxwidth, headerlen, blocksize); return; }
void main(void) { struct PrStatus mystat; *((int *)&mystat) = prtstat(0); show(notbusy); show(selected); show(paperout); }
int Lstat(const char *file_name, struct stat *buf) { int result, _errno; Debug2("lstat(%s, %p)", file_name, buf); result = lstat(file_name, buf); _errno = errno; prtstat("lstat", buf, result); errno = _errno; return result; }
int Fstat(int filedes, struct stat *buf) { int result, _errno; Debug2("fstat(%d, %p)", filedes, buf); result = fstat(filedes, buf); _errno = errno; prtstat("fstat", buf, result); errno = _errno; return result; }
int ufs_df(char *file, struct maxwidths *mwp) { struct statfs statfsbuf; struct statvfs statvfsbuf; struct statfs *sfsp; struct statvfs *vsfsp; const char *mntpt; static int synced; if (synced++ == 0) sync(); if ((rfd = open(file, O_RDONLY)) < 0) { warn("%s", file); return (1); } if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) { close(rfd); return (1); } sfsp = &statfsbuf; vsfsp = &statvfsbuf; sfsp->f_type = 1; strcpy(sfsp->f_fstypename, "ufs"); sfsp->f_flags = 0; sfsp->f_bsize = vsfsp->f_bsize = sblock.fs_fsize; sfsp->f_iosize = vsfsp->f_frsize = sblock.fs_bsize; sfsp->f_blocks = vsfsp->f_blocks = sblock.fs_dsize; sfsp->f_bfree = vsfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + sblock.fs_cstotal.cs_nffree; sfsp->f_bavail = vsfsp->f_bavail = freespace(&sblock, sblock.fs_minfree); sfsp->f_files = vsfsp->f_files = sblock.fs_ncg * sblock.fs_ipg; sfsp->f_ffree = vsfsp->f_ffree = sblock.fs_cstotal.cs_nifree; sfsp->f_fsid.val[0] = 0; sfsp->f_fsid.val[1] = 0; if ((mntpt = getmntpt(file)) == NULL) mntpt = ""; memmove(&sfsp->f_mntonname[0], mntpt, (size_t)MNAMELEN); memmove(&sfsp->f_mntfromname[0], file, (size_t)MNAMELEN); prtstat(sfsp, vsfsp, mwp); close(rfd); return (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); }
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); }
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); }
int main(int argc, char *argv[]) { struct stat stbuf; struct statvfs *mntbuf; long mntsize; int ch, i, maxwidth, width; char *mntpt; setprogname(argv[0]); (void)setlocale(LC_ALL, ""); while ((ch = getopt(argc, argv, "aGghiklmnPt:")) != -1) switch (ch) { case 'a': aflag = 1; break; case 'g': hflag = 0; usize = 1024 * 1024 * 1024; break; case 'G': gflag = 1; break; case 'h': hflag = 1; usize = 0; break; case 'i': iflag = 1; break; case 'k': hflag = 0; usize = 1024; break; case 'l': lflag = 1; break; case 'm': hflag = 0; usize = 1024 * 1024; break; case 'n': nflag = 1; break; case 'P': Pflag = 1; break; case 't': if (typelist != NULL) errx(EXIT_FAILURE, "only one -t option may be specified."); maketypelist(optarg); break; case '?': default: usage(); } if (gflag && (Pflag || iflag)) errx(EXIT_FAILURE, "only one of -G and -P or -i may be specified"); if (Pflag && iflag) errx(EXIT_FAILURE, "only one of -P and -i may be specified"); #if 0 /* * The block size cannot be checked until after getbsize() is called. */ if (Pflag && (hflag || (usize != 1024 && usize != 512))) errx(EXIT_FAILURE, "non-standard block size incompatible with -P"); #endif argc -= optind; argv += optind; mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); if (mntsize == 0) err(EXIT_FAILURE, "retrieving information on mounted file systems"); if (*argv == NULL) { mntsize = regetmntinfo(&mntbuf, mntsize); } else { if ((mntbuf = malloc(argc * sizeof(*mntbuf))) == NULL) err(EXIT_FAILURE, "can't allocate statvfs array"); mntsize = 0; for (/*EMPTY*/; *argv != NULL; argv++) { if (stat(*argv, &stbuf) < 0) { if ((mntpt = getmntpt(*argv)) == 0) { warn("%s", *argv); continue; } } else if (S_ISBLK(stbuf.st_mode)) { if ((mntpt = getmntpt(*argv)) == 0) mntpt = *argv; } else mntpt = *argv; /* * Statfs does not take a `wait' flag, so we cannot * implement nflag here. */ if (!statvfs(mntpt, &mntbuf[mntsize])) if (lflag && (mntbuf[mntsize].f_flag & MNT_LOCAL) == 0) warnx("Warning: %s is not a local %s", *argv, "file system"); else if (!selected(mntbuf[mntsize].f_fstypename, sizeof(mntbuf[mntsize].f_fstypename))) warnx("Warning: %s mounted as a %s %s", *argv, mntbuf[mntsize].f_fstypename, "file system"); else ++mntsize; else warn("%s", *argv); } } maxwidth = 0; for (i = 0; i < mntsize; i++) { width = (int)strlen(mntbuf[i].f_mntfromname); if (width > maxwidth) maxwidth = width; } for (i = 0; i < mntsize; i++) prtstat(&mntbuf[i], maxwidth); return 0; }