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