2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
47 static const char rcsid[] =
48 "$FreeBSD: src/usr.bin/write/write.c,v 1.12 1999/08/28 01:07:48 peter Exp $";
51 #include <sys/param.h>
52 #include <sys/signal.h>
67 void do_write __P((char *, char *, uid_t));
68 static void usage __P((void));
69 int term_chk __P((char *, int *, time_t *, int));
70 void wr_fputs __P((unsigned char *s));
71 void search_utmp __P((char *, char *, char *, uid_t));
72 int utmp_chk __P((char *, char *));
83 char tty[MAXPATHLEN], *mytty;
85 (void)setlocale(LC_CTYPE, "");
87 /* check that sender has write enabled */
88 if (isatty(fileno(stdin)))
89 myttyfd = fileno(stdin);
90 else if (isatty(fileno(stdout)))
91 myttyfd = fileno(stdout);
92 else if (isatty(fileno(stderr)))
93 myttyfd = fileno(stderr);
95 errx(1, "can't find your tty");
96 if (!(mytty = ttyname(myttyfd)))
97 errx(1, "can't find your tty's name");
98 if ((cp = rindex(mytty, '/')))
100 if (term_chk(mytty, &msgsok, &atime, 1))
103 errx(1, "you have write permission turned off");
110 search_utmp(argv[1], tty, mytty, myuid);
111 do_write(tty, mytty, myuid);
114 if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
115 argv[2] += strlen(_PATH_DEV);
116 if (utmp_chk(argv[1], argv[2]))
117 errx(1, "%s is not logged in on %s", argv[1], argv[2]);
118 if (term_chk(argv[2], &msgsok, &atime, 1))
120 if (myuid && !msgsok)
121 errx(1, "%s has messages disabled on %s", argv[1], argv[2]);
122 do_write(argv[2], mytty, myuid);
134 (void)fprintf(stderr, "usage: write user [tty]\n");
139 * utmp_chk - checks that the given user is actually logged in on
149 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
150 return(0); /* ignore error, shouldn't happen anyway */
152 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
153 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
154 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
164 * search_utmp - search utmp for the "best" terminal to write to
166 * Ignores terminals with messages disabled, and of the rest, returns
167 * the one with the most recent access time. Returns as value the number
168 * of the user's terminals with messages enabled, or -1 if the user is
169 * not logged in at all.
171 * Special case for writing to yourself - ignore the terminal you're
172 * writing from, unless that's the only terminal with messages enabled.
175 search_utmp(user, tty, mytty, myuid)
176 char *user, *tty, *mytty;
180 time_t bestatime, atime;
181 int ufd, nloggedttys, nttys, msgsok, user_is_me;
182 char atty[UT_LINESIZE + 1];
184 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
187 nloggedttys = nttys = 0;
190 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
191 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
193 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
194 atty[UT_LINESIZE] = '\0';
195 if (term_chk(atty, &msgsok, &atime, 0))
196 continue; /* bad term? skip */
197 if (myuid && !msgsok)
198 continue; /* skip ttys with msgs off */
199 if (strcmp(atty, mytty) == 0) {
201 continue; /* don't write to yourself */
204 if (atime > bestatime) {
206 (void)strcpy(tty, atty);
211 if (nloggedttys == 0)
212 errx(1, "%s is not logged in", user);
214 if (user_is_me) { /* ok, so write to yourself! */
215 (void)strcpy(tty, mytty);
218 errx(1, "%s has messages disabled", user);
219 } else if (nttys > 1) {
220 warnx("%s is logged in more than once; writing to %s", user, tty);
225 * term_chk - check that a terminal exists, and get the message bit
226 * and the access time
229 term_chk(tty, msgsokP, atimeP, showerror)
231 int *msgsokP, showerror;
235 char path[MAXPATHLEN];
237 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
238 if (stat(path, &s) < 0) {
243 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
244 *atimeP = s.st_atime;
249 * do_write - actually make the connection
252 do_write(tty, mytty, myuid)
256 register char *login, *nows;
257 register struct passwd *pwd;
259 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
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;
269 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
270 if ((freopen(path, "w", stdout)) == NULL)
273 (void)signal(SIGINT, done);
274 (void)signal(SIGHUP, done);
277 if (gethostname(host, sizeof(host)) < 0)
278 (void)strcpy(host, "???");
279 now = time((time_t *)NULL);
282 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
283 login, host, mytty, nows + 11);
285 while (fgets(line, sizeof(line), stdin) != NULL)
290 * done - cleanup and exit
294 int n; /* signal number */
296 (void)printf("EOF\r\n");
301 * wr_fputs - like fputs(), but makes control characters visible and
306 register unsigned char *s;
309 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
311 for (; *s != '\0'; ++s) {
314 } else if (((*s & 0x80) && *s < 0xA0) ||
315 /* disable upper controls */
316 (!isprint(*s) && !isspace(*s) &&
317 *s != '\a' && *s != '\b')