1 static char *sccsid = "@(#)wall.c 4.4 (Berkeley) 81/05/06"; 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 <whoami.h> 13 #include <signal.h> 14 #define USERS 128 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 continue; 66 /*** this might be nice, but utmp gets so out of date !! 67 sleep(1); 68 ***/ 69 sendmes(p->ut_line); 70 } 71 exit(0); 72 } 73 74 sendmes(tty) 75 char *tty; 76 { 77 register i; 78 char t[50], buf[BUFSIZ]; 79 register char *cp; 80 register int c, ch; 81 FILE *f; 82 83 /*** you can't do this with lots of users & MAXUPROC 84 i = fork(); 85 if(i == -1) { 86 fprintf(stderr, "Try again\n"); 87 return; 88 } 89 ***/ 90 while ((i = fork()) == -1) 91 if (wait((int *)0) == -1) { 92 fprintf(stderr, "Try again\n"); 93 return; 94 } 95 if(i) 96 return; 97 strcpy(t, "/dev/"); 98 strcat(t, tty); 99 100 signal(SIGALRM, SIG_DFL); /* blow away if open hangs */ 101 alarm(10); 102 103 if((f = fopen(t, "w")) == NULL) { 104 fprintf(stderr,"cannot open %s\n", t); 105 exit(1); 106 } 107 setbuf(f, buf); 108 fprintf(f, 109 "\nBroadcast Message from %s!%s (%.*s) at %d:%02d ...\r\n\n" 110 , sysname 111 , who 112 , sizeof(utmp[sline].ut_line) 113 , utmp[sline].ut_line 114 , localclock -> tm_hour 115 , localclock -> tm_min 116 ); 117 /* fwrite(mesg, msize, 1, f); */ 118 for (cp = mesg, c = msize; c-- > 0; cp++) { 119 ch = *cp; 120 if (ch == '\n') 121 putc('\r', f); 122 putc(ch, f); 123 } 124 125 /* 126 * Bitchin'. 127 */ 128 129 exit(0); 130 } 131