C  Enhanced SHOW USERS program.  
C  Shows image names and batch jobs along with regular information.
C  Requires WORLD privilege.  If installed with /PRIV=WORLD, then must
C  be linked with /NOTRACEBACK.
C  One shot version is $FORT SUPER
C  Continuous version is $FORT/D_LINES SUPER
C
C
	INCLUDE '($SSDEF)/LIST' ! defines SS$_NOMOREPROC
	INCLUDE '($JPIDEF)/LIST' ! defines JPI$... codes.
C
	IMPLICIT INTEGER (A-Z)
	CHARACTER TERM*10, USERNAME*10, PRCNAM*16, IMAGNAME*80, IMAGE1*27
	CHARACTER ESC*1, SCR*4, CURS*3, COMAND*80
	REAL*4 FTME
	LOGICAL CONT, DET
	FTME=3.5
	DATA SCR/'#[2J'/, CURS/'#[f'/
	ESC = CHAR(27)
	SCR(1:1) = ESC
	CURS(1:1) = ESC
C
C  Get command line
	CALL LIB$GET_FOREIGN(COMAND)
	CONT = (INDEX(COMAND,'/C').NE.0)
	DET = (INDEX(COMAND,'/NOD').EQ.0)
C
C  Print heading.
 8888	PRINT 4
    4	FORMAT(/,'  PID  CPU Time  Terminal  ',
     &   'Username  Process Name    Image Name',/)
C
C  This dummy PID acts like a wildcard.
	CONTEXT = -1
C
C  Count processes
	INTER = 0
	BATCH = 0
	NETWORK = 0
	OTHER = 0
C
C  Loop until there are no more processes.
    1	ISTAT=LIB$GETJPI( JPI$_PID, CONTEXT,, PID,, )
	IF(ISTAT.EQ.SS$_NOMOREPROC) GOTO 9999
	IF(ISTAT.EQ.SS$_NOPRIV) GOTO 1
C
C  TERM gets terminal name or process mode.
	ISTAT=LIB$GETJPI( JPI$_MODE, PID,, MODE, TERM, )
	IF(MODE.EQ.JPI$K_INTERACTIVE) THEN
	   ISTAT=LIB$GETJPI( JPI$_TERMINAL, PID,,, TERM, )
	   INTER = INTER + 1
	   IF(TERM.EQ.' ') 
	1	ISTAT=LIB$GETJPI( JPI$_OWNER, PID,,, TERM, )
	ELSE IF(MODE.EQ.JPI$K_BATCH) THEN
	   BATCH = BATCH + 1
	ELSE IF(MODE.EQ.JPI$K_NETWORK) THEN
	   NETWORK = NETWORK + 1
	ELSE
	   OTHER = OTHER + 1
	   IF(.NOT.DET) GOTO 1 ! Supress detatched processes
	END IF
C
	ISTAT=LIB$GETJPI( JPI$_CPUTIM, PID,, CPUTIM,, )
	IMIN = MOD(CPUTIM/6000, 60)
	IHOUR= MOD(CPUTIM/360000, 24)
	IDAY = CPUTIM / (360000*24)
	ISTAT=LIB$GETJPI( JPI$_USERNAME, PID,,, USERNAME, )
	ISTAT=LIB$GETJPI( JPI$_PRCNAM, PID,,, PRCNAM, )
	IMAGNAM = '----'
	ISTAT=LIB$GETJPI( JPI$_IMAGNAME, PID,,, IMAGNAME, )
	CALL LIB$TRIM_FILESPEC(IMAGNAME, IMAGE1,, LIMAG)
C
	PRINT 14, PID, IDAY,IHOUR,IMIN, 
     &    TERM, USERNAME, PRCNAM, IMAGE1(1:LIMAG)
14	FORMAT(X,Z4.4, I4,X,I2,':',I2.2, 2X, 99A)
	GOTO 1
C
C  Finish up by printing total
9999	PRINT 9994, INTER, BATCH, NETWORK, OTHER
9994	FORMAT(/,' Process totals:',I4,' interactive,', I4,' batch,',
     &     I4,' network,',I4,' others.')
C
C  1 shot version is $FORT SUPER, continuous version is $FORT/D_LINES SUPER
	IF(CONT) THEN
           CALL LIB$WAIT(FTME)
	   PRINT 9995, SCR, CURS
9995       FORMAT(' '4A,3A)
           GOTO 8888
	ENDIF
	END
