1: /*
   2:  * Copyright (c) 1980 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(DOSCCS) && !defined(lint)
   8: char copyright[] =
   9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  10:  All rights reserved.\n";
  11: 
  12: static char sccsid[] = "@(#)wall.c	5.3.2 (2.11BSD GTE) 1996/11/16";
  13: #endif
  14: 
  15: /*
  16:  * wall.c - Broadcast a message to all users.
  17:  *
  18:  * This program is not related to David Wall, whose Stanford Ph.D. thesis
  19:  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
  20:  */
  21: 
  22: #include <stdio.h>
  23: #include <utmp.h>
  24: #include <errno.h>
  25: #include <signal.h>
  26: #include <sys/time.h>
  27: #include <fcntl.h>
  28: #include <sys/types.h>
  29: #include <sys/stat.h>
  30: 
  31: #define IGNOREUSER  "sleeper"
  32: 
  33: char    hostname[32];
  34: char    mesg[3000];
  35: int msize,sline;
  36: struct  utmp *utmp;
  37: char    *strcpy();
  38: char    *strcat();
  39: char    *malloc();
  40: char    who[UT_NAMESIZE + 1] = "???";
  41: long    clock, time();
  42: struct tm *localtime();
  43: struct tm *localclock;
  44: 
  45: extern  errno;
  46: 
  47: main(argc, argv)
  48: char *argv[];
  49: {
  50:     register int i, c;
  51:     register struct utmp *p;
  52:     int f;
  53:     struct stat statb;
  54: 
  55:     (void) gethostname(hostname, sizeof (hostname));
  56:     if ((f = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
  57:         fprintf(stderr, "Cannot open %s\n", _PATH_UTMP);
  58:         exit(1);
  59:     }
  60:     clock = time( 0 );
  61:     localclock = localtime( &clock );
  62:     sline = ttyslot();  /* 'utmp' slot no. of sender */
  63:     (void) fstat(f, &statb);
  64:     utmp = (struct utmp *)malloc((unsigned)statb.st_size);
  65:     c = read(f, (char *)utmp, (int)statb.st_size);
  66:     (void) close(f);
  67:     c /= sizeof(struct utmp);
  68:     if (sline)
  69:         strncpy(who, utmp[sline].ut_name, sizeof(utmp[sline].ut_name));
  70:     sprintf(mesg,
  71:         "\n\007\007Broadcast Message from %s@%s (%.*s) at %d:%02d ...\r\n\n"
  72:         , who
  73:         , hostname
  74:         , sizeof(utmp[sline].ut_line)
  75:         , utmp[sline].ut_line
  76:         , localclock -> tm_hour
  77:         , localclock -> tm_min
  78:     );
  79:     msize = strlen(mesg);
  80:     if (argc >= 2) {
  81:         /* take message from unix file instead of standard input */
  82:         if (freopen(argv[1], "r", stdin) == NULL) {
  83:             perror(argv[1]);
  84:             exit(1);
  85:         }
  86:     }
  87:     while ((i = getchar()) != EOF) {
  88:         if (i == '\n')
  89:             mesg[msize++] = '\r';
  90:         if (msize >= sizeof mesg) {
  91:             fprintf(stderr, "Message too long\n");
  92:             exit(1);
  93:         }
  94:         mesg[msize++] = i;
  95:     }
  96:     fclose(stdin);
  97:     for (i=0; i<c; i++) {
  98:         p = &utmp[i];
  99:         if (p->ut_name[0] == 0 ||
 100:             strncmp(p->ut_name, IGNOREUSER, sizeof(p->ut_name)) == 0)
 101:             continue;
 102:         sendmes(p->ut_line);
 103:     }
 104:     exit(0);
 105: }
 106: 
 107: sendmes(tty)
 108: char *tty;
 109: {
 110:     register f, flags;
 111:     static char t[50] = "/dev/";
 112:     int e, i;
 113: 
 114:     strcpy(t + 5, tty);
 115: 
 116:     if ((f = open(t, O_WRONLY|O_NDELAY)) < 0) {
 117:         if (errno != EWOULDBLOCK)
 118:             perror(t);
 119:         return;
 120:     }
 121:     if ((flags = fcntl(f, F_GETFL, 0)) == -1) {
 122:         perror(t);
 123:         return;
 124:     }
 125:     if (fcntl(f, F_SETFL, flags | FNDELAY) == -1)
 126:         goto oldway;
 127:     i = write(f, mesg, msize);
 128:     e = errno;
 129:     (void) fcntl(f, F_SETFL, flags);
 130:     if (i == msize) {
 131:         (void) close(f);
 132:         return;
 133:     }
 134:     if (e != EWOULDBLOCK) {
 135:         errno = e;
 136:         perror(t);
 137:         (void) close(f);
 138:         return;
 139:     }
 140: oldway:
 141:     while ((i = fork()) == -1)
 142:         if (wait((int *)0) == -1) {
 143:             fprintf(stderr, "Try again\n");
 144:             return;
 145:         }
 146:     if (i) {
 147:         (void) close(f);
 148:         return;
 149:     }
 150: 
 151:     (void) write(f, mesg, msize);
 152:     exit(0);
 153: }

Defined functions

main defined in line 47; never used
sendmes defined in line 107; used 1 times

Defined variables

clock defined in line 41; used 2 times
copyright defined in line 8; never used
hostname defined in line 33; used 3 times
localclock defined in line 43; used 3 times
mesg defined in line 34; used 7 times
msize defined in line 35; used 7 times
sccsid defined in line 12; never used
sline defined in line 35; used 6 times
utmp defined in line 36; used 7 times
who defined in line 40; used 2 times

Defined macros

IGNOREUSER defined in line 31; used 1 times
Last modified: 1996-11-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3779
Valid CSS Valid XHTML 1.0 Strict