|
|
1.1 ! root 1: static char *sccsid = "@(#)write.c 4.2 (Berkeley) 11/10/80"; ! 2: /* ! 3: * write to another user ! 4: */ ! 5: ! 6: #include <stdio.h> ! 7: #include <sys/types.h> ! 8: #include <sys/stat.h> ! 9: #include <signal.h> ! 10: #include <utmp.h> ! 11: #include <time.h> ! 12: ! 13: #define NMAX sizeof(ubuf.ut_name) ! 14: #define LMAX sizeof(ubuf.ut_line) ! 15: ! 16: char *strcat(); ! 17: char *strcpy(); ! 18: struct utmp ubuf; ! 19: int signum[] = {SIGHUP, SIGINT, SIGQUIT, 0}; ! 20: char me[10] = "???"; ! 21: char *him; ! 22: char *mytty; ! 23: char histty[32]; ! 24: char *histtya; ! 25: char *ttyname(); ! 26: char *rindex(); ! 27: int logcnt; ! 28: int eof(); ! 29: int timout(); ! 30: FILE *tf; ! 31: char *getenv(); ! 32: ! 33: main(argc, argv) ! 34: char *argv[]; ! 35: { ! 36: struct stat stbuf; ! 37: register i; ! 38: register FILE *uf; ! 39: int c1, c2; ! 40: long clock = time( 0 ); ! 41: struct tm *localtime(); ! 42: struct tm *localclock = localtime( &clock ); ! 43: ! 44: if(argc < 2) { ! 45: printf("usage: write user [ttyname]\n"); ! 46: exit(1); ! 47: } ! 48: him = argv[1]; ! 49: if(argc > 2) ! 50: histtya = argv[2]; ! 51: if ((uf = fopen("/etc/utmp", "r")) == NULL) { ! 52: printf("cannot open /etc/utmp\n"); ! 53: goto cont; ! 54: } ! 55: mytty = ttyname(2); ! 56: if (mytty == NULL) { ! 57: printf("Can't find your tty\n"); ! 58: exit(1); ! 59: } ! 60: mytty = rindex(mytty, '/') + 1; ! 61: if (histtya) { ! 62: strcpy(histty, "/dev/"); ! 63: strcat(histty, histtya); ! 64: } ! 65: while (fread((char *)&ubuf, sizeof(ubuf), 1, uf) == 1) { ! 66: if (ubuf.ut_name[0] == '\0') ! 67: continue; ! 68: if (strcmp(ubuf.ut_line, mytty)==0) { ! 69: for(i=0; i<NMAX; i++) { ! 70: c1 = ubuf.ut_name[i]; ! 71: if(c1 == ' ') ! 72: c1 = 0; ! 73: me[i] = c1; ! 74: if(c1 == 0) ! 75: break; ! 76: } ! 77: } ! 78: if(him[0] != '-' || him[1] != 0) ! 79: for(i=0; i<NMAX; i++) { ! 80: c1 = him[i]; ! 81: c2 = ubuf.ut_name[i]; ! 82: if(c1 == 0) ! 83: if(c2 == 0 || c2 == ' ') ! 84: break; ! 85: if(c1 != c2) ! 86: goto nomat; ! 87: } ! 88: logcnt++; ! 89: if (histty[0]==0) { ! 90: strcpy(histty, "/dev/"); ! 91: strcat(histty, ubuf.ut_line); ! 92: } ! 93: nomat: ! 94: ; ! 95: } ! 96: cont: ! 97: if (logcnt==0 && histty[0]=='\0') { ! 98: printf("%s not logged in.\n", him); ! 99: exit(1); ! 100: } ! 101: fclose(uf); ! 102: if (histtya==0 && logcnt > 1) { ! 103: printf("%s logged more than once\nwriting to %s\n", him, histty+5); ! 104: } ! 105: if(histty[0] == 0) { ! 106: printf(him); ! 107: if(logcnt) ! 108: printf(" not on that tty\n"); else ! 109: printf(" not logged in\n"); ! 110: exit(1); ! 111: } ! 112: if (access(histty, 0) < 0) { ! 113: printf("No such tty\n"); ! 114: exit(1); ! 115: } ! 116: signal(SIGALRM, timout); ! 117: alarm(5); ! 118: if ((tf = fopen(histty, "w")) == NULL) ! 119: goto perm; ! 120: alarm(0); ! 121: if (fstat(fileno(tf), &stbuf) < 0) ! 122: goto perm; ! 123: if ((stbuf.st_mode&02) == 0) ! 124: goto perm; ! 125: sigs(eof); ! 126: fprintf(tf, "\r\nMessage from "); ! 127: #ifdef interdata ! 128: fprintf(tf, "(Interdata) " ); ! 129: #endif ! 130: fprintf(tf, "%s on %s at %d:%02d ...\r\n" ! 131: , me, mytty , localclock -> tm_hour , localclock -> tm_min ); ! 132: fflush(tf); ! 133: for(;;) { ! 134: char buf[128]; ! 135: i = read(0, buf, 128); ! 136: if(i <= 0) ! 137: eof(); ! 138: if(buf[0] == '!') { ! 139: buf[i] = 0; ! 140: ex(buf); ! 141: continue; ! 142: } ! 143: write(fileno(tf), buf, i); ! 144: if ( buf[ i - 1 ] == '\n' ) ! 145: write( fileno( tf ) , "\r" , 1 ); ! 146: } ! 147: ! 148: perm: ! 149: printf("Permission denied\n"); ! 150: exit(1); ! 151: } ! 152: ! 153: timout() ! 154: { ! 155: ! 156: printf("Timeout opening their tty\n"); ! 157: exit(1); ! 158: } ! 159: ! 160: eof() ! 161: { ! 162: ! 163: fprintf(tf, "EOF\r\n"); ! 164: exit(0); ! 165: } ! 166: ! 167: ex(bp) ! 168: char *bp; ! 169: { ! 170: register i; ! 171: ! 172: sigs(SIG_IGN); ! 173: i = fork(); ! 174: if(i < 0) { ! 175: printf("Try again\n"); ! 176: goto out; ! 177: } ! 178: if(i == 0) { ! 179: sigs((int (*)())0); ! 180: execl(getenv("SHELL") ? getenv("SHELL") : "/bin/sh", "sh", "-c", bp+1, 0); ! 181: exit(0); ! 182: } ! 183: while(wait((int *)NULL) != i) ! 184: ; ! 185: printf("!\n"); ! 186: out: ! 187: sigs(eof); ! 188: } ! 189: ! 190: sigs(sig) ! 191: int (*sig)(); ! 192: { ! 193: register i; ! 194: ! 195: for(i=0;signum[i];i++) ! 196: signal(signum[i],sig); ! 197: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.