#/usr/local/bin/perl
# Uses syscall to fetch/get HP quotas for users, and allows su to set.
# wjm@feenix.metronet.com

require 'sys/syscall.ph'
require 'sys/quota.ph'
#  local ($Q_QUOTAON)=1;
#  local ($Q_QUOTAOFF)=2;
#  local ($Q_SETQUOTA)=3;
#  local ($Q_GETQUOTA)=4;
#  local ($Q_SETQLIM)=5;
#  local ($Q_SYNC)=6;
@comments = ("","Absolute disk limit for user ","Preferred disk limit for user "
,"Current block count for user ","Maximum allocated files for user ",
"Preferred file limit for user ","Current files in use by user ");
  local ($dir) = "/dev/dsk/6s0\0";
local ($stat,$buf); 
if($< == 0){
print "view [change] quotas for which user? > ";
chop($user = <STDIN>);}
else{
print "Enter your login name > ";
chop($user = <STDIN>);}
@newvals = ();
@uinfo = getpwnam($user);
local ($dqblk)  = pack("LLLLLLLL",0,0,0,0,0,0,0,0); 
$stat=syscall($SYS_quota,$Q_GETQUOTA,$dir,$uinfo[2]+0,$dqblk); 
if($stat != 0){die "You cant do that!!";}
@retvals = unpack("LLLLLLLL",$dqblk);
print("\n[1] $comments[1] $user > $retvals[0]\n");
print("\n[2] $comments[2] $user > $retvals[1]\n");
print("\n[3] $comments[3] $user > $retvals[2]\n");
print("\n[4] $comments[4] $user > $retvals[3]\n");
print("\n[5] $comments[5] $user > $retvals[4]\n");
print("\n[6] $comments[6] $user > $retvals[5]\n");
if($< != 0){print "\nThanks for using Metronet!\n";exit 0;}
@newvals = @retvals;
while(1){
print "\nChange parameter? [1,2,4,5],  <CR> to quit. \"s\" to save new values > ";
chop($num = <STDIN>);
if ($num eq ""){exit 0;}

if($num eq "s"){
$dqblk  = pack("LLLLLLLL",@newvals); 
$stat=syscall($SYS_quota,$Q_SETQLIM,$dir,$uinfo[2]+0,$dqblk); 
$stat=syscall($SYS_quota,$Q_SYNC,$dir,$uinfo[2]+0,$dqblk); 
exit(0);}

if (($num < 1) || ($num > 5)){print "invalid number\n"; exit 0;}
print "Enter new value for $comments[$num]$user ($newvals[$num-1])>";
chop($nval = <STDIN>);

if($nval eq ""){next;}

$newvals[$num-1] = $nval;
print "changing $comments[$num] to $newvals[$num-1] for $user\n";

}
