1*13594Swnj static char *sccsid = "@(#)wall.c 4.7 (Berkeley) 83/07/01"; 21159Sbill /* 31159Sbill * wall.c - Broadcast a message to all users. 41159Sbill * 51159Sbill * This program is not related to David Wall, whose Stanford Ph.D. thesis 61159Sbill * is entitled "Mechanisms for Broadcast and Selective Broadcast". 71159Sbill */ 81159Sbill 91159Sbill #include <stdio.h> 101159Sbill #include <utmp.h> 11*13594Swnj #include <sys/time.h> 123176Swnj #include <signal.h> 131159Sbill #define USERS 128 143884Sroot #define IGNOREUSER "sleeper" 151159Sbill 166200Sroot char hostname[32]; 171159Sbill char mesg[3000]; 181159Sbill int msize,sline; 191159Sbill struct utmp utmp[USERS]; 201159Sbill char *strcpy(); 211159Sbill char *strcat(); 221159Sbill char who[9] = "???"; 233660Smark long clock, time(); 241159Sbill struct tm *localtime(); 251159Sbill struct tm *localclock; 261159Sbill 271159Sbill main(argc, argv) 281159Sbill char *argv[]; 291159Sbill { 301159Sbill register i; 311159Sbill register char c; 321159Sbill register struct utmp *p; 331159Sbill FILE *f; 343176Swnj FILE *mf; 351159Sbill 366200Sroot gethostname(hostname, sizeof (hostname)); 371159Sbill if((f = fopen("/etc/utmp", "r")) == NULL) { 381159Sbill fprintf(stderr, "Cannot open /etc/utmp\n"); 391159Sbill exit(1); 401159Sbill } 411159Sbill clock = time( 0 ); 421159Sbill localclock = localtime( &clock ); 433176Swnj mf = stdin; 441159Sbill if(argc >= 2) { 451159Sbill /* take message from unix file instead of standard input */ 463176Swnj if((mf = fopen(argv[1], "r")) == NULL) { 471159Sbill fprintf(stderr,"Cannot open %s\n", argv[1]); 481159Sbill exit(1); 491159Sbill } 501159Sbill } 513176Swnj while((i = getc(mf)) != EOF) { 523176Swnj if (msize >= sizeof mesg) { 533176Swnj fprintf(stderr, "Message too long\n"); 543176Swnj exit(1); 553176Swnj } 563176Swnj mesg[msize++] = i; 573176Swnj } 583176Swnj fclose(mf); 593176Swnj sline = ttyslot(2); /* 'utmp' slot no. of sender */ 603176Swnj fread((char *)utmp, sizeof(struct utmp), USERS, f); 611159Sbill fclose(f); 623176Swnj if (sline) 633176Swnj strncpy(who, utmp[sline].ut_name, sizeof(utmp[sline].ut_name)); 641159Sbill for(i=0; i<USERS; i++) { 651159Sbill p = &utmp[i]; 663884Sroot if ((p->ut_name[0] == 0) || 673884Sroot (strncmp (p->ut_name, IGNOREUSER, sizeof(p->ut_name)) == 0)) 681159Sbill continue; 691159Sbill sendmes(p->ut_line); 701159Sbill } 711159Sbill exit(0); 721159Sbill } 731159Sbill 741159Sbill sendmes(tty) 751159Sbill char *tty; 761159Sbill { 771159Sbill register i; 781159Sbill char t[50], buf[BUFSIZ]; 791159Sbill register char *cp; 801159Sbill register int c, ch; 811159Sbill FILE *f; 821159Sbill 833176Swnj while ((i = fork()) == -1) 843176Swnj if (wait((int *)0) == -1) { 853176Swnj fprintf(stderr, "Try again\n"); 863176Swnj return; 873176Swnj } 881159Sbill if(i) 891159Sbill return; 901159Sbill strcpy(t, "/dev/"); 911159Sbill strcat(t, tty); 921159Sbill 933176Swnj signal(SIGALRM, SIG_DFL); /* blow away if open hangs */ 943176Swnj alarm(10); 953176Swnj 961159Sbill if((f = fopen(t, "w")) == NULL) { 971159Sbill fprintf(stderr,"cannot open %s\n", t); 981159Sbill exit(1); 991159Sbill } 1001159Sbill setbuf(f, buf); 1013176Swnj fprintf(f, 1023176Swnj "\nBroadcast Message from %s!%s (%.*s) at %d:%02d ...\r\n\n" 1036200Sroot , hostname 1043176Swnj , who 1053176Swnj , sizeof(utmp[sline].ut_line) 1063176Swnj , utmp[sline].ut_line 1073176Swnj , localclock -> tm_hour 1083176Swnj , localclock -> tm_min 1093176Swnj ); 1101159Sbill /* fwrite(mesg, msize, 1, f); */ 1111159Sbill for (cp = mesg, c = msize; c-- > 0; cp++) { 1121159Sbill ch = *cp; 1131159Sbill if (ch == '\n') 1141159Sbill putc('\r', f); 1151159Sbill putc(ch, f); 1161159Sbill } 1171159Sbill 1181159Sbill /* 1191159Sbill * Bitchin'. 1201159Sbill */ 1211159Sbill 1221159Sbill exit(0); 1231159Sbill } 124