#!/usr/bin/perl
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Formatted, columnized "ps -gawux" listing.  This will probably  have #
# to be changed for every different version of "ps" that is out there, #
# and there a lot  of  them  lurking  in  the  Unix  landscape.   This #
# program's output format is dependent on how it is called.            #
#                                                                      #
# Copyright (c) 1992 by John Chambers                                  #
#   ...{bu.edu,harvard.edu,trillian.mit.edu}!minya.uucp!jc                #
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$0 =~ s/.*\///;
#
# Check out the name we were called with:
#
if ($0 eq 'PP') {		# PP sorts by pid.
	$S = '|sort +1n';
} elsif ($0 eq 'PPP') {	# PPP sorts by ppid.
	$S = '|sort +2n +1n';
} elsif ($0 eq 'PU') {	# PU sorts by user.
	$S = '|sort +0';
} elsif ($0 eq 'PUP') {	# PUP sorts by user, and then by pid.
	$S = '|sort +0 +1n';
} elsif ($0 eq 'PUPP') {	# PUPP sorts by user, then by ppid.
	$S = '|sort +0 +2n';
} else {
	$S = '';
}

$Month{'Jan'} =  1;
$Month{'Feb'} =  2;
$Month{'Mar'} =  3;
$Month{'Apr'} =  4;
$Month{'May'} =  5;
$Month{'Jun'} =  6;
$Month{'Jul'} =  7;
$Month{'Aug'} =  8;
$Month{'Sep'} =  9;
$Month{'Oct'} = 10;
$Month{'Nov'} = 11;
$Month{'Dec'} = 12;

open(PS,"/bin/ps -gawux $H |") || die "$1: Can't exec /bin/ps\n";
#pen(AL,"$S") || die "$1: Can't exec align\n";
open(AL,"| align l4 r5 r4 r4 r4 r4 l2 c $S") || die "$1: Can't exec align\n";
$, = ' ';
$[ = 1;

for $line (<PS>) {
	$line =~ s/^ *//;
	chop $line;
	if ($line =~ /(16336|2204)/) {
		print "Matched $1\n"; }
	@line = split(/\s+/,$line);
	if ($line[4] =~ /(\d+\.\d)(\d+)/) {
		splice(@line,4,1,$1,$2)
	}
	$n = $#line;
	$UID = shift(@line); 
	$PID = shift(@line); 
	$PPID = shift(@line); 
	$C = shift(@line); 
	$STIME = shift(@line); 
	if ($Month{$STIME}) {
		$Day = shift(@line); 
		$STIME .= '_' . $Day; 
	}
	$TTY = shift(@line); 
	$TIME = shift(@line); 
	print AL $UID,$PID,$PPID,$C,$STIME,$TTY,$TIME,@line,"\n";
}
close AL;
