1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 */ 12 13 #ifndef lint 14 char copyright[] = 15 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 16 All rights reserved.\n"; 17 #endif /* not lint */ 18 19 #ifndef lint 20 static char sccsid[] = "@(#)sysline.c 5.11 (Berkeley) 06/01/88"; 21 #endif /* not lint */ 22 23 /* 24 * sysline - system status display on 25th line of terminal 25 * j.k.foderaro 26 * 27 * Prints a variety of information on the special status line of terminals 28 * that have a status display capability. Cursor motions, status commands, 29 * etc. are gleamed from /etc/termcap. 30 * By default, all information is printed, and flags are given on the command 31 * line to disable the printing of information. The information and 32 * disabling flags are: 33 * 34 * flag what 35 * ----- ---- 36 * time of day 37 * load average and change in load average in the last 5 mins 38 * number of user logged on 39 * -p # of processes the users owns which are runnable and the 40 * number which are suspended. Processes whose parent is 1 41 * are not counted. 42 * -l users who've logged on and off. 43 * -m summarize new mail which has arrived 44 * 45 * <other flags> 46 * -r use non reverse video 47 * -c turn off 25th line for 5 seconds before redisplaying. 48 * -b beep once one the half hour, twice on the hour 49 * +N refresh display every N seconds. 50 * -i print pid first thing 51 * -e do simple print designed for an emacs buffer line 52 * -w do the right things for a window 53 * -h print hostname between time and load average 54 * -D print day/date before time of day 55 * -d debug mode - print status line data in human readable format 56 * -q quiet mode - don't output diagnostic messages 57 * -s print Short (left-justified) line if escapes not allowed 58 * -j Print left Justified line regardless 59 */ 60 61 #define BSD4_2 /* for 4.2 BSD */ 62 #define WHO /* turn this on always */ 63 #define HOSTNAME /* 4.1a or greater, with hostname() */ 64 #define RWHO /* 4.1a or greater, with rwho */ 65 #define VMUNIX /* turn this on if you are running on vmunix */ 66 #define NEW_BOOTTIME /* 4.1c or greater */ 67 68 #define NETPREFIX "ucb" 69 #define DEFDELAY 60 /* update status once per minute */ 70 #define MAILDIR "/usr/spool/mail" 71 /* 72 * if MAXLOAD is defined, then if the load average exceeded MAXLOAD 73 * then the process table will not be scanned and the log in/out data 74 * will not be checked. The purpose of this is to reduced the load 75 * on the system when it is loaded. 76 */ 77 #define MAXLOAD 6.0 78 79 #include <stdio.h> 80 #include <sys/param.h> 81 #include <sys/signal.h> 82 #include <utmp.h> 83 #include <ctype.h> 84 #ifndef BSD4_2 85 #include <unctrl.h> 86 #endif 87 #include <sys/time.h> 88 #include <sys/stat.h> 89 #ifdef VMUNIX 90 #include <nlist.h> 91 #include <sys/vtimes.h> 92 #include <sys/proc.h> 93 #endif 94 #ifdef pdp11 95 #include <a.out.h> 96 #include <sys/proc.h> 97 #endif 98 #include <curses.h> 99 #undef nl 100 #ifdef TERMINFO 101 #include <term.h> 102 #endif TERMINFO 103 104 #ifdef RWHO 105 #include <protocols/rwhod.h> 106 107 #define DOWN_THRESHOLD (11 * 60) 108 #define RWHOLEADER "/usr/spool/rwho/whod." 109 110 struct remotehost { 111 char *rh_host; 112 int rh_file; 113 } remotehost[10]; 114 int nremotes = 0; 115 #endif RWHO 116 117 struct nlist nl[] = { 118 #ifdef NEW_BOOTTIME 119 { "_boottime" }, /* After 4.1a the label changed to "boottime" */ 120 #else 121 { "_bootime" }, /* Under 4.1a and earlier it is "bootime" */ 122 #endif 123 #define NL_BOOT 0 124 { "_proc" }, 125 #define NL_PROC 1 126 { "_avenrun" }, 127 #define NL_AVEN 2 128 #ifdef VMUNIX 129 { "_nproc" }, 130 #define NL_NPROC 3 131 #endif 132 0 133 }; 134 135 /* stuff for the kernel */ 136 int kmem; /* file descriptor for /dev/kmem */ 137 struct proc *proc, *procNPROC; 138 int nproc; 139 int procadr; 140 double avenrun[3]; /* used for storing load averages */ 141 142 /* 143 * In order to determine how many people are logged on and who has 144 * logged in or out, we read in the /etc/utmp file. We also keep track of 145 * the previous utmp file. 146 */ 147 int ut = -1; /* the file descriptor */ 148 struct utmp *new, *old; 149 char *status; /* per tty status bits, see below */ 150 int nentries; /* number of utmp entries */ 151 /* string lengths for printing */ 152 #define LINESIZE (sizeof old->ut_line) 153 #define NAMESIZE (sizeof old->ut_name) 154 /* 155 * Status codes to say what has happened to a particular entry in utmp. 156 * NOCH means no change, ON means new person logged on, 157 * OFF means person logged off. 158 */ 159 #define NOCH 0 160 #define ON 0x1 161 #define OFF 0x2 162 163 #ifdef WHO 164 char whofilename[100]; 165 char whofilename2[100]; 166 #endif 167 168 #ifdef HOSTNAME 169 char hostname[MAXHOSTNAMELEN+1]; /* one more for null termination */ 170 #endif 171 172 char lockfilename[100]; /* if exists, will prevent us from running */ 173 174 /* flags which determine which info is printed */ 175 int mailcheck = 1; /* m - do biff like checking of mail */ 176 int proccheck = 1; /* p - give information on processes */ 177 int logcheck = 1; /* l - tell who logs in and out */ 178 int hostprint = 0; /* h - print out hostname */ 179 int dateprint = 0; /* h - print out day/date */ 180 int quiet = 0; /* q - hush diagnostic messages */ 181 182 /* flags which determine how things are printed */ 183 int clr_bet_ref = 0; /* c - clear line between refeshes */ 184 int reverse = 1; /* r - use reverse video */ 185 int shortline = 0; /* s - short (left-justified) if escapes not allowed */ 186 int leftline = 0; /* j - left-justified even if escapes allowed */ 187 188 /* flags which have terminal do random things */ 189 int beep = 0; /* b - beep every half hour and twice every hour */ 190 int printid = 0; /* i - print pid of this process at startup */ 191 int synch = 1; /* synchronize with clock */ 192 193 /* select output device (status display or straight output) */ 194 int emacs = 0; /* e - assume status display */ 195 int window = 0; /* w - window mode */ 196 int dbug = 0; /* d - debug */ 197 198 /* 199 * used to turn off reverse video every REVOFF times 200 * in an attempt to not wear out the phospher. 201 */ 202 #define REVOFF 5 203 int revtime = 1; 204 205 /* used by mail checker */ 206 off_t mailsize = 0; 207 off_t linebeg = 0; /* place where we last left off reading */ 208 209 /* things used by the string routines */ 210 int chars; /* number of printable characters */ 211 char *sp; 212 char strarr[512]; /* big enough now? */ 213 /* flags to stringdump() */ 214 char sawmail; /* remember mail was seen to print bells */ 215 char mustclear; /* status line messed up */ 216 217 /* strings which control status line display */ 218 #ifdef TERMINFO 219 char *rev_out, *rev_end, *arrows; 220 char *tparm(); 221 #else 222 char to_status_line[64]; 223 char from_status_line[64]; 224 char dis_status_line[64]; 225 char clr_eol[64]; 226 char rev_out[20], rev_end[20]; 227 char *arrows, *bell = "\007"; 228 int eslok; /* escapes on status line okay (reverse, cursor addressing) */ 229 int hasws = 0; /* is "ws" explicitly defined? */ 230 int columns; 231 #define tparm(cap, parm) tgoto((cap), 0, (parm)) 232 char *tgoto(); 233 #endif 234 235 /* to deal with window size changes */ 236 #ifdef SIGWINCH 237 int sigwinch(); 238 char winchanged; /* window size has changed since last update */ 239 #endif 240 241 /* random globals */ 242 char *username; 243 char *ourtty; /* keep track of what tty we're on */ 244 struct stat stbuf, mstbuf; /* mstbuf for mail check only */ 245 unsigned delay = DEFDELAY; 246 uid_t uid; 247 double loadavg = 0.0; /* current load average */ 248 int users = 0; 249 250 char *getenv(); 251 char *ttyname(); 252 char *strcpy1(); 253 char *sysrup(); 254 char *calloc(); 255 char *malloc(); 256 int outc(); 257 int erroutc(); 258 259 main(argc,argv) 260 register char **argv; 261 { 262 int clearbotl(); 263 register char *cp; 264 char *home; 265 extern char *index(); 266 267 #ifdef HOSTNAME 268 gethostname(hostname, sizeof hostname - 1); 269 if ((cp = index(hostname, '.')) != NULL) 270 *cp = '\0'; 271 #endif 272 273 for (argv++; *argv != 0; argv++) 274 switch (**argv) { 275 case '-': 276 for (cp = *argv + 1; *cp; cp++) { 277 switch(*cp) { 278 case 'r' : /* turn off reverse video */ 279 reverse = 0; 280 break; 281 case 'c': 282 clr_bet_ref = 1; 283 break; 284 case 'h': 285 hostprint = 1; 286 break; 287 case 'D': 288 dateprint = 1; 289 break; 290 #ifdef RWHO 291 case 'H': 292 if (argv[1] == 0) 293 break; 294 argv++; 295 if (strcmp(hostname, *argv) && 296 strcmp(&hostname[sizeof NETPREFIX - 1], *argv)) 297 remotehost[nremotes++].rh_host = *argv; 298 break; 299 #endif RWHO 300 case 'm': 301 mailcheck = 0; 302 break; 303 case 'p': 304 proccheck = 0; 305 break; 306 case 'l': 307 logcheck = 0; 308 break; 309 case 'b': 310 beep = 1; 311 break; 312 case 'i': 313 printid = 1; 314 break; 315 case 'w': 316 window = 1; 317 break; 318 case 'e': 319 emacs = 1; 320 break; 321 case 'd': 322 dbug = 1; 323 break; 324 case 'q': 325 quiet = 1; 326 break; 327 case 's': 328 shortline = 1; 329 break; 330 case 'j': 331 leftline = 1; 332 break; 333 default: 334 fprintf(stderr, 335 "sysline: bad flag: %c\n", *cp); 336 } 337 } 338 break; 339 case '+': 340 delay = atoi(*argv + 1); 341 if (delay < 10) 342 delay = 10; 343 else if (delay > 500) 344 delay = 500; 345 synch = 0; /* no more sync */ 346 break; 347 default: 348 fprintf(stderr, "sysline: illegal argument %s\n", 349 argv[0]); 350 } 351 if (emacs) { 352 reverse = 0; 353 columns = 79; 354 } else /* if not to emacs window, initialize terminal dependent info */ 355 initterm(); 356 #ifdef SIGWINCH 357 /* 358 * When the window size changes and we are the foreground 359 * process (true if -w), we get this signal. 360 */ 361 signal(SIGWINCH, sigwinch); 362 #endif 363 getwinsize(); /* get window size from ioctl */ 364 365 /* immediately fork and let the parent die if not emacs mode */ 366 if (!emacs && !window && !dbug) { 367 if (fork()) 368 exit(0); 369 /* pgrp should take care of things, but ignore them anyway */ 370 signal(SIGINT, SIG_IGN); 371 signal(SIGQUIT, SIG_IGN); 372 #ifdef VMUNIX 373 signal(SIGTTOU, SIG_IGN); 374 #endif 375 } 376 /* 377 * When we logoff, init will do a "vhangup()" on this 378 * tty which turns off I/O access and sends a SIGHUP 379 * signal. We catch this and thereby clear the status 380 * display. Note that a bug in 4.1bsd caused the SIGHUP 381 * signal to be sent to the wrong process, so you had to 382 * `kill -HUP' yourself in your .logout file. 383 * Do the same thing for SIGTERM, which is the default kill 384 * signal. 385 */ 386 signal(SIGHUP, clearbotl); 387 signal(SIGTERM, clearbotl); 388 /* 389 * This is so kill -ALRM to force update won't screw us up.. 390 */ 391 signal(SIGALRM, SIG_IGN); 392 393 uid = getuid(); 394 ourtty = ttyname(2); /* remember what tty we are on */ 395 if (printid) { 396 printf("%d\n", getpid()); 397 fflush(stdout); 398 } 399 dup2(2, 1); 400 401 if ((home = getenv("HOME")) == 0) 402 home = ""; 403 strcpy1(strcpy1(whofilename, home), "/.who"); 404 strcpy1(strcpy1(whofilename2, home), "/.sysline"); 405 strcpy1(strcpy1(lockfilename, home), "/.syslinelock"); 406 407 if ((kmem = open("/dev/kmem",0)) < 0) { 408 fprintf(stderr, "Can't open kmem.\n"); 409 exit(1); 410 } 411 readnamelist(); 412 if (proccheck) 413 initprocread(); 414 if (mailcheck) 415 if ((username = getenv("USER")) == 0) 416 mailcheck = 0; 417 else { 418 chdir(MAILDIR); 419 if (stat(username, &mstbuf) >= 0) 420 mailsize = mstbuf.st_size; 421 else 422 mailsize = 0; 423 } 424 425 while (emacs || window || isloggedin()) 426 if (access(lockfilename, 0) >= 0) 427 sleep(60); 428 else { 429 prtinfo(); 430 sleep(delay); 431 if (clr_bet_ref) { 432 tputs(dis_status_line, 1, outc); 433 fflush(stdout); 434 sleep(5); 435 } 436 revtime = (1 + revtime) % REVOFF; 437 } 438 clearbotl(); 439 /*NOTREACHED*/ 440 } 441 442 isloggedin() 443 { 444 /* 445 * you can tell if a person has logged out if the owner of 446 * the tty has changed 447 */ 448 struct stat statbuf; 449 450 return fstat(2, &statbuf) == 0 && statbuf.st_uid == uid; 451 } 452 453 readnamelist() 454 { 455 time_t bootime, clock, nintv, time(); 456 457 #ifdef pdp11 458 nlist("/unix", nl); 459 #else 460 nlist("/vmunix", nl); 461 #endif 462 if (nl[0].n_value == 0) { 463 if (!quiet) 464 fprintf(stderr, "No namelist\n"); 465 return; 466 } 467 lseek(kmem, (long)nl[NL_BOOT].n_value, 0); 468 read(kmem, &bootime, sizeof(bootime)); 469 (void) time(&clock); 470 nintv = clock - bootime; 471 if (nintv <= 0L || nintv > 60L*60L*24L*365L) { 472 if (!quiet) 473 fprintf(stderr, 474 "Time makes no sense... namelist must be wrong\n"); 475 nl[NL_PROC].n_value = nl[NL_AVEN].n_value = 0; 476 } 477 } 478 479 readutmp(nflag) 480 char nflag; 481 { 482 static time_t lastmod; /* initially zero */ 483 static off_t utmpsize; /* ditto */ 484 struct stat st; 485 486 if (ut < 0 && (ut = open("/etc/utmp", 0)) < 0) { 487 fprintf(stderr, "sysline: Can't open utmp.\n"); 488 exit(1); 489 } 490 if (fstat(ut, &st) < 0 || st.st_mtime == lastmod) 491 return 0; 492 lastmod = st.st_mtime; 493 if (utmpsize != st.st_size) { 494 utmpsize = st.st_size; 495 nentries = utmpsize / sizeof (struct utmp); 496 if (old == 0) { 497 old = (struct utmp *)calloc(utmpsize, 1); 498 new = (struct utmp *)calloc(utmpsize, 1); 499 } else { 500 old = (struct utmp *)realloc((char *)old, utmpsize); 501 new = (struct utmp *)realloc((char *)new, utmpsize); 502 free(status); 503 } 504 status = malloc(nentries * sizeof *status); 505 if (old == 0 || new == 0 || status == 0) { 506 fprintf(stderr, "sysline: Out of memory.\n"); 507 exit(1); 508 } 509 } 510 lseek(ut, 0L, 0); 511 (void) read(ut, (char *) (nflag ? new : old), utmpsize); 512 return 1; 513 } 514 515 /* 516 * read in the process table locations and sizes, and allocate space 517 * for storing the process table. This is done only once. 518 */ 519 initprocread() 520 { 521 522 if (nl[NL_PROC].n_value == 0) 523 return; 524 #ifdef VMUNIX 525 lseek(kmem, (long)nl[NL_PROC].n_value, 0); 526 read(kmem, &procadr, sizeof procadr); 527 lseek(kmem, (long)nl[NL_NPROC].n_value, 0); 528 read(kmem, &nproc, sizeof nproc); 529 #endif 530 #ifdef pdp11 531 procadr = nl[NL_PROC].n_value; 532 nproc = NPROC; /* from param.h */ 533 #endif 534 if ((proc = (struct proc *) calloc(nproc, sizeof (struct proc))) == 0) { 535 fprintf(stderr, "Out of memory.\n"); 536 exit(1); 537 } 538 procNPROC = proc + nproc; 539 } 540 541 /* 542 * read in the process table. This assumes that initprocread has alread been 543 * called to set up storage. 544 */ 545 readproctab() 546 { 547 548 if (nl[NL_PROC].n_value == 0) 549 return (0); 550 lseek(kmem, (long)procadr, 0); 551 read(kmem, (char *)proc, nproc * sizeof (struct proc)); 552 return (1); 553 } 554 555 prtinfo() 556 { 557 int on, off; 558 register i; 559 char fullprocess; 560 561 stringinit(); 562 #ifdef SIGWINCH 563 if (winchanged) { 564 winchanged = 0; 565 getwinsize(); 566 mustclear = 1; 567 } 568 #endif 569 #ifdef WHO 570 /* check for file named .who in the home directory */ 571 whocheck(); 572 #endif 573 timeprint(); 574 /* 575 * if mail is seen, don't print rest of info, just the mail 576 * reverse new and old so that next time we run, we won't lose log 577 * in and out information 578 */ 579 if (mailcheck && (sawmail = mailseen())) 580 goto bottom; 581 #ifdef HOSTNAME 582 #ifdef RWHO 583 for (i = 0; i < nremotes; i++) { 584 char *tmp; 585 586 stringspace(); 587 tmp = sysrup(remotehost + i); 588 stringcat(tmp, strlen(tmp)); 589 } 590 #endif 591 /* 592 * print hostname info if requested 593 */ 594 if (hostprint) { 595 stringspace(); 596 stringcat(hostname, -1); 597 } 598 #endif 599 /* 600 * print load average and difference between current load average 601 * and the load average 5 minutes ago 602 */ 603 if (nl[NL_AVEN].n_value != 0) { 604 double diff; 605 606 stringspace(); 607 #ifdef VMUNIX 608 lseek(kmem, (long)nl[NL_AVEN].n_value, 0); 609 read(kmem, avenrun, sizeof avenrun); 610 #endif 611 #ifdef pdp11 612 loadav(avenrun); 613 #endif 614 if ((diff = avenrun[0] - avenrun[1]) < 0.0) 615 stringprt("%.1f %.1f", avenrun[0], diff); 616 else 617 stringprt("%.1f +%.1f", avenrun[0], diff); 618 loadavg = avenrun[0]; /* remember load average */ 619 } 620 /* 621 * print log on and off information 622 */ 623 stringspace(); 624 fullprocess = 1; 625 #ifdef MAXLOAD 626 if (loadavg > MAXLOAD) 627 fullprocess = 0; /* too loaded to run */ 628 #endif 629 /* 630 * Read utmp file (logged in data) only if we are doing a full 631 * process, or if this is the first time and we are calculating 632 * the number of users. 633 */ 634 on = off = 0; 635 if (users == 0) { /* first time */ 636 if (readutmp(0)) 637 for (i = 0; i < nentries; i++) 638 if (old[i].ut_name[0]) 639 users++; 640 } else if (fullprocess && readutmp(1)) { 641 struct utmp *tmp; 642 643 users = 0; 644 for (i = 0; i < nentries; i++) { 645 if (strncmp(old[i].ut_name, 646 new[i].ut_name, NAMESIZE) == 0) 647 status[i] = NOCH; 648 else if (old[i].ut_name[0] == '\0') { 649 status[i] = ON; 650 on++; 651 } else if (new[i].ut_name[0] == '\0') { 652 status[i] = OFF; 653 off++; 654 } else { 655 status[i] = ON | OFF; 656 on++; 657 off++; 658 } 659 if (new[i].ut_name[0]) 660 users++; 661 } 662 tmp = new; 663 new = old; 664 old = tmp; 665 } 666 /* 667 * Print: 668 * 1. number of users 669 * 2. a * for unread mail 670 * 3. a - if load is too high 671 * 4. number of processes running and stopped 672 */ 673 stringprt("%du", users); 674 if (mailsize > 0 && mstbuf.st_mtime >= mstbuf.st_atime) 675 stringcat("*", -1); 676 if (!fullprocess && (proccheck || logcheck)) 677 stringcat("-", -1); 678 if (fullprocess && proccheck && readproctab()) { 679 register struct proc *p; 680 int procrun, procstop; 681 682 /* 683 * We are only interested in processes which have the same 684 * uid as us, and whose parent process id is not 1. 685 */ 686 procrun = procstop = 0; 687 for (p = proc; p < procNPROC; p++) { 688 if (p->p_stat == 0 || p->p_pgrp == 0 || 689 p->p_uid != uid || p->p_ppid == 1) 690 continue; 691 switch (p->p_stat) { 692 case SSTOP: 693 procstop++; 694 break; 695 case SSLEEP: 696 /* 697 * Sleep can mean waiting for a signal or just 698 * in a disk or page wait queue ready to run. 699 * We can tell if it is the later by the pri 700 * being negative. 701 */ 702 if (p->p_pri < PZERO) 703 procrun++; 704 break; 705 case SWAIT: 706 case SRUN: 707 case SIDL: 708 procrun++; 709 } 710 } 711 if (procrun > 0 || procstop > 0) { 712 stringspace(); 713 if (procrun > 0 && procstop > 0) 714 stringprt("%dr %ds", procrun, procstop); 715 else if (procrun > 0) 716 stringprt("%dr", procrun); 717 else 718 stringprt("%ds", procstop); 719 } 720 } 721 /* 722 * If anyone has logged on or off, and we are interested in it, 723 * print it out. 724 */ 725 if (logcheck) { 726 /* old and new have already been swapped */ 727 if (on) { 728 stringspace(); 729 stringcat("on:", -1); 730 for (i = 0; i < nentries; i++) 731 if (status[i] & ON) { 732 stringprt(" %.8s", old[i].ut_name); 733 ttyprint(old[i].ut_line); 734 } 735 } 736 if (off) { 737 stringspace(); 738 stringcat("off:", -1); 739 for (i = 0; i < nentries; i++) 740 if (status[i] & OFF) { 741 stringprt(" %.8s", new[i].ut_name); 742 ttyprint(new[i].ut_line); 743 } 744 } 745 } 746 bottom: 747 /* dump out what we know */ 748 stringdump(); 749 } 750 751 timeprint() 752 { 753 long curtime; 754 struct tm *tp, *localtime(); 755 static int beepable = 1; 756 757 /* always print time */ 758 time(&curtime); 759 tp = localtime(&curtime); 760 if (dateprint) 761 stringprt("%.11s", ctime(&curtime)); 762 stringprt("%d:%02d", tp->tm_hour > 12 ? tp->tm_hour - 12 : 763 (tp->tm_hour == 0 ? 12 : tp->tm_hour), tp->tm_min); 764 if (synch) /* sync with clock */ 765 delay = 60 - tp->tm_sec; 766 /* 767 * Beepable is used to insure that we get at most one set of beeps 768 * every half hour. 769 */ 770 if (beep) 771 if (beepable) { 772 if (tp->tm_min == 30) { 773 tputs(bell, 1, outc); 774 fflush(stdout); 775 beepable = 0; 776 } else if (tp->tm_min == 0) { 777 tputs(bell, 1, outc); 778 fflush(stdout); 779 sleep(2); 780 tputs(bell, 1, outc); 781 fflush(stdout); 782 beepable = 0; 783 } 784 } else 785 if (tp->tm_min != 0 && tp->tm_min != 30) 786 beepable = 1; 787 } 788 789 /* 790 * whocheck -- check for file named .who and print it on the who line first 791 */ 792 whocheck() 793 { 794 int chss; 795 register char *p; 796 char buff[81]; 797 int whofile; 798 799 if ((whofile = open(whofilename, 0)) < 0 && 800 (whofile = open(whofilename2, 0)) < 0) 801 return; 802 chss = read(whofile, buff, sizeof buff - 1); 803 close(whofile); 804 if (chss <= 0) 805 return; 806 buff[chss] = '\0'; 807 /* 808 * Remove all line feeds, and replace by spaces if they are within 809 * the message, else replace them by nulls. 810 */ 811 for (p = buff; *p;) 812 if (*p == '\n') 813 if (p[1]) 814 *p++ = ' '; 815 else 816 *p = '\0'; 817 else 818 p++; 819 stringcat(buff, p - buff); 820 stringspace(); 821 } 822 823 /* 824 * ttyprint -- given the name of a tty, print in the string buffer its 825 * short name surrounded by parenthesis. 826 * ttyxx is printed as (xx) 827 * console is printed as (cty) 828 */ 829 ttyprint(name) 830 char *name; 831 { 832 char buff[11]; 833 834 if (strncmp(name, "tty", 3) == 0) 835 stringprt("(%.*s)", LINESIZE - 3, name + 3); 836 else if (strcmp(name, "console") == 0) 837 stringcat("(cty)", -1); 838 else 839 stringprt("(%.*s)", LINESIZE, name); 840 } 841 842 /* 843 * mail checking function 844 * returns 0 if no mail seen 845 */ 846 mailseen() 847 { 848 FILE *mfd; 849 register n; 850 register char *cp; 851 char lbuf[100], sendbuf[100], *bufend; 852 char seenspace; 853 int retval = 0; 854 855 if (stat(username, &mstbuf) < 0) { 856 mailsize = 0; 857 return 0; 858 } 859 if (mstbuf.st_size <= mailsize || (mfd = fopen(username,"r")) == NULL) { 860 mailsize = mstbuf.st_size; 861 return 0; 862 } 863 fseek(mfd, mailsize, 0); 864 while ((n = readline(mfd, lbuf, sizeof lbuf)) >= 0 && 865 strncmp(lbuf, "From ", 5) != 0) 866 ; 867 if (n < 0) { 868 stringcat("Mail has just arrived", -1); 869 goto out; 870 } 871 retval = 1; 872 /* 873 * Found a From line, get second word, which is the sender, 874 * and print it. 875 */ 876 for (cp = lbuf + 5; *cp && *cp != ' '; cp++) /* skip to blank */ 877 ; 878 *cp = '\0'; /* terminate name */ 879 stringspace(); 880 stringprt("Mail from %s ", lbuf + 5); 881 /* 882 * Print subject, and skip over header. 883 */ 884 while ((n = readline(mfd, lbuf, sizeof lbuf)) > 0) 885 if (strncmp(lbuf, "Subject:", 8) == 0) 886 stringprt("on %s ", lbuf + 9); 887 if (!emacs) 888 stringcat(arrows, 2); 889 else 890 stringcat(": ", 2); 891 if (n < 0) /* already at eof */ 892 goto out; 893 /* 894 * Print as much of the letter as we can. 895 */ 896 cp = sendbuf; 897 if ((n = columns - chars) > sizeof sendbuf - 1) 898 n = sizeof sendbuf - 1; 899 bufend = cp + n; 900 seenspace = 0; 901 while ((n = readline(mfd, lbuf, sizeof lbuf)) >= 0) { 902 register char *rp; 903 904 if (strncmp(lbuf, "From ", 5) == 0) 905 break; 906 if (cp >= bufend) 907 continue; 908 if (!seenspace) { 909 *cp++ = ' '; /* space before lines */ 910 seenspace = 1; 911 } 912 rp = lbuf; 913 while (*rp && cp < bufend) 914 if (isspace(*rp)) { 915 if (!seenspace) { 916 *cp++ = ' '; 917 seenspace = 1; 918 } 919 rp++; 920 } else { 921 *cp++ = *rp++; 922 seenspace = 0; 923 } 924 } 925 *cp = 0; 926 stringcat(sendbuf, -1); 927 /* 928 * Want to update write time so a star will 929 * appear after the number of users until the 930 * user reads his mail. 931 */ 932 out: 933 mailsize = linebeg; 934 fclose(mfd); 935 touch(username); 936 return retval; 937 } 938 939 /* 940 * readline -- read a line from fp and store it in buf. 941 * return the number of characters read. 942 */ 943 readline(fp, buf, n) 944 register FILE *fp; 945 char *buf; 946 register n; 947 { 948 register c; 949 register char *cp = buf; 950 951 linebeg = ftell(fp); /* remember loc where line begins */ 952 cp = buf; 953 while (--n > 0 && (c = getc(fp)) != EOF && c != '\n') 954 *cp++ = c; 955 *cp = 0; 956 if (c == EOF && cp - buf == 0) 957 return -1; 958 return cp - buf; 959 } 960 961 962 /* 963 * string hacking functions 964 */ 965 966 stringinit() 967 { 968 sp = strarr; 969 chars = 0; 970 } 971 972 /*VARARGS1*/ 973 stringprt(format, a, b, c) 974 char *format; 975 { 976 char tempbuf[150]; 977 978 (void)sprintf(tempbuf, format, a, b, c); 979 stringcat(tempbuf, -1); 980 } 981 982 stringdump() 983 { 984 char bigbuf[sizeof strarr + 200]; 985 register char *bp = bigbuf; 986 register int i; 987 988 if (!emacs) { 989 if (sawmail) 990 bp = strcpy1(bp, bell); 991 if (eslok) 992 bp = strcpy1(bp, tparm(to_status_line, 993 leftline ? 0 : columns - chars)); 994 else { 995 bp = strcpy1(bp, to_status_line); 996 if (!shortline && !leftline) 997 for (i = columns - chars; --i >= 0;) 998 *bp++ = ' '; 999 } 1000 if (reverse && revtime != 0) 1001 bp = strcpy1(bp, rev_out); 1002 } 1003 *sp = 0; 1004 bp = strcpy1(bp, strarr); 1005 if (!emacs) { 1006 if (reverse) 1007 bp = strcpy1(bp, rev_end); 1008 bp = strcpy1(bp, from_status_line); 1009 if (sawmail) 1010 bp = strcpy1(strcpy1(bp, bell), bell); 1011 *bp = 0; 1012 tputs(bigbuf, 1, outc); 1013 if (mustclear) { 1014 mustclear = 0; 1015 tputs(clr_eol, 1, outc); 1016 } 1017 if (dbug) 1018 putchar('\n'); 1019 fflush(stdout); 1020 } else 1021 write(2, bigbuf, bp - bigbuf); 1022 } 1023 1024 stringspace() 1025 { 1026 if (reverse && revtime != 0) { 1027 #ifdef TERMINFO 1028 stringcat(rev_end, 1029 magic_cookie_glitch <= 0 ? 0 : magic_cookie_glitch); 1030 stringcat(" ", 1); 1031 stringcat(rev_out, 1032 magic_cookie_glitch <= 0 ? 0 : magic_cookie_glitch); 1033 #else 1034 stringcat(rev_end, 0); 1035 stringcat(" ", 1); 1036 stringcat(rev_out, 0); 1037 #endif TERMINFO 1038 } else 1039 stringcat(" ", 1); 1040 } 1041 1042 /* 1043 * stringcat :: concatenate the characters in string str to the list we are 1044 * building to send out. 1045 * str - the string to print. may contain funny (terminal control) chars. 1046 * n - the number of printable characters in the string 1047 * or if -1 then str is all printable so we can truncate it, 1048 * otherwise don't print only half a string. 1049 */ 1050 stringcat(str, n) 1051 register char *str; 1052 register n; 1053 { 1054 register char *p = sp; 1055 1056 if (n < 0) { /* truncate */ 1057 n = columns - chars; 1058 while ((*p++ = *str++) && --n >= 0) 1059 ; 1060 p--; 1061 chars += p - sp; 1062 sp = p; 1063 } else if (chars + n <= columns) { /* don't truncate */ 1064 while (*p++ = *str++) 1065 ; 1066 chars += n; 1067 sp = p - 1; 1068 } 1069 } 1070 1071 /* 1072 * touch :: update the modify time of a file. 1073 */ 1074 touch(name) 1075 char *name; /* name of file */ 1076 { 1077 register fd; 1078 char buf; 1079 1080 if ((fd = open(name, 2)) >= 0) { 1081 read(fd, &buf, 1); /* get first byte */ 1082 lseek(fd, 0L, 0); /* go to beginning */ 1083 write(fd, &buf, 1); /* and rewrite first byte */ 1084 close(fd); 1085 } 1086 } 1087 1088 1089 /* 1090 * clearbotl :: clear bottom line. 1091 * called when process quits or is killed. 1092 * it clears the bottom line of the terminal. 1093 */ 1094 clearbotl() 1095 { 1096 register int fd; 1097 int exit(); 1098 1099 signal(SIGALRM, exit); 1100 alarm(30); /* if can't open in 30 secs, just die */ 1101 if (!emacs && (fd = open(ourtty, 1)) >= 0) { 1102 write(fd, dis_status_line, strlen(dis_status_line)); 1103 close(fd); 1104 } 1105 #ifdef PROF 1106 if (chdir("/usr/src/ucb/sysline") < 0) 1107 (void) chdir("/tmp"); 1108 #endif 1109 exit(0); 1110 } 1111 1112 #ifdef TERMINFO 1113 initterm() 1114 { 1115 static char standbuf[40]; 1116 1117 setupterm(0, 1, 0); 1118 if (!window && !has_status_line) { 1119 /* not an appropriate terminal */ 1120 if (!quiet) 1121 fprintf(stderr, "sysline: no status capability for %s\n", 1122 getenv("TERM")); 1123 exit(1); 1124 } 1125 if (window || status_line_esc_ok) { 1126 if (set_attributes) { 1127 /* reverse video mode */ 1128 strcpy(standbuf, 1129 tparm(set_attributes,0,0,1,0,0,0,0,0,0)); 1130 rev_out = standbuf; 1131 rev_end = exit_attribute_mode; 1132 } else if (enter_standout_mode && exit_standout_mode) { 1133 rev_out = enter_standout_mode; 1134 rev_end = exit_standout_mode; 1135 } else 1136 rev_out = rev_end = ""; 1137 } else 1138 rev_out = rev_end = ""; 1139 columns--; /* avoid cursor wraparound */ 1140 } 1141 1142 #else /* TERMCAP */ 1143 1144 initterm() 1145 { 1146 char *term, *cp; 1147 static char tbuf[1024]; 1148 char is2[40]; 1149 extern char *UP; 1150 1151 if ((term = getenv("TERM")) == NULL) { 1152 if (!quiet) 1153 fprintf(stderr, 1154 "sysline: No TERM variable in enviroment\n"); 1155 exit(1); 1156 } 1157 if (tgetent(tbuf, term) <= 0) { 1158 if (!quiet) 1159 fprintf(stderr, 1160 "sysline: Unknown terminal type: %s\n", term); 1161 exit(1); 1162 } 1163 if (!window && tgetflag("hs") <= 0) { 1164 if (!strncmp(term, "h19", 3)) { 1165 /* for upward compatability with h19sys */ 1166 strcpy(to_status_line, 1167 "\033j\033x5\033x1\033Y8%+ \033o"); 1168 strcpy(from_status_line, "\033k\033y5"); 1169 strcpy(dis_status_line, "\033y1"); 1170 strcpy(rev_out, "\033p"); 1171 strcpy(rev_end, "\033q"); 1172 arrows = "\033Fhh\033G"; 1173 columns = 80; 1174 UP = "\b"; 1175 return; 1176 } 1177 if (!quiet) 1178 fprintf(stderr, 1179 "sysline: No status capability for %s\n", term); 1180 exit(1); 1181 } 1182 cp = is2; 1183 if (tgetstr("i2", &cp) != NULL) { 1184 /* someday tset will do this */ 1185 tputs(is2, 1, erroutc); 1186 fflush(stdout); 1187 } 1188 1189 /* the "-1" below is to avoid cursor wraparound problems */ 1190 columns = tgetnum("ws"); 1191 hasws = columns >= 0; 1192 if (!hasws) 1193 columns = tgetnum("co"); 1194 columns -= 1; 1195 if (window) { 1196 strcpy(to_status_line, "\r"); 1197 cp = dis_status_line; /* use the clear line sequence */ 1198 *cp++ = '\r'; 1199 tgetstr("ce", &cp); 1200 if (leftline) 1201 strcpy(from_status_line, dis_status_line + 1); 1202 else 1203 strcpy(from_status_line, ""); 1204 } else { 1205 cp = to_status_line; 1206 tgetstr("ts", &cp); 1207 cp = from_status_line; 1208 tgetstr("fs", &cp); 1209 cp = dis_status_line; 1210 tgetstr("ds", &cp); 1211 eslok = tgetflag("es"); 1212 } 1213 if (eslok || window) { 1214 cp = rev_out; 1215 tgetstr("so", &cp); 1216 cp = rev_end; 1217 tgetstr("se", &cp); 1218 cp = clr_eol; 1219 tgetstr("ce", &cp); 1220 } else 1221 reverse = 0; /* turn off reverse video */ 1222 UP = "\b"; 1223 if (!strncmp(term, "h19", 3)) 1224 arrows = "\033Fhh\033G"; /* "two tiny graphic arrows" */ 1225 else 1226 arrows = "->"; 1227 } 1228 #endif TERMINFO 1229 1230 #ifdef pdp11 1231 loadav(ap) 1232 double ap[]; 1233 { 1234 register int i; 1235 short s_avenrun[3]; 1236 1237 lseek(kmem, (long)nl[NL_AVEN].n_value, 0); 1238 read(kmem, s_avenrun, sizeof(s_avenrun)); 1239 for (i=0; i < (sizeof(s_avenrun)/sizeof(s_avenrun[0])); i++) 1240 ap[i] = s_avenrun[i] / 256.0; 1241 } 1242 #endif 1243 1244 #ifdef RWHO 1245 char * 1246 sysrup(hp) 1247 register struct remotehost *hp; 1248 { 1249 char filename[100]; 1250 struct whod wd; 1251 #define WHOD_HDR_SIZE (sizeof (wd) - sizeof (wd.wd_we)) 1252 static char buffer[50]; 1253 time_t now; 1254 1255 /* 1256 * rh_file is initially 0. 1257 * This is ok since standard input is assumed to exist. 1258 */ 1259 if (hp->rh_file == 0) { 1260 /* 1261 * Try rwho hostname file, and if that fails try ucbhostname. 1262 */ 1263 (void) strcpy1(strcpy1(filename, RWHOLEADER), hp->rh_host); 1264 if ((hp->rh_file = open(filename, 0)) < 0) { 1265 (void) strcpy1(strcpy1(strcpy1(filename, RWHOLEADER), 1266 NETPREFIX), hp->rh_host); 1267 hp->rh_file = open(filename, 0); 1268 } 1269 } 1270 if (hp->rh_file < 0) { 1271 (void) sprintf(buffer, "%s?", hp->rh_host); 1272 return(buffer); 1273 } 1274 (void) lseek(hp->rh_file, (off_t)0, 0); 1275 if (read(hp->rh_file, (char *)&wd, WHOD_HDR_SIZE) != WHOD_HDR_SIZE) { 1276 (void) sprintf(buffer, "%s ?", hp->rh_host); 1277 return(buffer); 1278 } 1279 (void) time(&now); 1280 if (now - wd.wd_recvtime > DOWN_THRESHOLD) { 1281 long interval; 1282 long days, hours, minutes; 1283 1284 interval = now - wd.wd_recvtime; 1285 minutes = (interval + 59) / 60; /* round to minutes */ 1286 hours = minutes / 60; /* extract hours from minutes */ 1287 minutes %= 60; /* remove hours from minutes */ 1288 days = hours / 24; /* extract days from hours */ 1289 hours %= 24; /* remove days from hours */ 1290 if (days > 7 || days < 0) 1291 (void) sprintf(buffer, "%s down", hp->rh_host); 1292 else if (days > 0) 1293 (void) sprintf(buffer, "%s %d+%d:%02d", 1294 hp->rh_host, days, hours, minutes); 1295 else 1296 (void) sprintf(buffer, "%s %d:%02d", 1297 hp->rh_host, hours, minutes); 1298 } else 1299 (void) sprintf(buffer, "%s %.1f", 1300 hp->rh_host, wd.wd_loadav[0]/100.0); 1301 return buffer; 1302 } 1303 #endif RWHO 1304 1305 getwinsize() 1306 { 1307 #ifdef TIOCGWINSZ 1308 struct winsize winsize; 1309 1310 /* the "-1" below is to avoid cursor wraparound problems */ 1311 if (!hasws && ioctl(2, TIOCGWINSZ, (char *)&winsize) >= 0 && 1312 winsize.ws_col != 0) 1313 columns = winsize.ws_col - 1; 1314 #endif 1315 } 1316 1317 #ifdef SIGWINCH 1318 sigwinch() 1319 { 1320 winchanged++; 1321 } 1322 #endif 1323 1324 char * 1325 strcpy1(p, q) 1326 register char *p, *q; 1327 { 1328 1329 while (*p++ = *q++) 1330 ; 1331 return p - 1; 1332 } 1333 1334 outc(c) 1335 char c; 1336 { 1337 if (dbug) 1338 printf("%s", unctrl(c)); 1339 else 1340 putchar(c); 1341 } 1342 1343 erroutc(c) 1344 char c; 1345 { 1346 if (dbug) 1347 fprintf(stderr, "%s", unctrl(c)); 1348 else 1349 putc(c, stderr); 1350 } 1351