#! /etc/LOCAL/bin/perl5 -w # Free, public domain program. use strict; # $Source: /local/undoc/RCS/mostspace,v $ # $Id: mostspace,v 1.4 1997/08/16 03:51:21 rdroot Exp $ # # Given a list of directories, select one which is on a filesystem # with the most free space. my $DF = '/local/bin/df'; $::myname = 'mostspace'; $::myname =~ s|.*/||; $::RCSHEADER = '$Source: /local/undoc/RCS/mostspace,v $' . "\n" . '$Id: mostspace,v 1.4 1997/08/16 03:51:21 rdroot Exp $'; $::usage = "usage: $::myname [-vtx] [-f list] [ directory ... ] (or -h for help)"; # suppresss perl warnings $::debug = $::trace = $::verbose = $::opt_h = undef; $::opt_v = $::opt_t = $::opt_x = undef; if (@ARGV && $ARGV[0] =~ "^-.+" ) { require "getopts.pl"; &Getopts("vtxhf:"); } $::debug = $::opt_x; $::trace = $::opt_t; $::verbose = $::debug || $::trace || $::opt_v; if ($::opt_h) { &givehelp(); exit(0); } my @dirs; if ($::opt_f) { # read filesystems from list open(LIST, $::opt_f) || die "$::myname: error: can't read $::opt_f: $!\n"; while () { chomp; /^#/ && next; /^\s*$/ && next; s/^\s+//; s/\s+$//; push(@dirs, split(' ', $_)); } } else { @dirs = @ARGV; } (@dirs < 1) && die "$::myname: error: no directories\n"; my $space; my $mostspace = -9999; # lower than any possible low space value my $mostdir; my $dir; for $dir (@dirs) { $space = &df($dir); $::debug && print "> $dir has $space M free\n"; if ($space > $mostspace) { $mostspace = $space; $mostdir = $dir; } } print "$mostdir\n"; sub usage_error { my($msg) = @_; if ($msg) { die "$msg\n"; } else { die "$::usage\n"; } } sub givehelp { ## require 'local/page.pl'; ## &page(< $DF $dir\n"; my($device, $kbytes, $used, $avail, $capacity, $mounted); while () { /^Filesystem/ && next; ($device, $kbytes, $used, $avail, $capacity, $mounted) = split(' ', $_); $::debug && print ">> $_"; last; } defined($avail) || return 0; # maybe invalid directory my $megs = int(($avail / 1024) + 0.5); $::debug && print ">> $megs megabytes free\n"; $megs; }