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
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)write.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.bin/write/write.c,v 1.12 1999/08/28 01:07:48 peter Exp $
39 * $DragonFly: src/usr.bin/write/write.c,v 1.7 2005/08/30 22:02:36 liamfoy Exp $
42 #include <sys/param.h>
59 static void done(int);
60 static void do_write(const char *, const char *, uid_t, int *);
61 static void usage(void);
62 static int term_chk(const char *, int *, time_t *, int);
63 static void wr_fputs(unsigned char *s);
64 static void search_utmp(const char *, char *, size_t, const char *, uid_t);
65 static int utmp_chk(const char *, const char *);
68 main(int argc, char **argv)
72 int msgsok, mymsgok, myttyfd;
73 char tty[MAXPATHLEN], *mytty;
75 setlocale(LC_CTYPE, "");
77 /* check that sender has write enabled */
78 if (isatty(fileno(stdin)))
79 myttyfd = fileno(stdin);
80 else if (isatty(fileno(stdout)))
81 myttyfd = fileno(stdout);
82 else if (isatty(fileno(stderr)))
83 myttyfd = fileno(stderr);
85 errx(1, "can't find your tty");
86 if (!(mytty = ttyname(myttyfd)))
87 errx(1, "can't find your tty's name");
88 if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
89 mytty += strlen(_PATH_DEV);
90 if (term_chk(mytty, &mymsgok, &atime, 1))
93 warnx("you have write permission turned off (man mesg)");
100 search_utmp(argv[1], tty, sizeof tty, mytty, myuid);
101 do_write(tty, mytty, myuid, &mymsgok);
104 if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
105 argv[2] += strlen(_PATH_DEV);
106 if (utmp_chk(argv[1], argv[2]))
107 errx(1, "%s is not logged in on %s", argv[1], argv[2]);
108 if (term_chk(argv[2], &msgsok, &atime, 1))
110 if (myuid && !msgsok)
111 errx(1, "%s has messages disabled on %s", argv[1], argv[2]);
112 do_write(argv[2], mytty, myuid, &mymsgok);
124 fprintf(stderr, "usage: write user [tty]\n");
129 * utmp_chk - checks that the given user is actually logged in on
133 utmp_chk(const char *user, const char *tty)
138 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
139 err(1, "open failed: %s\n", _PATH_UTMP);
141 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) {
142 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
143 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
154 * search_utmp - search utmp for the "best" terminal to write to
156 * Ignores terminals with messages disabled, and of the rest, returns
157 * the one with the most recent access time. Returns as value the number
158 * of the user's terminals with messages enabled, or -1 if the user is
159 * not logged in at all.
161 * Special case for writing to yourself - ignore the terminal you're
162 * writing from, unless that's the only terminal with messages enabled.
165 search_utmp(const char *user, char *tty, size_t ttyl, const char *mytty, uid_t myuid)
168 time_t bestatime, atime;
169 int ufd, nloggedttys, nttys, msgsok, user_is_me;
170 char atty[UT_LINESIZE + 1];
172 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
173 err(1, "open failed: %s", _PATH_UTMP);
175 nloggedttys = nttys = 0;
178 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
179 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
181 strlcpy(atty, u.ut_line, ttyl);
182 if (term_chk(atty, &msgsok, &atime, 0))
183 continue; /* bad term? skip */
184 if (myuid && !msgsok)
185 continue; /* skip ttys with msgs off */
186 if (strcmp(atty, mytty) == 0) {
188 continue; /* don't write to yourself */
191 if (atime > bestatime) {
193 strlcpy(tty, atty, ttyl);
198 if (nloggedttys == 0)
199 errx(1, "%s is not logged in", user);
201 if (user_is_me) { /* ok, so write to yourself! */
202 strlcpy(tty, mytty, ttyl);
205 errx(1, "%s has messages disabled", user);
206 } else if (nttys > 1) {
207 warnx("%s is logged in more than once; writing to %s", user, tty);
212 * term_chk - check that a terminal exists, and get the message bit
213 * and the access time
216 term_chk(const char *tty, int *msgsokP, time_t *atimeP, int showerror)
219 char path[MAXPATHLEN];
221 snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
222 if (stat(path, &s) < 0) {
227 *msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */
228 *atimeP = s.st_atime;
233 * do_write - actually make the connection
236 do_write(const char *tty, const char *mytty, uid_t myuid, int *mymsgok)
242 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
244 /* Determine our login name before we reopen() stdout */
245 if ((login = getlogin()) == NULL) {
246 if ((pwd = getpwuid(myuid)))
247 login = pwd->pw_name;
252 snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
253 if ((freopen(path, "w", stdout)) == NULL)
256 signal(SIGINT, done);
257 signal(SIGHUP, done);
260 if (gethostname(host, sizeof(host)) < 0)
261 strlcpy(host, "???", sizeof host);
265 printf("\r\n\007\007\007Message from %s@%s on %s at %s (%s %s replies)...\r\n",
266 login, host, mytty, nows + 11, login, *mymsgok == 0 ? "does not accept" : "accepts");
268 while (fgets(line, sizeof(line), stdin) != NULL)
273 * done - cleanup and exit
283 * wr_fputs - like fputs(), but makes control characters visible and
287 wr_fputs(unsigned char *s)
290 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
292 for (; *s != '\0'; ++s) {
295 } else if (((*s & 0x80) && *s < 0xA0) ||
296 /* disable upper controls */
297 (!isprint(*s) && !isspace(*s) &&
298 *s != '\a' && *s != '\b')