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

Defined functions

eof defined in line 169; used 4 times
ex defined in line 176; used 1 times
main defined in line 35; never used
sigs defined in line 200; used 4 times
timout defined in line 162; used 2 times

Defined variables

him defined in line 22; used 8 times
histty defined in line 24; used 10 times
histtya defined in line 25; used 4 times
logcnt defined in line 28; used 4 times
me defined in line 21; used 2 times
mytty defined in line 23; used 7 times
sccsid defined in line 1; never used
signum defined in line 20; used 2 times
ubuf defined in line 19; used 9 times

Defined macros

LMAX defined in line 15; never used
NMAX defined in line 14; used 2 times
Last modified: 1983-06-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 960
Valid CSS Valid XHTML 1.0 Strict