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