xref: /csrg-svn/old/sysline/sysline.c (revision 43861)
122534Smckusick /*
222534Smckusick  * Copyright (c) 1980 Regents of the University of California.
334586Sbostic  * All rights reserved.
434586Sbostic  *
542769Sbostic  * %sccs.include.redist.c%
622534Smckusick  */
722534Smckusick 
822534Smckusick #ifndef lint
922534Smckusick char copyright[] =
1022534Smckusick "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1122534Smckusick  All rights reserved.\n";
1234586Sbostic #endif /* not lint */
1322534Smckusick 
1422534Smckusick #ifndef lint
15*43861Sbostic static char sccsid[] = "@(#)sysline.c	5.16 (Berkeley) 06/24/90";
1634586Sbostic #endif /* not lint */
1722534Smckusick 
1822534Smckusick /*
1922534Smckusick  * sysline - system status display on 25th line of terminal
2022534Smckusick  * j.k.foderaro
2122534Smckusick  *
2222534Smckusick  * Prints a variety of information on the special status line of terminals
2322534Smckusick  * that have a status display capability.  Cursor motions, status commands,
2422534Smckusick  * etc. are gleamed from /etc/termcap.
2522534Smckusick  * By default, all information is printed, and flags are given on the command
2622534Smckusick  * line to disable the printing of information.  The information and
2722534Smckusick  * disabling flags are:
2822534Smckusick  *
2922534Smckusick  *  flag	what
3022534Smckusick  *  -----	----
3122534Smckusick  *		time of day
3222534Smckusick  *		load average and change in load average in the last 5 mins
3322534Smckusick  *		number of user logged on
3422534Smckusick  *   -p		# of processes the users owns which are runnable and the
3522534Smckusick  *		  number which are suspended.  Processes whose parent is 1
3622534Smckusick  *		  are not counted.
3722534Smckusick  *   -l		users who've logged on and off.
3822534Smckusick  *   -m		summarize new mail which has arrived
3922534Smckusick  *
4022534Smckusick  *  <other flags>
4122534Smckusick  *   -r		use non reverse video
4222534Smckusick  *   -c		turn off 25th line for 5 seconds before redisplaying.
4322534Smckusick  *   -b		beep once one the half hour, twice on the hour
4422534Smckusick  *   +N		refresh display every N seconds.
4522534Smckusick  *   -i		print pid first thing
4622534Smckusick  *   -e		do simple print designed for an emacs buffer line
4722534Smckusick  *   -w		do the right things for a window
4822534Smckusick  *   -h		print hostname between time and load average
4922534Smckusick  *   -D		print day/date before time of day
5022534Smckusick  *   -d		debug mode - print status line data in human readable format
5122534Smckusick  *   -q		quiet mode - don't output diagnostic messages
5222534Smckusick  *   -s		print Short (left-justified) line if escapes not allowed
5322534Smckusick  *   -j		Print left Justified line regardless
5422534Smckusick  */
5522534Smckusick 
5622534Smckusick #define BSD4_2			/* for 4.2 BSD */
5722534Smckusick #define WHO			/* turn this on always */
5822534Smckusick #define HOSTNAME		/* 4.1a or greater, with hostname() */
5922534Smckusick #define RWHO			/* 4.1a or greater, with rwho */
6022534Smckusick #define VMUNIX			/* turn this on if you are running on vmunix */
6122534Smckusick #define NEW_BOOTTIME		/* 4.1c or greater */
6222534Smckusick 
6322534Smckusick #define NETPREFIX "ucb"
6422534Smckusick #define DEFDELAY 60		/* update status once per minute */
6522534Smckusick /*
6622534Smckusick  * if MAXLOAD is defined, then if the load average exceeded MAXLOAD
6722534Smckusick  * then the process table will not be scanned and the log in/out data
6822534Smckusick  * will not be checked.   The purpose of this is to reduced the load
6922534Smckusick  * on the system when it is loaded.
7022534Smckusick  */
7122534Smckusick #define MAXLOAD 6.0
7222534Smckusick 
7322534Smckusick #include <stdio.h>
7422534Smckusick #include <sys/param.h>
7538186Smckusick #include <sys/types.h>
7622534Smckusick #include <sys/signal.h>
7722534Smckusick #include <utmp.h>
7822534Smckusick #include <ctype.h>
7922534Smckusick #ifndef BSD4_2
8022534Smckusick #include <unctrl.h>
8122534Smckusick #endif
8222534Smckusick #include <sys/time.h>
8322534Smckusick #include <sys/stat.h>
8422534Smckusick #ifdef VMUNIX
8522534Smckusick #include <nlist.h>
8622534Smckusick #include <sys/vtimes.h>
8722534Smckusick #include <sys/proc.h>
8822534Smckusick #endif
8922534Smckusick #ifdef pdp11
9022534Smckusick #include <a.out.h>
9122534Smckusick #include <sys/proc.h>
9222534Smckusick #endif
9322534Smckusick #include <curses.h>
9422534Smckusick #undef nl
9522534Smckusick #ifdef TERMINFO
9622534Smckusick #include <term.h>
9722534Smckusick #endif TERMINFO
9822534Smckusick 
9922534Smckusick #ifdef RWHO
10024502Skarels #include <protocols/rwhod.h>
10124502Skarels 
10222534Smckusick #define	DOWN_THRESHOLD	(11 * 60)
10322534Smckusick 
10422534Smckusick struct remotehost {
10522534Smckusick 	char *rh_host;
10622534Smckusick 	int rh_file;
10722534Smckusick } remotehost[10];
10822534Smckusick int nremotes = 0;
10922534Smckusick #endif RWHO
11022534Smckusick 
11137905Sbostic #include "pathnames.h"
11237905Sbostic 
11322534Smckusick struct nlist nl[] = {
11422534Smckusick #ifdef NEW_BOOTTIME
11522534Smckusick 	{ "_boottime" },	/* After 4.1a the label changed to "boottime" */
11622534Smckusick #else
11722534Smckusick 	{ "_bootime" },		/* Under 4.1a and earlier it is "bootime" */
11822534Smckusick #endif
11922534Smckusick #define	NL_BOOT 0
12022534Smckusick 	{ "_proc" },
12122534Smckusick #define NL_PROC 1
12222534Smckusick #ifdef VMUNIX
12322534Smckusick 	{ "_nproc" },
12438186Smckusick #define NL_NPROC 2
12522534Smckusick #endif
12622534Smckusick 	0
12722534Smckusick };
12822534Smckusick 
12922534Smckusick 	/* stuff for the kernel */
13037905Sbostic int kmem;			/* file descriptor for _PATH_KMEM */
13122534Smckusick struct proc *proc, *procNPROC;
13222534Smckusick int nproc;
13322534Smckusick int procadr;
13422534Smckusick double avenrun[3];		/* used for storing load averages */
13522534Smckusick 
13622534Smckusick /*
13722534Smckusick  * In order to determine how many people are logged on and who has
13822534Smckusick  * logged in or out, we read in the /etc/utmp file. We also keep track of
13922534Smckusick  * the previous utmp file.
14022534Smckusick  */
14123748Sedward int ut = -1;			/* the file descriptor */
14222534Smckusick struct utmp *new, *old;
14322534Smckusick char *status;			/* per tty status bits, see below */
14422534Smckusick int nentries;			/* number of utmp entries */
14522534Smckusick 	/* string lengths for printing */
14622534Smckusick #define LINESIZE (sizeof old->ut_line)
14722534Smckusick #define NAMESIZE (sizeof old->ut_name)
14822534Smckusick /*
14922534Smckusick  * Status codes to say what has happened to a particular entry in utmp.
15022534Smckusick  * NOCH means no change, ON means new person logged on,
15122534Smckusick  * OFF means person logged off.
15222534Smckusick  */
15322534Smckusick #define NOCH	0
15422534Smckusick #define ON	0x1
15522534Smckusick #define OFF	0x2
15622534Smckusick 
15722534Smckusick #ifdef WHO
15822534Smckusick char whofilename[100];
15922534Smckusick char whofilename2[100];
16022534Smckusick #endif
16122534Smckusick 
16222534Smckusick #ifdef HOSTNAME
16324502Skarels char hostname[MAXHOSTNAMELEN+1];	/* one more for null termination */
16422534Smckusick #endif
16522534Smckusick 
16622534Smckusick char lockfilename[100];		/* if exists, will prevent us from running */
16722534Smckusick 
16822534Smckusick 	/* flags which determine which info is printed */
16922534Smckusick int mailcheck = 1;	/* m - do biff like checking of mail */
17022534Smckusick int proccheck = 1;	/* p - give information on processes */
17122534Smckusick int logcheck = 1; 	/* l - tell who logs in and out */
17222534Smckusick int hostprint = 0;	/* h - print out hostname */
17322534Smckusick int dateprint = 0;	/* h - print out day/date */
17422534Smckusick int quiet = 0;		/* q - hush diagnostic messages */
17522534Smckusick 
17622534Smckusick 	/* flags which determine how things are printed */
17722534Smckusick int clr_bet_ref = 0;	/* c - clear line between refeshes */
17822534Smckusick int reverse = 1;	/* r - use reverse video */
17922534Smckusick int shortline = 0;	/* s - short (left-justified) if escapes not allowed */
18022534Smckusick int leftline = 0;	/* j - left-justified even if escapes allowed */
18122534Smckusick 
18222534Smckusick 	/* flags which have terminal do random things	*/
18322534Smckusick int beep = 0;		/* b - beep every half hour and twice every hour */
18422534Smckusick int printid = 0;	/* i - print pid of this process at startup */
18522534Smckusick int synch = 1;		/* synchronize with clock */
18622534Smckusick 
18722534Smckusick 	/* select output device (status display or straight output) */
18822534Smckusick int emacs = 0;		/* e - assume status display */
18922534Smckusick int window = 0;		/* w - window mode */
19022534Smckusick int dbug = 0;		/* d - debug */
19122534Smckusick 
19222534Smckusick /*
19322534Smckusick  * used to turn off reverse video every REVOFF times
19422534Smckusick  * in an attempt to not wear out the phospher.
19522534Smckusick  */
19622534Smckusick #define REVOFF 5
19722534Smckusick int revtime = 1;
19822534Smckusick 
19922534Smckusick 	/* used by mail checker */
20022534Smckusick off_t mailsize = 0;
20122534Smckusick off_t linebeg = 0;		/* place where we last left off reading */
20222534Smckusick 
20322534Smckusick 	/* things used by the string routines */
20422534Smckusick int chars;			/* number of printable characters */
20522534Smckusick char *sp;
20622534Smckusick char strarr[512];		/* big enough now? */
20722534Smckusick 	/* flags to stringdump() */
20822534Smckusick char sawmail;			/* remember mail was seen to print bells */
20922534Smckusick char mustclear;			/* status line messed up */
21022534Smckusick 
21122534Smckusick 	/* strings which control status line display */
21222534Smckusick #ifdef TERMINFO
21322534Smckusick char	*rev_out, *rev_end, *arrows;
21422534Smckusick char	*tparm();
21522534Smckusick #else
21622534Smckusick char	to_status_line[64];
21722534Smckusick char	from_status_line[64];
21822534Smckusick char	dis_status_line[64];
21922534Smckusick char	clr_eol[64];
22022534Smckusick char	rev_out[20], rev_end[20];
22122534Smckusick char	*arrows, *bell = "\007";
22222534Smckusick int	eslok;	/* escapes on status line okay (reverse, cursor addressing) */
22334586Sbostic int	hasws = 0;	/* is "ws" explicitly defined? */
22422534Smckusick int	columns;
22522534Smckusick #define tparm(cap, parm) tgoto((cap), 0, (parm))
22622534Smckusick char	*tgoto();
22722534Smckusick #endif
22822534Smckusick 
22922534Smckusick 	/* to deal with window size changes */
23022534Smckusick #ifdef SIGWINCH
23122534Smckusick int sigwinch();
23222534Smckusick char winchanged;	/* window size has changed since last update */
23322534Smckusick #endif
23422534Smckusick 
23522534Smckusick 	/* random globals */
23622534Smckusick char *username;
23722534Smckusick char *ourtty;			/* keep track of what tty we're on */
23822534Smckusick struct stat stbuf, mstbuf;	/* mstbuf for mail check only */
23922534Smckusick unsigned delay = DEFDELAY;
24032535Sbostic uid_t uid;
24122534Smckusick double loadavg = 0.0;		/* current load average */
24222534Smckusick int users = 0;
24322534Smckusick 
24422534Smckusick char *getenv();
24522534Smckusick char *ttyname();
24622534Smckusick char *strcpy1();
24722534Smckusick char *sysrup();
24822534Smckusick char *calloc();
24922534Smckusick char *malloc();
25022534Smckusick int outc();
25122534Smckusick int erroutc();
25222534Smckusick 
25322534Smckusick main(argc,argv)
25422534Smckusick 	register char **argv;
25522534Smckusick {
25622534Smckusick 	int clearbotl();
25722534Smckusick 	register char *cp;
25822534Smckusick 	char *home;
25924747Sbloom 	extern char *index();
26022534Smckusick 
26122534Smckusick #ifdef HOSTNAME
26222534Smckusick 	gethostname(hostname, sizeof hostname - 1);
26324747Sbloom 	if ((cp = index(hostname, '.')) != NULL)
26424747Sbloom 		*cp = '\0';
26522534Smckusick #endif
26622534Smckusick 
26722534Smckusick 	for (argv++; *argv != 0; argv++)
26822534Smckusick 		switch (**argv) {
26922534Smckusick 		case '-':
27022534Smckusick 			for (cp = *argv + 1; *cp; cp++) {
27122534Smckusick 				switch(*cp) {
27222534Smckusick 				case 'r' :	/* turn off reverse video */
27322534Smckusick 					reverse = 0;
27422534Smckusick 					break;
27522534Smckusick 				case 'c':
27622534Smckusick 					clr_bet_ref = 1;
27722534Smckusick 					break;
27822534Smckusick 				case 'h':
27922534Smckusick 					hostprint = 1;
28022534Smckusick 					break;
28122534Smckusick 				case 'D':
28222534Smckusick 					dateprint = 1;
28322534Smckusick 					break;
28422534Smckusick #ifdef RWHO
28522534Smckusick 				case 'H':
28622534Smckusick 					if (argv[1] == 0)
28722534Smckusick 						break;
28822534Smckusick 					argv++;
28922534Smckusick 					if (strcmp(hostname, *argv) &&
29022534Smckusick 					    strcmp(&hostname[sizeof NETPREFIX - 1], *argv))
29122534Smckusick 						remotehost[nremotes++].rh_host = *argv;
29222534Smckusick 					break;
29322534Smckusick #endif RWHO
29422534Smckusick 				case 'm':
29522534Smckusick 					mailcheck = 0;
29622534Smckusick 					break;
29722534Smckusick 				case 'p':
29822534Smckusick 					proccheck = 0;
29922534Smckusick 					break;
30022534Smckusick 				case 'l':
30122534Smckusick 					logcheck = 0;
30222534Smckusick 					break;
30322534Smckusick 				case 'b':
30422534Smckusick 					beep = 1;
30522534Smckusick 					break;
30622534Smckusick 				case 'i':
30722534Smckusick 					printid = 1;
30822534Smckusick 					break;
30922534Smckusick 				case 'w':
31022534Smckusick 					window = 1;
31122534Smckusick 					break;
31222534Smckusick 				case 'e':
31322534Smckusick 					emacs = 1;
31422534Smckusick 					break;
31522534Smckusick 				case 'd':
31622534Smckusick 					dbug = 1;
31722534Smckusick 					break;
31822534Smckusick 				case 'q':
31922534Smckusick 					quiet = 1;
32022534Smckusick 					break;
32122534Smckusick 				case 's':
32222534Smckusick 					shortline = 1;
32322534Smckusick 					break;
32422534Smckusick 				case 'j':
32522534Smckusick 					leftline = 1;
32622534Smckusick 					break;
32722534Smckusick 				default:
32822534Smckusick 					fprintf(stderr,
32922534Smckusick 						"sysline: bad flag: %c\n", *cp);
33022534Smckusick 				}
33122534Smckusick 			}
33222534Smckusick 			break;
33322534Smckusick 		case '+':
33422534Smckusick 			delay = atoi(*argv + 1);
33522534Smckusick 			if (delay < 10)
33622534Smckusick 				delay = 10;
33722534Smckusick 			else if (delay > 500)
33822534Smckusick 				delay = 500;
33922534Smckusick 			synch = 0;	/* no more sync */
34022534Smckusick 			break;
34122534Smckusick 		default:
34222534Smckusick 			fprintf(stderr, "sysline: illegal argument %s\n",
34322534Smckusick 				argv[0]);
34422534Smckusick 		}
34522534Smckusick 	if (emacs) {
34622534Smckusick 		reverse = 0;
34722534Smckusick 		columns = 79;
34822534Smckusick 	} else	/* if not to emacs window, initialize terminal dependent info */
34922534Smckusick 		initterm();
35022534Smckusick #ifdef SIGWINCH
35122534Smckusick 	/*
35222534Smckusick 	 * When the window size changes and we are the foreground
35322534Smckusick 	 * process (true if -w), we get this signal.
35422534Smckusick 	 */
35522534Smckusick 	signal(SIGWINCH, sigwinch);
35622534Smckusick #endif
35722534Smckusick 	getwinsize();		/* get window size from ioctl */
35822534Smckusick 
35922534Smckusick 	/* immediately fork and let the parent die if not emacs mode */
36022534Smckusick 	if (!emacs && !window && !dbug) {
36122534Smckusick 		if (fork())
36222534Smckusick 			exit(0);
36322534Smckusick 		/* pgrp should take care of things, but ignore them anyway */
36422534Smckusick 		signal(SIGINT, SIG_IGN);
36522534Smckusick 		signal(SIGQUIT, SIG_IGN);
36622534Smckusick #ifdef VMUNIX
36722534Smckusick 		signal(SIGTTOU, SIG_IGN);
36822534Smckusick #endif
36922534Smckusick 	}
37022534Smckusick 	/*
37122534Smckusick 	 * When we logoff, init will do a "vhangup()" on this
37222534Smckusick 	 * tty which turns off I/O access and sends a SIGHUP
37322534Smckusick 	 * signal.  We catch this and thereby clear the status
37422534Smckusick 	 * display.  Note that a bug in 4.1bsd caused the SIGHUP
37522534Smckusick 	 * signal to be sent to the wrong process, so you had to
37622534Smckusick 	 * `kill -HUP' yourself in your .logout file.
37722534Smckusick 	 * Do the same thing for SIGTERM, which is the default kill
37822534Smckusick 	 * signal.
37922534Smckusick 	 */
38022534Smckusick 	signal(SIGHUP, clearbotl);
38122534Smckusick 	signal(SIGTERM, clearbotl);
38222534Smckusick 	/*
38322534Smckusick 	 * This is so kill -ALRM to force update won't screw us up..
38422534Smckusick 	 */
38522534Smckusick 	signal(SIGALRM, SIG_IGN);
38622534Smckusick 
38722534Smckusick 	uid = getuid();
38822534Smckusick 	ourtty = ttyname(2);	/* remember what tty we are on */
38922534Smckusick 	if (printid) {
39022534Smckusick 		printf("%d\n", getpid());
39122534Smckusick 		fflush(stdout);
39222534Smckusick 	}
39322534Smckusick 	dup2(2, 1);
39422534Smckusick 
39522534Smckusick 	if ((home = getenv("HOME")) == 0)
39622534Smckusick 		home = "";
39722534Smckusick 	strcpy1(strcpy1(whofilename, home), "/.who");
39822534Smckusick 	strcpy1(strcpy1(whofilename2, home), "/.sysline");
39922534Smckusick 	strcpy1(strcpy1(lockfilename, home), "/.syslinelock");
40022534Smckusick 
40137905Sbostic 	if ((kmem = open(_PATH_KMEM,0)) < 0) {
40237905Sbostic 		fprintf(stderr, "Can't open %s\n", _PATH_KMEM);
40322534Smckusick 		exit(1);
40422534Smckusick 	}
40522534Smckusick 	readnamelist();
40622534Smckusick 	if (proccheck)
40722534Smckusick 		initprocread();
40822534Smckusick 	if (mailcheck)
40922534Smckusick 		if ((username = getenv("USER")) == 0)
41022534Smckusick 			mailcheck = 0;
41122534Smckusick 		else {
412*43861Sbostic 			chdir(_PATH_MAILDIR);
41322534Smckusick 			if (stat(username, &mstbuf) >= 0)
41422534Smckusick 				mailsize = mstbuf.st_size;
41522534Smckusick 			else
41622534Smckusick 				mailsize = 0;
41722534Smckusick 		}
41822534Smckusick 
41922534Smckusick 	while (emacs || window || isloggedin())
42022534Smckusick 		if (access(lockfilename, 0) >= 0)
42122534Smckusick 			sleep(60);
42222534Smckusick 		else {
42322534Smckusick 			prtinfo();
42422534Smckusick 			sleep(delay);
42522534Smckusick 			if (clr_bet_ref) {
42622534Smckusick 				tputs(dis_status_line, 1, outc);
42722534Smckusick 				fflush(stdout);
42822534Smckusick 				sleep(5);
42922534Smckusick 			}
43022534Smckusick 			revtime = (1 + revtime) % REVOFF;
43122534Smckusick 		}
43222534Smckusick 	clearbotl();
43322534Smckusick 	/*NOTREACHED*/
43422534Smckusick }
43522534Smckusick 
43622534Smckusick isloggedin()
43722534Smckusick {
43822534Smckusick 	/*
43922534Smckusick 	 * you can tell if a person has logged out if the owner of
44022534Smckusick 	 * the tty has changed
44122534Smckusick 	 */
44222534Smckusick 	struct stat statbuf;
44322534Smckusick 
44422534Smckusick 	return fstat(2, &statbuf) == 0 && statbuf.st_uid == uid;
44522534Smckusick }
44622534Smckusick 
44722534Smckusick readnamelist()
44822534Smckusick {
44922534Smckusick 	time_t bootime, clock, nintv, time();
45022534Smckusick 
45137905Sbostic 	nlist(_PATH_UNIX, nl);
45222534Smckusick 	if (nl[0].n_value == 0) {
45322534Smckusick 		if (!quiet)
45422534Smckusick 			fprintf(stderr, "No namelist\n");
45522534Smckusick 		return;
45622534Smckusick 	}
45722534Smckusick 	lseek(kmem, (long)nl[NL_BOOT].n_value, 0);
45822534Smckusick 	read(kmem, &bootime, sizeof(bootime));
45922534Smckusick 	(void) time(&clock);
46022534Smckusick 	nintv = clock - bootime;
46122534Smckusick 	if (nintv <= 0L || nintv > 60L*60L*24L*365L) {
46222534Smckusick 		if (!quiet)
46322534Smckusick 			fprintf(stderr,
46422534Smckusick 			"Time makes no sense... namelist must be wrong\n");
46538186Smckusick 		nl[NL_PROC].n_value = 0;
46622534Smckusick 	}
46722534Smckusick }
46822534Smckusick 
46923748Sedward readutmp(nflag)
47023748Sedward 	char nflag;
47122534Smckusick {
47223748Sedward 	static time_t lastmod;		/* initially zero */
47323748Sedward 	static off_t utmpsize;		/* ditto */
47422534Smckusick 	struct stat st;
47522534Smckusick 
47637905Sbostic 	if (ut < 0 && (ut = open(_PATH_UTMP, 0)) < 0) {
47737905Sbostic 		fprintf(stderr, "sysline: Can't open %s.\n", _PATH_UTMP);
47822534Smckusick 		exit(1);
47922534Smckusick 	}
48023748Sedward 	if (fstat(ut, &st) < 0 || st.st_mtime == lastmod)
48122534Smckusick 		return 0;
48222534Smckusick 	lastmod = st.st_mtime;
48323748Sedward 	if (utmpsize != st.st_size) {
48423748Sedward 		utmpsize = st.st_size;
48523748Sedward 		nentries = utmpsize / sizeof (struct utmp);
48623748Sedward 		if (old == 0) {
48723748Sedward 			old = (struct utmp *)calloc(utmpsize, 1);
48823748Sedward 			new = (struct utmp *)calloc(utmpsize, 1);
48923748Sedward 		} else {
49023748Sedward 			old = (struct utmp *)realloc((char *)old, utmpsize);
49123748Sedward 			new = (struct utmp *)realloc((char *)new, utmpsize);
49223748Sedward 			free(status);
49323748Sedward 		}
49423748Sedward 		status = malloc(nentries * sizeof *status);
49523748Sedward 		if (old == 0 || new == 0 || status == 0) {
49623748Sedward 			fprintf(stderr, "sysline: Out of memory.\n");
49723748Sedward 			exit(1);
49823748Sedward 		}
49923748Sedward 	}
50022534Smckusick 	lseek(ut, 0L, 0);
50123748Sedward 	(void) read(ut, (char *) (nflag ? new : old), utmpsize);
50222534Smckusick 	return 1;
50322534Smckusick }
50422534Smckusick 
50522534Smckusick /*
50622534Smckusick  * read in the process table locations and sizes, and allocate space
50722534Smckusick  * for storing the process table.  This is done only once.
50822534Smckusick  */
50922534Smckusick initprocread()
51022534Smckusick {
51122534Smckusick 
51222534Smckusick 	if (nl[NL_PROC].n_value == 0)
51322534Smckusick 		return;
51422534Smckusick #ifdef VMUNIX
51522534Smckusick 	lseek(kmem, (long)nl[NL_PROC].n_value, 0);
51622534Smckusick 	read(kmem, &procadr, sizeof procadr);
51722534Smckusick 	lseek(kmem, (long)nl[NL_NPROC].n_value, 0);
51822534Smckusick 	read(kmem, &nproc, sizeof nproc);
51922534Smckusick #endif
52022534Smckusick #ifdef pdp11
52122534Smckusick 	procadr = nl[NL_PROC].n_value;
52222534Smckusick 	nproc = NPROC;			/* from param.h */
52322534Smckusick #endif
52422534Smckusick 	if ((proc = (struct proc *) calloc(nproc, sizeof (struct proc))) == 0) {
52522534Smckusick 		fprintf(stderr, "Out of memory.\n");
52622534Smckusick 		exit(1);
52722534Smckusick 	}
52822534Smckusick 	procNPROC = proc + nproc;
52922534Smckusick }
53022534Smckusick 
53122534Smckusick /*
53222534Smckusick  * read in the process table.  This assumes that initprocread has alread been
53322534Smckusick  * called to set up storage.
53422534Smckusick  */
53522534Smckusick readproctab()
53622534Smckusick {
53722534Smckusick 
53822534Smckusick 	if (nl[NL_PROC].n_value == 0)
53922534Smckusick 		return (0);
54022534Smckusick 	lseek(kmem, (long)procadr, 0);
54122534Smckusick 	read(kmem, (char *)proc, nproc * sizeof (struct proc));
54222534Smckusick 	return (1);
54322534Smckusick }
54422534Smckusick 
54522534Smckusick prtinfo()
54622534Smckusick {
54722534Smckusick 	int on, off;
54822534Smckusick 	register i;
54922534Smckusick 	char fullprocess;
55022534Smckusick 
55122534Smckusick 	stringinit();
55222534Smckusick #ifdef SIGWINCH
55322534Smckusick 	if (winchanged) {
55422534Smckusick 		winchanged = 0;
55522534Smckusick 		getwinsize();
55622534Smckusick 		mustclear = 1;
55722534Smckusick 	}
55822534Smckusick #endif
55922534Smckusick #ifdef WHO
56022534Smckusick 	/* check for file named .who in the home directory */
56122534Smckusick 	whocheck();
56222534Smckusick #endif
56322534Smckusick 	timeprint();
56422534Smckusick 	/*
56522534Smckusick 	 * if mail is seen, don't print rest of info, just the mail
56622534Smckusick 	 * reverse new and old so that next time we run, we won't lose log
56722534Smckusick 	 * in and out information
56822534Smckusick 	 */
56922534Smckusick 	if (mailcheck && (sawmail = mailseen()))
57022534Smckusick 		goto bottom;
57122534Smckusick #ifdef HOSTNAME
57222534Smckusick #ifdef RWHO
57322534Smckusick 	for (i = 0; i < nremotes; i++) {
57422534Smckusick 		char *tmp;
57522534Smckusick 
57622534Smckusick 		stringspace();
57722534Smckusick 		tmp = sysrup(remotehost + i);
57822534Smckusick 		stringcat(tmp, strlen(tmp));
57922534Smckusick 	}
58022534Smckusick #endif
58122534Smckusick 	/*
58222534Smckusick 	 * print hostname info if requested
58322534Smckusick 	 */
58422534Smckusick 	if (hostprint) {
58522534Smckusick 		stringspace();
58622534Smckusick 		stringcat(hostname, -1);
58722534Smckusick 	}
58822534Smckusick #endif
58922534Smckusick 	/*
59022534Smckusick 	 * print load average and difference between current load average
59122534Smckusick 	 * and the load average 5 minutes ago
59222534Smckusick 	 */
59338186Smckusick 	if (getloadavg(avenrun, 3) > 0) {
59422534Smckusick 		double diff;
59522534Smckusick 
59622534Smckusick 		stringspace();
59738186Smckusick 
59822534Smckusick 		if ((diff = avenrun[0] - avenrun[1]) < 0.0)
59922534Smckusick 			stringprt("%.1f %.1f", avenrun[0],  diff);
60022534Smckusick 		else
60122534Smckusick 			stringprt("%.1f +%.1f", avenrun[0], diff);
60222534Smckusick 		loadavg = avenrun[0];		/* remember load average */
60322534Smckusick 	}
60422534Smckusick 	/*
60522534Smckusick 	 * print log on and off information
60622534Smckusick 	 */
60722534Smckusick 	stringspace();
60822534Smckusick 	fullprocess = 1;
60922534Smckusick #ifdef MAXLOAD
61022534Smckusick 	if (loadavg > MAXLOAD)
61122534Smckusick 		fullprocess = 0;	/* too loaded to run */
61222534Smckusick #endif
61322534Smckusick 	/*
61422534Smckusick 	 * Read utmp file (logged in data) only if we are doing a full
61522534Smckusick 	 * process, or if this is the first time and we are calculating
61622534Smckusick 	 * the number of users.
61722534Smckusick 	 */
61822534Smckusick 	on = off = 0;
61922534Smckusick 	if (users == 0) {		/* first time */
62023748Sedward 		if (readutmp(0))
62122534Smckusick 			for (i = 0; i < nentries; i++)
62222534Smckusick 				if (old[i].ut_name[0])
62322534Smckusick 					users++;
62423748Sedward 	} else if (fullprocess && readutmp(1)) {
62522534Smckusick 		struct utmp *tmp;
62622534Smckusick 
62722534Smckusick 		users = 0;
62822534Smckusick 		for (i = 0; i < nentries; i++) {
62922534Smckusick 			if (strncmp(old[i].ut_name,
63022534Smckusick 			    new[i].ut_name, NAMESIZE) == 0)
63122534Smckusick 				status[i] = NOCH;
63222534Smckusick 			else if (old[i].ut_name[0] == '\0') {
63322534Smckusick 				status[i] = ON;
63422534Smckusick 				on++;
63522534Smckusick 			} else if (new[i].ut_name[0] == '\0') {
63622534Smckusick 				status[i] = OFF;
63722534Smckusick 				off++;
63822534Smckusick 			} else {
63922534Smckusick 				status[i] = ON | OFF;
64022534Smckusick 				on++;
64122534Smckusick 				off++;
64222534Smckusick 			}
64322534Smckusick 			if (new[i].ut_name[0])
64422534Smckusick 				users++;
64522534Smckusick 		}
64622534Smckusick 		tmp = new;
64722534Smckusick 		new = old;
64822534Smckusick 		old = tmp;
64922534Smckusick 	}
65022534Smckusick 	/*
65122534Smckusick 	 * Print:
65222534Smckusick 	 * 	1.  number of users
65322534Smckusick 	 *	2.  a * for unread mail
65422534Smckusick 	 *	3.  a - if load is too high
65522534Smckusick 	 *	4.  number of processes running and stopped
65622534Smckusick 	 */
65722534Smckusick 	stringprt("%du", users);
65822534Smckusick 	if (mailsize > 0 && mstbuf.st_mtime >= mstbuf.st_atime)
65922534Smckusick 		stringcat("*", -1);
66022534Smckusick 	if (!fullprocess && (proccheck || logcheck))
66122534Smckusick 		stringcat("-", -1);
66222534Smckusick 	if (fullprocess && proccheck && readproctab()) {
66322534Smckusick 		register struct proc *p;
66422534Smckusick 		int procrun, procstop;
66522534Smckusick 
66622534Smckusick 		/*
66722534Smckusick 		 * We are only interested in processes which have the same
66822534Smckusick 		 * uid as us, and whose parent process id is not 1.
66922534Smckusick 		 */
67022534Smckusick 		procrun = procstop = 0;
67122534Smckusick 		for (p = proc; p < procNPROC; p++) {
67222534Smckusick 			if (p->p_stat == 0 || p->p_pgrp == 0 ||
67322534Smckusick 			    p->p_uid != uid || p->p_ppid == 1)
67422534Smckusick 				continue;
67522534Smckusick 			switch (p->p_stat) {
67622534Smckusick 			case SSTOP:
67722534Smckusick 				procstop++;
67822534Smckusick 				break;
67922534Smckusick 			case SSLEEP:
68022534Smckusick 				/*
68122534Smckusick 				 * Sleep can mean waiting for a signal or just
68222534Smckusick 				 * in a disk or page wait queue ready to run.
68322534Smckusick 				 * We can tell if it is the later by the pri
68422534Smckusick 				 * being negative.
68522534Smckusick 				 */
68622534Smckusick 				if (p->p_pri < PZERO)
68722534Smckusick 					procrun++;
68822534Smckusick 				break;
68922534Smckusick 			case SWAIT:
69022534Smckusick 			case SRUN:
69122534Smckusick 			case SIDL:
69222534Smckusick 				procrun++;
69322534Smckusick 			}
69422534Smckusick 		}
69522534Smckusick 		if (procrun > 0 || procstop > 0) {
69622534Smckusick 			stringspace();
69722534Smckusick 			if (procrun > 0 && procstop > 0)
69822534Smckusick 				stringprt("%dr %ds", procrun, procstop);
69922534Smckusick 			else if (procrun > 0)
70022534Smckusick 				stringprt("%dr", procrun);
70122534Smckusick 			else
70222534Smckusick 				stringprt("%ds", procstop);
70322534Smckusick 		}
70422534Smckusick 	}
70522534Smckusick 	/*
70622534Smckusick 	 * If anyone has logged on or off, and we are interested in it,
70722534Smckusick 	 * print it out.
70822534Smckusick 	 */
70922534Smckusick 	if (logcheck) {
71022534Smckusick 		/* old and new have already been swapped */
71122534Smckusick 		if (on) {
71222534Smckusick 			stringspace();
71322534Smckusick 			stringcat("on:", -1);
71422534Smckusick 			for (i = 0; i < nentries; i++)
71522534Smckusick 				if (status[i] & ON) {
71622534Smckusick 					stringprt(" %.8s", old[i].ut_name);
71722534Smckusick 					ttyprint(old[i].ut_line);
71822534Smckusick 				}
71922534Smckusick 		}
72022534Smckusick 		if (off) {
72122534Smckusick 			stringspace();
72222534Smckusick 			stringcat("off:", -1);
72322534Smckusick 			for (i = 0; i < nentries; i++)
72422534Smckusick 				if (status[i] & OFF) {
72522534Smckusick 					stringprt(" %.8s", new[i].ut_name);
72622534Smckusick 					ttyprint(new[i].ut_line);
72722534Smckusick 				}
72822534Smckusick 		}
72922534Smckusick 	}
73022534Smckusick bottom:
73122534Smckusick 		/* dump out what we know */
73222534Smckusick 	stringdump();
73322534Smckusick }
73422534Smckusick 
73522534Smckusick timeprint()
73622534Smckusick {
73722534Smckusick 	long curtime;
73822534Smckusick 	struct tm *tp, *localtime();
73922534Smckusick 	static int beepable = 1;
74022534Smckusick 
74122534Smckusick 		/* always print time */
74222534Smckusick 	time(&curtime);
74322534Smckusick 	tp = localtime(&curtime);
74422534Smckusick 	if (dateprint)
74522534Smckusick 		stringprt("%.11s", ctime(&curtime));
74622534Smckusick 	stringprt("%d:%02d", tp->tm_hour > 12 ? tp->tm_hour - 12 :
74722534Smckusick 		(tp->tm_hour == 0 ? 12 : tp->tm_hour), tp->tm_min);
74822534Smckusick 	if (synch)			/* sync with clock */
74922534Smckusick 		delay = 60 - tp->tm_sec;
75022534Smckusick 	/*
75122534Smckusick 	 * Beepable is used to insure that we get at most one set of beeps
75222534Smckusick 	 * every half hour.
75322534Smckusick 	 */
75422534Smckusick 	if (beep)
75522534Smckusick 		if (beepable) {
75622534Smckusick 			if (tp->tm_min == 30) {
75722534Smckusick 				tputs(bell, 1, outc);
75822534Smckusick 				fflush(stdout);
75922534Smckusick 				beepable = 0;
76022534Smckusick 			} else if (tp->tm_min == 0) {
76122534Smckusick 				tputs(bell, 1, outc);
76222534Smckusick 				fflush(stdout);
76322534Smckusick 				sleep(2);
76422534Smckusick 				tputs(bell, 1, outc);
76522534Smckusick 				fflush(stdout);
76622534Smckusick 				beepable = 0;
76722534Smckusick 			}
76822534Smckusick 		} else
76922534Smckusick 			if (tp->tm_min != 0 && tp->tm_min != 30)
77022534Smckusick 				beepable = 1;
77122534Smckusick }
77222534Smckusick 
77322534Smckusick /*
77422534Smckusick  * whocheck -- check for file named .who and print it on the who line first
77522534Smckusick  */
77622534Smckusick whocheck()
77722534Smckusick {
77822534Smckusick 	int chss;
77922534Smckusick 	register char *p;
78022534Smckusick 	char buff[81];
78122534Smckusick 	int whofile;
78222534Smckusick 
78322534Smckusick 	if ((whofile = open(whofilename, 0)) < 0 &&
78422534Smckusick 	    (whofile = open(whofilename2, 0)) < 0)
78522534Smckusick 		return;
78622534Smckusick 	chss = read(whofile, buff, sizeof buff - 1);
78722534Smckusick 	close(whofile);
78822534Smckusick 	if (chss <= 0)
78922534Smckusick 		return;
79022534Smckusick 	buff[chss] = '\0';
79122534Smckusick 	/*
79222534Smckusick 	 * Remove all line feeds, and replace by spaces if they are within
79322534Smckusick 	 * the message, else replace them by nulls.
79422534Smckusick 	 */
79522534Smckusick 	for (p = buff; *p;)
79622534Smckusick 		if (*p == '\n')
79722534Smckusick 			if (p[1])
79822534Smckusick 				*p++ = ' ';
79922534Smckusick 			else
80022534Smckusick 				*p = '\0';
80122534Smckusick 		else
80222534Smckusick 			p++;
80322534Smckusick 	stringcat(buff, p - buff);
80422534Smckusick 	stringspace();
80522534Smckusick }
80622534Smckusick 
80722534Smckusick /*
80822534Smckusick  * ttyprint -- given the name of a tty, print in the string buffer its
80922534Smckusick  * short name surrounded by parenthesis.
81022534Smckusick  * ttyxx is printed as (xx)
81122534Smckusick  * console is printed as (cty)
81222534Smckusick  */
81322534Smckusick ttyprint(name)
81422534Smckusick 	char *name;
81522534Smckusick {
81622534Smckusick 	char buff[11];
81722534Smckusick 
81822534Smckusick 	if (strncmp(name, "tty", 3) == 0)
81922534Smckusick 		stringprt("(%.*s)", LINESIZE - 3, name + 3);
82022534Smckusick 	else if (strcmp(name, "console") == 0)
82122534Smckusick 		stringcat("(cty)", -1);
82222534Smckusick 	else
82322534Smckusick 		stringprt("(%.*s)", LINESIZE, name);
82422534Smckusick }
82522534Smckusick 
82622534Smckusick /*
82722534Smckusick  * mail checking function
82822534Smckusick  * returns 0 if no mail seen
82922534Smckusick  */
83022534Smckusick mailseen()
83122534Smckusick {
83222534Smckusick 	FILE *mfd;
83322534Smckusick 	register n;
83422534Smckusick 	register char *cp;
83522534Smckusick 	char lbuf[100], sendbuf[100], *bufend;
83622534Smckusick 	char seenspace;
83722534Smckusick 	int retval = 0;
83822534Smckusick 
83922534Smckusick 	if (stat(username, &mstbuf) < 0) {
84022534Smckusick 		mailsize = 0;
84122534Smckusick 		return 0;
84222534Smckusick 	}
84322534Smckusick 	if (mstbuf.st_size <= mailsize || (mfd = fopen(username,"r")) == NULL) {
84422534Smckusick 		mailsize = mstbuf.st_size;
84522534Smckusick 		return 0;
84622534Smckusick 	}
84722534Smckusick 	fseek(mfd, mailsize, 0);
84822534Smckusick 	while ((n = readline(mfd, lbuf, sizeof lbuf)) >= 0 &&
84922534Smckusick 	       strncmp(lbuf, "From ", 5) != 0)
85022534Smckusick 		;
85122534Smckusick 	if (n < 0) {
85234586Sbostic 		stringcat("Mail has just arrived", -1);
85322534Smckusick 		goto out;
85422534Smckusick 	}
85522534Smckusick 	retval = 1;
85622534Smckusick 	/*
85722534Smckusick 	 * Found a From line, get second word, which is the sender,
85822534Smckusick 	 * and print it.
85922534Smckusick 	 */
86022534Smckusick 	for (cp = lbuf + 5; *cp && *cp != ' '; cp++)	/* skip to blank */
86122534Smckusick 		;
86222534Smckusick 	*cp = '\0';					/* terminate name */
86322534Smckusick 	stringspace();
86422534Smckusick 	stringprt("Mail from %s ", lbuf + 5);
86522534Smckusick 	/*
86622534Smckusick 	 * Print subject, and skip over header.
86722534Smckusick 	 */
86822534Smckusick 	while ((n = readline(mfd, lbuf, sizeof lbuf)) > 0)
86922534Smckusick 		if (strncmp(lbuf, "Subject:", 8) == 0)
87022534Smckusick 			stringprt("on %s ", lbuf + 9);
87122534Smckusick 	if (!emacs)
87222534Smckusick 		stringcat(arrows, 2);
87322534Smckusick 	else
87422534Smckusick 		stringcat(": ", 2);
87522534Smckusick 	if (n < 0)			/* already at eof */
87622534Smckusick 		goto out;
87722534Smckusick 	/*
87822534Smckusick 	 * Print as much of the letter as we can.
87922534Smckusick 	 */
88022534Smckusick 	cp = sendbuf;
88122534Smckusick 	if ((n = columns - chars) > sizeof sendbuf - 1)
88222534Smckusick 		n = sizeof sendbuf - 1;
88322534Smckusick 	bufend = cp + n;
88422534Smckusick 	seenspace = 0;
88522534Smckusick 	while ((n = readline(mfd, lbuf, sizeof lbuf)) >= 0) {
88622534Smckusick 		register char *rp;
88722534Smckusick 
88822534Smckusick 		if (strncmp(lbuf, "From ", 5) == 0)
88922534Smckusick 			break;
89022534Smckusick 		if (cp >= bufend)
89122534Smckusick 			continue;
89222534Smckusick 		if (!seenspace) {
89322534Smckusick 			*cp++ = ' ';		/* space before lines */
89422534Smckusick 			seenspace = 1;
89522534Smckusick 		}
89622534Smckusick 		rp = lbuf;
89722534Smckusick 		while (*rp && cp < bufend)
89822534Smckusick 			if (isspace(*rp)) {
89922534Smckusick 				if (!seenspace) {
90022534Smckusick 					*cp++ = ' ';
90122534Smckusick 					seenspace = 1;
90222534Smckusick 				}
90322534Smckusick 				rp++;
90422534Smckusick 			} else {
90522534Smckusick 				*cp++ = *rp++;
90622534Smckusick 				seenspace = 0;
90722534Smckusick 			}
90822534Smckusick 	}
90922534Smckusick 	*cp = 0;
91022534Smckusick 	stringcat(sendbuf, -1);
91122534Smckusick 	/*
91222534Smckusick 	 * Want to update write time so a star will
91322534Smckusick 	 * appear after the number of users until the
91422534Smckusick 	 * user reads his mail.
91522534Smckusick 	 */
91622534Smckusick out:
91722534Smckusick 	mailsize = linebeg;
91822534Smckusick 	fclose(mfd);
91922534Smckusick 	touch(username);
92022534Smckusick 	return retval;
92122534Smckusick }
92222534Smckusick 
92322534Smckusick /*
92422534Smckusick  * readline -- read a line from fp and store it in buf.
92522534Smckusick  * return the number of characters read.
92622534Smckusick  */
92722534Smckusick readline(fp, buf, n)
92822534Smckusick 	register FILE *fp;
92922534Smckusick 	char *buf;
93022534Smckusick 	register n;
93122534Smckusick {
93222534Smckusick 	register c;
93322534Smckusick 	register char *cp = buf;
93422534Smckusick 
93522534Smckusick 	linebeg = ftell(fp);		/* remember loc where line begins */
93622534Smckusick 	cp = buf;
93722534Smckusick 	while (--n > 0 && (c = getc(fp)) != EOF && c != '\n')
93822534Smckusick 		*cp++ = c;
93922534Smckusick 	*cp = 0;
94022534Smckusick 	if (c == EOF && cp - buf == 0)
94122534Smckusick 		return -1;
94222534Smckusick 	return cp - buf;
94322534Smckusick }
94422534Smckusick 
94522534Smckusick 
94622534Smckusick /*
94722534Smckusick  * string hacking functions
94822534Smckusick  */
94922534Smckusick 
95022534Smckusick stringinit()
95122534Smckusick {
95222534Smckusick 	sp = strarr;
95322534Smckusick 	chars = 0;
95422534Smckusick }
95522534Smckusick 
95622534Smckusick /*VARARGS1*/
95722534Smckusick stringprt(format, a, b, c)
95822534Smckusick 	char *format;
95922534Smckusick {
96022534Smckusick 	char tempbuf[150];
96122534Smckusick 
96232501Sbostic 	(void)sprintf(tempbuf, format, a, b, c);
96332501Sbostic 	stringcat(tempbuf, -1);
96422534Smckusick }
96522534Smckusick 
96622534Smckusick stringdump()
96722534Smckusick {
96822534Smckusick 	char bigbuf[sizeof strarr + 200];
96922534Smckusick 	register char *bp = bigbuf;
97022534Smckusick 	register int i;
97122534Smckusick 
97222534Smckusick 	if (!emacs) {
97322534Smckusick 		if (sawmail)
97422534Smckusick 			bp = strcpy1(bp, bell);
97522534Smckusick 		if (eslok)
97622534Smckusick 			bp = strcpy1(bp, tparm(to_status_line,
97722534Smckusick 				leftline ? 0 : columns - chars));
97822534Smckusick 		else {
97922534Smckusick 			bp = strcpy1(bp, to_status_line);
98022534Smckusick 			if (!shortline && !leftline)
98122534Smckusick 				for (i = columns - chars; --i >= 0;)
98222534Smckusick 					*bp++ = ' ';
98322534Smckusick 		}
98422534Smckusick 		if (reverse && revtime != 0)
98522534Smckusick 			bp = strcpy1(bp, rev_out);
98622534Smckusick 	}
98722534Smckusick 	*sp = 0;
98822534Smckusick 	bp = strcpy1(bp, strarr);
98922534Smckusick 	if (!emacs) {
99022534Smckusick 		if (reverse)
99122534Smckusick 			bp = strcpy1(bp, rev_end);
99222534Smckusick 		bp = strcpy1(bp, from_status_line);
99322534Smckusick 		if (sawmail)
99422534Smckusick 			bp = strcpy1(strcpy1(bp, bell), bell);
99522534Smckusick 		*bp = 0;
99622534Smckusick 		tputs(bigbuf, 1, outc);
99722534Smckusick 		if (mustclear) {
99822534Smckusick 			mustclear = 0;
99922534Smckusick 			tputs(clr_eol, 1, outc);
100022534Smckusick 		}
100122534Smckusick 		if (dbug)
100222534Smckusick 			putchar('\n');
100322534Smckusick 		fflush(stdout);
100422534Smckusick 	} else
100522534Smckusick 		write(2, bigbuf, bp - bigbuf);
100622534Smckusick }
100722534Smckusick 
100822534Smckusick stringspace()
100922534Smckusick {
101022534Smckusick 	if (reverse && revtime != 0) {
101122534Smckusick #ifdef TERMINFO
101222534Smckusick 		stringcat(rev_end,
101322534Smckusick 			magic_cookie_glitch <= 0 ? 0 : magic_cookie_glitch);
101422534Smckusick 		stringcat(" ", 1);
101522534Smckusick 		stringcat(rev_out,
101622534Smckusick 			magic_cookie_glitch <= 0 ? 0 : magic_cookie_glitch);
101722534Smckusick #else
101822534Smckusick 		stringcat(rev_end, 0);
101922534Smckusick 		stringcat(" ", 1);
102022534Smckusick 		stringcat(rev_out, 0);
102122534Smckusick #endif TERMINFO
102222534Smckusick 	} else
102322534Smckusick 		stringcat(" ", 1);
102422534Smckusick }
102522534Smckusick 
102622534Smckusick /*
102722534Smckusick  * stringcat :: concatenate the characters in string str to the list we are
102822534Smckusick  * 	        building to send out.
102922534Smckusick  * str - the string to print. may contain funny (terminal control) chars.
103022534Smckusick  * n  - the number of printable characters in the string
103122534Smckusick  *	or if -1 then str is all printable so we can truncate it,
103222534Smckusick  *	otherwise don't print only half a string.
103322534Smckusick  */
103422534Smckusick stringcat(str, n)
103522534Smckusick 	register char *str;
103622534Smckusick 	register n;
103722534Smckusick {
103822534Smckusick 	register char *p = sp;
103922534Smckusick 
104022534Smckusick 	if (n < 0) {				/* truncate */
104122534Smckusick 		n = columns - chars;
104222534Smckusick 		while ((*p++ = *str++) && --n >= 0)
104322534Smckusick 			;
104422534Smckusick 		p--;
104522534Smckusick 		chars += p - sp;
104622534Smckusick 		sp = p;
104722534Smckusick 	} else if (chars + n <= columns) {	/* don't truncate */
104822534Smckusick 		while (*p++ = *str++)
104922534Smckusick 			;
105022534Smckusick 		chars += n;
105122534Smckusick 		sp = p - 1;
105222534Smckusick 	}
105322534Smckusick }
105422534Smckusick 
105522534Smckusick /*
105622534Smckusick  * touch :: update the modify time of a file.
105722534Smckusick  */
105822534Smckusick touch(name)
105922534Smckusick 	char *name;		/* name of file */
106022534Smckusick {
106122534Smckusick 	register fd;
106222534Smckusick 	char buf;
106322534Smckusick 
106422534Smckusick 	if ((fd = open(name, 2)) >= 0) {
106522534Smckusick 		read(fd, &buf, 1);		/* get first byte */
106622534Smckusick 		lseek(fd, 0L, 0);		/* go to beginning */
106722534Smckusick 		write(fd, &buf, 1);		/* and rewrite first byte */
106822534Smckusick 		close(fd);
106922534Smckusick 	}
107022534Smckusick }
107122534Smckusick 
107222534Smckusick 
107322534Smckusick /*
107422534Smckusick  * clearbotl :: clear bottom line.
107522534Smckusick  * called when process quits or is killed.
107622534Smckusick  * it clears the bottom line of the terminal.
107722534Smckusick  */
107822534Smckusick clearbotl()
107922534Smckusick {
108022534Smckusick 	register int fd;
108122534Smckusick 	int exit();
108222534Smckusick 
108322534Smckusick 	signal(SIGALRM, exit);
108422534Smckusick 	alarm(30);	/* if can't open in 30 secs, just die */
108522534Smckusick 	if (!emacs && (fd = open(ourtty, 1)) >= 0) {
108622534Smckusick 		write(fd, dis_status_line, strlen(dis_status_line));
108722534Smckusick 		close(fd);
108822534Smckusick 	}
108922534Smckusick #ifdef PROF
109037905Sbostic 	if (chdir(_PATH_SYSLINE) < 0)
109137905Sbostic 		(void) chdir(_PATH_TMP);
109222534Smckusick #endif
109322534Smckusick 	exit(0);
109422534Smckusick }
109522534Smckusick 
109622534Smckusick #ifdef TERMINFO
109722534Smckusick initterm()
109822534Smckusick {
109922534Smckusick 	static char standbuf[40];
110022534Smckusick 
110122534Smckusick 	setupterm(0, 1, 0);
110222534Smckusick 	if (!window && !has_status_line) {
110322534Smckusick 		/* not an appropriate terminal */
110422534Smckusick 		if (!quiet)
110522534Smckusick 		   fprintf(stderr, "sysline: no status capability for %s\n",
110622534Smckusick 			getenv("TERM"));
110722534Smckusick 		exit(1);
110822534Smckusick 	}
110922534Smckusick 	if (window || status_line_esc_ok) {
111022534Smckusick 		if (set_attributes) {
111122534Smckusick 			/* reverse video mode */
111222534Smckusick 			strcpy(standbuf,
111322534Smckusick 				tparm(set_attributes,0,0,1,0,0,0,0,0,0));
111422534Smckusick 			rev_out = standbuf;
111522534Smckusick 			rev_end = exit_attribute_mode;
111622534Smckusick 		} else if (enter_standout_mode && exit_standout_mode) {
111722534Smckusick 			rev_out = enter_standout_mode;
111822534Smckusick 			rev_end = exit_standout_mode;
111922534Smckusick 		} else
112022534Smckusick 			rev_out = rev_end = "";
112122534Smckusick 	} else
112222534Smckusick 		rev_out = rev_end = "";
112322534Smckusick 	columns--;	/* avoid cursor wraparound */
112422534Smckusick }
112522534Smckusick 
112622534Smckusick #else	/* TERMCAP */
112722534Smckusick 
112822534Smckusick initterm()
112922534Smckusick {
113022534Smckusick 	char *term, *cp;
113122534Smckusick 	static char tbuf[1024];
113222534Smckusick 	char is2[40];
113322534Smckusick 	extern char *UP;
113422534Smckusick 
113522534Smckusick 	if ((term = getenv("TERM")) == NULL) {
113622534Smckusick 		if (!quiet)
113722534Smckusick 			fprintf(stderr,
113822534Smckusick 				"sysline: No TERM variable in enviroment\n");
113922534Smckusick 		exit(1);
114022534Smckusick 	}
114122534Smckusick 	if (tgetent(tbuf, term) <= 0) {
114222534Smckusick 		if (!quiet)
114322534Smckusick 			fprintf(stderr,
114422534Smckusick 				"sysline: Unknown terminal type: %s\n", term);
114522534Smckusick 		exit(1);
114622534Smckusick 	}
114722534Smckusick 	if (!window && tgetflag("hs") <= 0) {
114822534Smckusick 		if (!strncmp(term, "h19", 3)) {
114922534Smckusick 			/* for upward compatability with h19sys */
115022534Smckusick 			strcpy(to_status_line,
115122534Smckusick 				"\033j\033x5\033x1\033Y8%+ \033o");
115222534Smckusick 			strcpy(from_status_line, "\033k\033y5");
115322534Smckusick 			strcpy(dis_status_line, "\033y1");
115422534Smckusick 			strcpy(rev_out, "\033p");
115522534Smckusick 			strcpy(rev_end, "\033q");
115622534Smckusick 			arrows = "\033Fhh\033G";
115722534Smckusick 			columns = 80;
115822534Smckusick 			UP = "\b";
115922534Smckusick 			return;
116022534Smckusick 		}
116122534Smckusick 		if (!quiet)
116222534Smckusick 			fprintf(stderr,
116322534Smckusick 				"sysline: No status capability for %s\n", term);
116422534Smckusick 		exit(1);
116522534Smckusick 	}
116622534Smckusick 	cp = is2;
116722534Smckusick 	if (tgetstr("i2", &cp) != NULL) {
116822534Smckusick 		/* someday tset will do this */
116922534Smckusick 		tputs(is2, 1, erroutc);
117022534Smckusick 		fflush(stdout);
117122534Smckusick 	}
117222534Smckusick 
117322534Smckusick 	/* the "-1" below is to avoid cursor wraparound problems */
117430978Sbostic 	columns = tgetnum("ws");
117534586Sbostic 	hasws = columns >= 0;
117634586Sbostic 	if (!hasws)
117730978Sbostic 		columns = tgetnum("co");
117830978Sbostic 	columns -= 1;
117922534Smckusick 	if (window) {
118022534Smckusick 		strcpy(to_status_line, "\r");
118122534Smckusick 		cp = dis_status_line;	/* use the clear line sequence */
118222534Smckusick 		*cp++ = '\r';
118322534Smckusick 		tgetstr("ce", &cp);
118425792Skarels 		if (leftline)
118525792Skarels 			strcpy(from_status_line, dis_status_line + 1);
118625792Skarels 		else
118725792Skarels 			strcpy(from_status_line, "");
118822534Smckusick 	} else {
118922534Smckusick 		cp = to_status_line;
119022534Smckusick 		tgetstr("ts", &cp);
119122534Smckusick 		cp = from_status_line;
119222534Smckusick 		tgetstr("fs", &cp);
119322534Smckusick 		cp = dis_status_line;
119422534Smckusick 		tgetstr("ds", &cp);
119522534Smckusick 		eslok = tgetflag("es");
119622534Smckusick 	}
119722534Smckusick 	if (eslok || window) {
119822534Smckusick 		cp = rev_out;
119922534Smckusick 		tgetstr("so", &cp);
120022534Smckusick 		cp = rev_end;
120122534Smckusick 		tgetstr("se", &cp);
120222534Smckusick 		cp = clr_eol;
120322534Smckusick 		tgetstr("ce", &cp);
120422534Smckusick 	} else
120522534Smckusick 		reverse = 0;	/* turn off reverse video */
120622534Smckusick 	UP = "\b";
120722534Smckusick 	if (!strncmp(term, "h19", 3))
120822534Smckusick 		arrows = "\033Fhh\033G";	/* "two tiny graphic arrows" */
120922534Smckusick 	else
121022534Smckusick 		arrows = "->";
121122534Smckusick }
121222534Smckusick #endif TERMINFO
121322534Smckusick 
121422534Smckusick #ifdef RWHO
121522534Smckusick char *
121622534Smckusick sysrup(hp)
121722534Smckusick 	register struct remotehost *hp;
121822534Smckusick {
121922534Smckusick 	char filename[100];
122022534Smckusick 	struct whod wd;
122125368Sbloom #define WHOD_HDR_SIZE (sizeof (wd) - sizeof (wd.wd_we))
122222534Smckusick 	static char buffer[50];
122322534Smckusick 	time_t now;
122422534Smckusick 
122522534Smckusick 	/*
122622534Smckusick 	 * rh_file is initially 0.
122722534Smckusick 	 * This is ok since standard input is assumed to exist.
122822534Smckusick 	 */
122922534Smckusick 	if (hp->rh_file == 0) {
123022534Smckusick 		/*
123122534Smckusick 		 * Try rwho hostname file, and if that fails try ucbhostname.
123222534Smckusick 		 */
123337905Sbostic 		(void) strcpy1(strcpy1(filename, _PATH_RWHO), hp->rh_host);
123422534Smckusick 		if ((hp->rh_file = open(filename, 0)) < 0) {
123537905Sbostic 			(void) strcpy1(strcpy1(strcpy1(filename, _PATH_RWHO),
123622534Smckusick 				NETPREFIX), hp->rh_host);
123722534Smckusick 			hp->rh_file = open(filename, 0);
123822534Smckusick 		}
123922534Smckusick 	}
124030978Sbostic 	if (hp->rh_file < 0) {
124130978Sbostic 		(void) sprintf(buffer, "%s?", hp->rh_host);
124230978Sbostic 		return(buffer);
124330978Sbostic 	}
124422534Smckusick 	(void) lseek(hp->rh_file, (off_t)0, 0);
124530978Sbostic 	if (read(hp->rh_file, (char *)&wd, WHOD_HDR_SIZE) != WHOD_HDR_SIZE) {
124630978Sbostic 		(void) sprintf(buffer, "%s ?", hp->rh_host);
124730978Sbostic 		return(buffer);
124830978Sbostic 	}
124922534Smckusick 	(void) time(&now);
125022534Smckusick 	if (now - wd.wd_recvtime > DOWN_THRESHOLD) {
125122534Smckusick 		long interval;
125222534Smckusick 		long days, hours, minutes;
125322534Smckusick 
125422534Smckusick 		interval = now - wd.wd_recvtime;
125522534Smckusick 		minutes = (interval + 59) / 60;	/* round to minutes */
125622534Smckusick 		hours = minutes / 60;		/* extract hours from minutes */
125722534Smckusick 		minutes %= 60;			/* remove hours from minutes */
125822534Smckusick 		days = hours / 24;		/* extract days from hours */
125922534Smckusick 		hours %= 24;			/* remove days from hours */
126022534Smckusick 		if (days > 7 || days < 0)
126122534Smckusick 			(void) sprintf(buffer, "%s down", hp->rh_host);
126222534Smckusick 		else if (days > 0)
126322534Smckusick 			(void) sprintf(buffer, "%s %d+%d:%02d",
126422534Smckusick 				hp->rh_host, days, hours, minutes);
126522534Smckusick 		else
126622534Smckusick 			(void) sprintf(buffer, "%s %d:%02d",
126722534Smckusick 				hp->rh_host, hours, minutes);
126822534Smckusick 	} else
126922534Smckusick 		(void) sprintf(buffer, "%s %.1f",
127022534Smckusick 			hp->rh_host, wd.wd_loadav[0]/100.0);
127122534Smckusick 	return buffer;
127222534Smckusick }
127322534Smckusick #endif RWHO
127422534Smckusick 
127522534Smckusick getwinsize()
127622534Smckusick {
127722534Smckusick #ifdef TIOCGWINSZ
127822534Smckusick 	struct winsize winsize;
127922534Smckusick 
128022534Smckusick 	/* the "-1" below is to avoid cursor wraparound problems */
128134586Sbostic 	if (!hasws && ioctl(2, TIOCGWINSZ, (char *)&winsize) >= 0 &&
128234586Sbostic 		winsize.ws_col != 0)
128322534Smckusick 		columns = winsize.ws_col - 1;
128422534Smckusick #endif
128522534Smckusick }
128622534Smckusick 
128722534Smckusick #ifdef SIGWINCH
128822534Smckusick sigwinch()
128922534Smckusick {
129022534Smckusick 	winchanged++;
129122534Smckusick }
129222534Smckusick #endif
129322534Smckusick 
129422534Smckusick char *
129522534Smckusick strcpy1(p, q)
129622534Smckusick 	register char *p, *q;
129722534Smckusick {
129822534Smckusick 
129922534Smckusick 	while (*p++ = *q++)
130022534Smckusick 		;
130122534Smckusick 	return p - 1;
130222534Smckusick }
130322534Smckusick 
130422534Smckusick outc(c)
130522534Smckusick 	char c;
130622534Smckusick {
130722534Smckusick 	if (dbug)
130822534Smckusick 		printf("%s", unctrl(c));
130922534Smckusick 	else
131022534Smckusick 		putchar(c);
131122534Smckusick }
131222534Smckusick 
131322534Smckusick erroutc(c)
131422534Smckusick 	char c;
131522534Smckusick {
131622534Smckusick 	if (dbug)
131722534Smckusick 		fprintf(stderr, "%s", unctrl(c));
131822534Smckusick 	else
131922534Smckusick 		putc(c, stderr);
132022534Smckusick }
1321