int process_df_disks() { //struct stat statbuf; struct statfs Totalbuf; struct maxWidths maxWidths; struct statfs *mountbuf; const char *fstyp; //char *mountpath, *mountpoint; const char **VFSlist; size_t i, mountsize; int RetVal; /* fstyp is name from System V UNIX */ fstyp = "ufs"; memset(&Totalbuf, 0, sizeof(Totalbuf)); Totalbuf.f_bsize = DEV_BSIZE; strncpy(Totalbuf.f_mntfromname, "total", MNAMELEN); VFSlist = NULL; mountsize = getmntinfo(&mountbuf, MNT_NOWAIT); bzero(&maxWidths, sizeof(maxWidths)); for(i = 0; i <mountsize ; i++) /* Calls a function */ update_maxwidths(&maxWidths, &mountbuf[i]); RetVal=0; mountsize = regetmntinfo(&mountbuf, mountsize, VFSlist); bzero(&maxWidths, sizeof(maxWidths)); for (i = 0; i <mountsize; i++) { update_maxwidths(&maxWidths, &mountbuf[i]); } for(i = 0; i < mountsize; i++) if((mountbuf[i].f_flags & MNT_IGNORE) == 0) printstat(&mountbuf[i], &maxWidths); /* This had getch on the End */ /*******************************************************************************/ return(RetVal); }
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; }
int main(int argc, char *argv[]) { struct stat stbuf; struct statfs *mntbuf; long mntsize; int ch, i; int width, maxwidth; char *mntpt; if (pledge("stdio rpath", NULL) == -1) err(1, "pledge"); while ((ch = getopt(argc, argv, "hiklnPt:")) != -1) switch (ch) { case 'h': hflag = 1; kflag = 0; break; case 'i': iflag = 1; break; case 'k': kflag = 1; hflag = 0; break; case 'l': lflag = 1; break; case 'n': nflag = 1; break; case 'P': Pflag = 1; break; case 't': if (typelist != NULL) errx(1, "only one -t option may be specified."); maketypelist(optarg); break; default: usage(); } argc -= optind; argv += optind; if ((iflag || hflag) && Pflag) { warnx("-h and -i are incompatible with -P"); usage(); } mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); if (mntsize == 0) err(1, "retrieving information on mounted file systems"); if (!*argv) { mntsize = regetmntinfo(&mntbuf, mntsize); } else { mntbuf = calloc(argc, sizeof(struct statfs)); if (mntbuf == NULL) err(1, NULL); mntsize = 0; for (; *argv; argv++) { if (stat(*argv, &stbuf) < 0) { if ((mntpt = getmntpt(*argv)) == 0) { warn("%s", *argv); continue; } } else if (S_ISCHR(stbuf.st_mode) || S_ISBLK(stbuf.st_mode)) { if (!raw_df(*argv, &mntbuf[mntsize])) ++mntsize; continue; } else mntpt = *argv; /* * Statfs does not take a `wait' flag, so we cannot * implement nflag here. */ if (!statfs(mntpt, &mntbuf[mntsize])) if (lflag && (mntbuf[mntsize].f_flags & MNT_LOCAL) == 0) warnx("%s is not a local file system", *argv); else if (!selected(mntbuf[mntsize].f_fstypename)) warnx("%s mounted as a %s file system", *argv, mntbuf[mntsize].f_fstypename); else ++mntsize; else warn("%s", *argv); } } if (mntsize) { maxwidth = 11; for (i = 0; i < mntsize; i++) { width = strlen(mntbuf[i].f_mntfromname); if (width > maxwidth) maxwidth = width; } if (Pflag) posixprint(mntbuf, mntsize, maxwidth); else bsdprint(mntbuf, mntsize, maxwidth); } return (mntsize ? 0 : 1); }
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; }