Deleted Added
full compact
write.c (12097) write.c (19190)
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/signal.h>
49#include <sys/stat.h>
50#include <sys/file.h>
51#include <sys/time.h>
52#include <utmp.h>
53#include <ctype.h>
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/signal.h>
49#include <sys/stat.h>
50#include <sys/file.h>
51#include <sys/time.h>
52#include <utmp.h>
53#include <ctype.h>
54#include <paths.h>
54#include <pwd.h>
55#include <stdio.h>
56#include <string.h>
57
58extern int errno;
59
60main(argc, argv)
61 int argc;
62 char **argv;
63{
64 register char *cp;
65 time_t atime;
66 uid_t myuid;
67 int msgsok, myttyfd;
68 char tty[MAXPATHLEN], *mytty, *ttyname();
69 void done();
70
71 /* check that sender has write enabled */
72 if (isatty(fileno(stdin)))
73 myttyfd = fileno(stdin);
74 else if (isatty(fileno(stdout)))
75 myttyfd = fileno(stdout);
76 else if (isatty(fileno(stderr)))
77 myttyfd = fileno(stderr);
78 else {
79 (void)fprintf(stderr, "write: can't find your tty\n");
80 exit(1);
81 }
82 if (!(mytty = ttyname(myttyfd))) {
83 (void)fprintf(stderr, "write: can't find your tty's name\n");
84 exit(1);
85 }
86 if (cp = rindex(mytty, '/'))
87 mytty = cp + 1;
88 if (term_chk(mytty, &msgsok, &atime, 1))
89 exit(1);
90 if (!msgsok) {
91 (void)fprintf(stderr,
92 "write: you have write permission turned off.\n");
93 exit(1);
94 }
95
96 myuid = getuid();
97
98 /* check args */
99 switch (argc) {
100 case 2:
101 search_utmp(argv[1], tty, mytty, myuid);
102 do_write(tty, mytty, myuid);
103 break;
104 case 3:
55#include <pwd.h>
56#include <stdio.h>
57#include <string.h>
58
59extern int errno;
60
61main(argc, argv)
62 int argc;
63 char **argv;
64{
65 register char *cp;
66 time_t atime;
67 uid_t myuid;
68 int msgsok, myttyfd;
69 char tty[MAXPATHLEN], *mytty, *ttyname();
70 void done();
71
72 /* check that sender has write enabled */
73 if (isatty(fileno(stdin)))
74 myttyfd = fileno(stdin);
75 else if (isatty(fileno(stdout)))
76 myttyfd = fileno(stdout);
77 else if (isatty(fileno(stderr)))
78 myttyfd = fileno(stderr);
79 else {
80 (void)fprintf(stderr, "write: can't find your tty\n");
81 exit(1);
82 }
83 if (!(mytty = ttyname(myttyfd))) {
84 (void)fprintf(stderr, "write: can't find your tty's name\n");
85 exit(1);
86 }
87 if (cp = rindex(mytty, '/'))
88 mytty = cp + 1;
89 if (term_chk(mytty, &msgsok, &atime, 1))
90 exit(1);
91 if (!msgsok) {
92 (void)fprintf(stderr,
93 "write: you have write permission turned off.\n");
94 exit(1);
95 }
96
97 myuid = getuid();
98
99 /* check args */
100 switch (argc) {
101 case 2:
102 search_utmp(argv[1], tty, mytty, myuid);
103 do_write(tty, mytty, myuid);
104 break;
105 case 3:
105 if (!strncmp(argv[2], "/dev/", 5))
106 if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
106 argv[2] += 5;
107 if (utmp_chk(argv[1], argv[2])) {
108 (void)fprintf(stderr,
109 "write: %s is not logged in on %s.\n",
110 argv[1], argv[2]);
111 exit(1);
112 }
113 if (term_chk(argv[2], &msgsok, &atime, 1))
114 exit(1);
115 if (myuid && !msgsok) {
116 (void)fprintf(stderr,
117 "write: %s has messages disabled on %s\n",
118 argv[1], argv[2]);
119 exit(1);
120 }
121 do_write(argv[2], mytty, myuid);
122 break;
123 default:
124 (void)fprintf(stderr, "usage: write user [tty]\n");
125 exit(1);
126 }
127 done();
128 /* NOTREACHED */
129}
130
131/*
132 * utmp_chk - checks that the given user is actually logged in on
133 * the given tty
134 */
135utmp_chk(user, tty)
136 char *user, *tty;
137{
138 struct utmp u;
139 int ufd;
140
141 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
142 return(0); /* ignore error, shouldn't happen anyway */
143
144 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
145 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
146 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
147 (void)close(ufd);
148 return(0);
149 }
150
151 (void)close(ufd);
152 return(1);
153}
154
155/*
156 * search_utmp - search utmp for the "best" terminal to write to
157 *
158 * Ignores terminals with messages disabled, and of the rest, returns
159 * the one with the most recent access time. Returns as value the number
160 * of the user's terminals with messages enabled, or -1 if the user is
161 * not logged in at all.
162 *
163 * Special case for writing to yourself - ignore the terminal you're
164 * writing from, unless that's the only terminal with messages enabled.
165 */
166search_utmp(user, tty, mytty, myuid)
167 char *user, *tty, *mytty;
168 uid_t myuid;
169{
170 struct utmp u;
171 time_t bestatime, atime;
172 int ufd, nloggedttys, nttys, msgsok, user_is_me;
173 char atty[UT_LINESIZE + 1];
174
175 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) {
176 perror("utmp");
177 exit(1);
178 }
179
180 nloggedttys = nttys = 0;
181 bestatime = 0;
182 user_is_me = 0;
183 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
184 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
185 ++nloggedttys;
186 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
187 atty[UT_LINESIZE] = '\0';
188 if (term_chk(atty, &msgsok, &atime, 0))
189 continue; /* bad term? skip */
190 if (myuid && !msgsok)
191 continue; /* skip ttys with msgs off */
192 if (strcmp(atty, mytty) == 0) {
193 user_is_me = 1;
194 continue; /* don't write to yourself */
195 }
196 ++nttys;
197 if (atime > bestatime) {
198 bestatime = atime;
199 (void)strcpy(tty, atty);
200 }
201 }
202
203 (void)close(ufd);
204 if (nloggedttys == 0) {
205 (void)fprintf(stderr, "write: %s is not logged in\n", user);
206 exit(1);
207 }
208 if (nttys == 0) {
209 if (user_is_me) { /* ok, so write to yourself! */
210 (void)strcpy(tty, mytty);
211 return;
212 }
213 (void)fprintf(stderr,
214 "write: %s has messages disabled\n", user);
215 exit(1);
216 } else if (nttys > 1) {
217 (void)fprintf(stderr,
218 "write: %s is logged in more than once; writing to %s\n",
219 user, tty);
220 }
221}
222
223/*
224 * term_chk - check that a terminal exists, and get the message bit
225 * and the access time
226 */
227term_chk(tty, msgsokP, atimeP, showerror)
228 char *tty;
229 int *msgsokP, showerror;
230 time_t *atimeP;
231{
232 struct stat s;
233 char path[MAXPATHLEN];
234
107 argv[2] += 5;
108 if (utmp_chk(argv[1], argv[2])) {
109 (void)fprintf(stderr,
110 "write: %s is not logged in on %s.\n",
111 argv[1], argv[2]);
112 exit(1);
113 }
114 if (term_chk(argv[2], &msgsok, &atime, 1))
115 exit(1);
116 if (myuid && !msgsok) {
117 (void)fprintf(stderr,
118 "write: %s has messages disabled on %s\n",
119 argv[1], argv[2]);
120 exit(1);
121 }
122 do_write(argv[2], mytty, myuid);
123 break;
124 default:
125 (void)fprintf(stderr, "usage: write user [tty]\n");
126 exit(1);
127 }
128 done();
129 /* NOTREACHED */
130}
131
132/*
133 * utmp_chk - checks that the given user is actually logged in on
134 * the given tty
135 */
136utmp_chk(user, tty)
137 char *user, *tty;
138{
139 struct utmp u;
140 int ufd;
141
142 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
143 return(0); /* ignore error, shouldn't happen anyway */
144
145 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
146 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
147 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
148 (void)close(ufd);
149 return(0);
150 }
151
152 (void)close(ufd);
153 return(1);
154}
155
156/*
157 * search_utmp - search utmp for the "best" terminal to write to
158 *
159 * Ignores terminals with messages disabled, and of the rest, returns
160 * the one with the most recent access time. Returns as value the number
161 * of the user's terminals with messages enabled, or -1 if the user is
162 * not logged in at all.
163 *
164 * Special case for writing to yourself - ignore the terminal you're
165 * writing from, unless that's the only terminal with messages enabled.
166 */
167search_utmp(user, tty, mytty, myuid)
168 char *user, *tty, *mytty;
169 uid_t myuid;
170{
171 struct utmp u;
172 time_t bestatime, atime;
173 int ufd, nloggedttys, nttys, msgsok, user_is_me;
174 char atty[UT_LINESIZE + 1];
175
176 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) {
177 perror("utmp");
178 exit(1);
179 }
180
181 nloggedttys = nttys = 0;
182 bestatime = 0;
183 user_is_me = 0;
184 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
185 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
186 ++nloggedttys;
187 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
188 atty[UT_LINESIZE] = '\0';
189 if (term_chk(atty, &msgsok, &atime, 0))
190 continue; /* bad term? skip */
191 if (myuid && !msgsok)
192 continue; /* skip ttys with msgs off */
193 if (strcmp(atty, mytty) == 0) {
194 user_is_me = 1;
195 continue; /* don't write to yourself */
196 }
197 ++nttys;
198 if (atime > bestatime) {
199 bestatime = atime;
200 (void)strcpy(tty, atty);
201 }
202 }
203
204 (void)close(ufd);
205 if (nloggedttys == 0) {
206 (void)fprintf(stderr, "write: %s is not logged in\n", user);
207 exit(1);
208 }
209 if (nttys == 0) {
210 if (user_is_me) { /* ok, so write to yourself! */
211 (void)strcpy(tty, mytty);
212 return;
213 }
214 (void)fprintf(stderr,
215 "write: %s has messages disabled\n", user);
216 exit(1);
217 } else if (nttys > 1) {
218 (void)fprintf(stderr,
219 "write: %s is logged in more than once; writing to %s\n",
220 user, tty);
221 }
222}
223
224/*
225 * term_chk - check that a terminal exists, and get the message bit
226 * and the access time
227 */
228term_chk(tty, msgsokP, atimeP, showerror)
229 char *tty;
230 int *msgsokP, showerror;
231 time_t *atimeP;
232{
233 struct stat s;
234 char path[MAXPATHLEN];
235
235 (void)sprintf(path, "/dev/%s", tty);
236 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
236 if (stat(path, &s) < 0) {
237 if (showerror)
238 (void)fprintf(stderr,
239 "write: %s: %s\n", path, strerror(errno));
240 return(1);
241 }
242 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
243 *atimeP = s.st_atime;
244 return(0);
245}
246
247/*
248 * do_write - actually make the connection
249 */
250do_write(tty, mytty, myuid)
251 char *tty, *mytty;
252 uid_t myuid;
253{
254 register char *login, *nows;
255 register struct passwd *pwd;
256 time_t now, time();
257 char *getlogin(), path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
258 void done();
259
260 /* Determine our login name before the we reopen() stdout */
261 if ((login = getlogin()) == NULL)
262 if (pwd = getpwuid(myuid))
263 login = pwd->pw_name;
264 else
265 login = "???";
266
237 if (stat(path, &s) < 0) {
238 if (showerror)
239 (void)fprintf(stderr,
240 "write: %s: %s\n", path, strerror(errno));
241 return(1);
242 }
243 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
244 *atimeP = s.st_atime;
245 return(0);
246}
247
248/*
249 * do_write - actually make the connection
250 */
251do_write(tty, mytty, myuid)
252 char *tty, *mytty;
253 uid_t myuid;
254{
255 register char *login, *nows;
256 register struct passwd *pwd;
257 time_t now, time();
258 char *getlogin(), path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
259 void done();
260
261 /* Determine our login name before the we reopen() stdout */
262 if ((login = getlogin()) == NULL)
263 if (pwd = getpwuid(myuid))
264 login = pwd->pw_name;
265 else
266 login = "???";
267
267 (void)sprintf(path, "/dev/%s", tty);
268 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
268 if ((freopen(path, "w", stdout)) == NULL) {
269 (void)fprintf(stderr, "write: %s: %s\n", path, strerror(errno));
270 exit(1);
271 }
272
273 (void)signal(SIGINT, done);
274 (void)signal(SIGHUP, done);
275
276 /* print greeting */
277 if (gethostname(host, sizeof(host)) < 0)
278 (void)strcpy(host, "???");
279 now = time((time_t *)NULL);
280 nows = ctime(&now);
281 nows[16] = '\0';
282 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
283 login, host, mytty, nows + 11);
284
285 while (fgets(line, sizeof(line), stdin) != NULL)
286 wr_fputs(line);
287}
288
289/*
290 * done - cleanup and exit
291 */
292void
293done()
294{
295 (void)printf("EOF\r\n");
296 exit(0);
297}
298
299/*
300 * wr_fputs - like fputs(), but makes control characters visible and
301 * turns \n into \r\n
302 */
303wr_fputs(s)
304 register unsigned char *s;
305{
306
307#define PUTC(c) if (putchar(c) == EOF) goto err;
308
309 for (; *s != '\0'; ++s) {
310 if (*s == '\n') {
311 PUTC('\r');
312 } else if (!isprint(*s) && !isspace(*s) && *s != '\007') {
313 if (*s & 0x80) {
314 *s &= ~0x80;
315 PUTC('M');
316 PUTC('-');
317 }
318 if (iscntrl(*s)) {
319 *s ^= 0x40;
320 PUTC('^');
321 }
322 }
323 PUTC(*s);
324 }
325 return;
326
327err: (void)fprintf(stderr, "write: %s\n", strerror(errno));
328 exit(1);
329#undef PUTC
330}
269 if ((freopen(path, "w", stdout)) == NULL) {
270 (void)fprintf(stderr, "write: %s: %s\n", path, strerror(errno));
271 exit(1);
272 }
273
274 (void)signal(SIGINT, done);
275 (void)signal(SIGHUP, done);
276
277 /* print greeting */
278 if (gethostname(host, sizeof(host)) < 0)
279 (void)strcpy(host, "???");
280 now = time((time_t *)NULL);
281 nows = ctime(&now);
282 nows[16] = '\0';
283 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
284 login, host, mytty, nows + 11);
285
286 while (fgets(line, sizeof(line), stdin) != NULL)
287 wr_fputs(line);
288}
289
290/*
291 * done - cleanup and exit
292 */
293void
294done()
295{
296 (void)printf("EOF\r\n");
297 exit(0);
298}
299
300/*
301 * wr_fputs - like fputs(), but makes control characters visible and
302 * turns \n into \r\n
303 */
304wr_fputs(s)
305 register unsigned char *s;
306{
307
308#define PUTC(c) if (putchar(c) == EOF) goto err;
309
310 for (; *s != '\0'; ++s) {
311 if (*s == '\n') {
312 PUTC('\r');
313 } else if (!isprint(*s) && !isspace(*s) && *s != '\007') {
314 if (*s & 0x80) {
315 *s &= ~0x80;
316 PUTC('M');
317 PUTC('-');
318 }
319 if (iscntrl(*s)) {
320 *s ^= 0x40;
321 PUTC('^');
322 }
323 }
324 PUTC(*s);
325 }
326 return;
327
328err: (void)fprintf(stderr, "write: %s\n", strerror(errno));
329 exit(1);
330#undef PUTC
331}