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