1: static char *sccsid = "@(#)wall.c	4.6 (Berkeley) 82/03/15";
   2: /*
   3:  * wall.c - Broadcast a message to all users.
   4:  *
   5:  * This program is not related to David Wall, whose Stanford Ph.D. thesis
   6:  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
   7:  */
   8: 
   9: #include <stdio.h>
  10: #include <utmp.h>
  11: #include <time.h>
  12: #include <signal.h>
  13: #define USERS   128
  14: #define IGNOREUSER  "sleeper"
  15: 
  16: char    mesg[3000];
  17: int msize,sline;
  18: struct  utmp utmp[USERS];
  19: char    *strcpy();
  20: char    *strcat();
  21: char who[9] = "???";
  22: long    clock, time();
  23: struct tm *localtime();
  24: struct tm *localclock;
  25: 
  26: main(argc, argv)
  27: char *argv[];
  28: {
  29:     register i;
  30:     register char c;
  31:     register struct utmp *p;
  32:     FILE *f;
  33:     FILE *mf;
  34: 
  35:     if((f = fopen("/etc/utmp", "r")) == NULL) {
  36:         fprintf(stderr, "Cannot open /etc/utmp\n");
  37:         exit(1);
  38:     }
  39:     clock = time( 0 );
  40:     localclock = localtime( &clock );
  41:     mf = stdin;
  42:     if(argc >= 2) {
  43:         /* take message from unix file instead of standard input */
  44:         if((mf = fopen(argv[1], "r")) == NULL) {
  45:             fprintf(stderr,"Cannot open %s\n", argv[1]);
  46:             exit(1);
  47:         }
  48:     }
  49:     while((i = getc(mf)) != EOF) {
  50:         if (msize >= sizeof mesg) {
  51:             fprintf(stderr, "Message too long\n");
  52:             exit(1);
  53:         }
  54:         mesg[msize++] = i;
  55:     }
  56:     fclose(mf);
  57:     sline = ttyslot(2); /* 'utmp' slot no. of sender */
  58:     fread((char *)utmp, sizeof(struct utmp), USERS, f);
  59:     fclose(f);
  60:     if (sline)
  61:         strncpy(who, utmp[sline].ut_name, sizeof(utmp[sline].ut_name));
  62:     for(i=0; i<USERS; i++) {
  63:         p = &utmp[i];
  64:         if ((p->ut_name[0] == 0) ||
  65:             (strncmp (p->ut_name, IGNOREUSER, sizeof(p->ut_name)) == 0))
  66:             continue;
  67:         sendmes(p->ut_line);
  68:     }
  69:     exit(0);
  70: }
  71: 
  72: sendmes(tty)
  73: char *tty;
  74: {
  75:     register i;
  76:     char t[50], buf[BUFSIZ];
  77:     register char *cp;
  78:     register int c, ch;
  79:     FILE *f;
  80: 
  81:     while ((i = fork()) == -1)
  82:         if (wait((int *)0) == -1) {
  83:             fprintf(stderr, "Try again\n");
  84:             return;
  85:         }
  86:     if(i)
  87:         return;
  88:     strcpy(t, "/dev/");
  89:     strcat(t, tty);
  90: 
  91:     signal(SIGALRM, SIG_DFL);   /* blow away if open hangs */
  92:     alarm(10);
  93: 
  94:     if((f = fopen(t, "w")) == NULL) {
  95:         fprintf(stderr,"cannot open %s\n", t);
  96:         exit(1);
  97:     }
  98:     setbuf(f, buf);
  99:     fprintf(f,
 100:         "\nBroadcast Message from %s (%.*s) at %d:%02d ...\r\n\n"
 101:         , who
 102:         , sizeof(utmp[sline].ut_line)
 103:         , utmp[sline].ut_line
 104:         , localclock -> tm_hour
 105:         , localclock -> tm_min
 106:     );
 107:     /* fwrite(mesg, msize, 1, f); */
 108:     for (cp = mesg, c = msize; c-- > 0; cp++) {
 109:         ch = *cp;
 110:         if (ch == '\n')
 111:             putc('\r', f);
 112:         putc(ch, f);
 113:     }
 114: 
 115:     /*
 116: 	 * Bitchin'.
 117: 	 */
 118: 
 119:     exit(0);
 120: }

Defined functions

main defined in line 26; never used
sendmes defined in line 72; used 1 times
  • in line 67

Defined variables

clock defined in line 22; used 2 times
localclock defined in line 24; used 3 times
mesg defined in line 16; used 3 times
msize defined in line 17; used 3 times
sccsid defined in line 1; never used
sline defined in line 17; used 6 times
utmp defined in line 18; used 6 times
who defined in line 21; used 2 times

Defined macros

IGNOREUSER defined in line 14; used 1 times
  • in line 65
USERS defined in line 13; used 3 times
Last modified: 1983-06-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 894
Valid CSS Valid XHTML 1.0 Strict