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