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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/signal.h>
65 void do_write(char *, char *, uid_t);
66 static void usage(void);
67 int term_chk(char *, int *, time_t *, int);
68 void wr_fputs(unsigned char *s);
69 void search_utmp(char *, char *, char *, uid_t);
70 int utmp_chk(char *, char *);
73 main(int argc, char **argv)
78 char tty[MAXPATHLEN], *mytty;
80 (void)setlocale(LC_CTYPE, "");
82 while (getopt(argc, argv, "") != -1)
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 (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
99 mytty += strlen(_PATH_DEV);
100 if (term_chk(mytty, &msgsok, &atime, 1))
103 errx(1, "you have write permission turned off");
110 search_utmp(argv[0], tty, mytty, myuid);
111 do_write(tty, mytty, myuid);
114 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
115 argv[1] += strlen(_PATH_DEV);
116 if (utmp_chk(argv[0], argv[1]))
117 errx(1, "%s is not logged in on %s", argv[0], argv[1]);
118 if (term_chk(argv[1], &msgsok, &atime, 1))
120 if (myuid && !msgsok)
121 errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
122 do_write(argv[1], mytty, myuid);
134 (void)fprintf(stderr, "usage: write user [tty]\n");
139 * utmp_chk - checks that the given user is actually logged in on
143 utmp_chk(char *user, char *tty)
147 strncpy(lu.ut_line, tty, sizeof lu.ut_line);
149 while ((u = getutxline(&lu)) != NULL)
150 if (u->ut_type == USER_PROCESS &&
151 strcmp(user, u->ut_user) == 0) {
160 * search_utmp - search utmp for the "best" terminal to write to
162 * Ignores terminals with messages disabled, and of the rest, returns
163 * the one with the most recent access time. Returns as value the number
164 * of the user's terminals with messages enabled, or -1 if the user is
165 * not logged in at all.
167 * Special case for writing to yourself - ignore the terminal you're
168 * writing from, unless that's the only terminal with messages enabled.
171 search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
174 time_t bestatime, atime;
175 int nloggedttys, nttys, msgsok, user_is_me;
177 nloggedttys = nttys = 0;
182 while ((u = getutxent()) != NULL)
183 if (u->ut_type == USER_PROCESS &&
184 strcmp(user, u->ut_user) == 0) {
186 if (term_chk(u->ut_line, &msgsok, &atime, 0))
187 continue; /* bad term? skip */
188 if (myuid && !msgsok)
189 continue; /* skip ttys with msgs off */
190 if (strcmp(u->ut_line, mytty) == 0) {
192 continue; /* don't write to yourself */
195 if (atime > bestatime) {
197 (void)strlcpy(tty, u->ut_line, MAXPATHLEN);
202 if (nloggedttys == 0)
203 errx(1, "%s is not logged in", user);
205 if (user_is_me) { /* ok, so write to yourself! */
206 (void)strlcpy(tty, mytty, MAXPATHLEN);
209 errx(1, "%s has messages disabled", user);
210 } else if (nttys > 1) {
211 warnx("%s is logged in more than once; writing to %s", user, tty);
216 * term_chk - check that a terminal exists, and get the message bit
217 * and the access time
220 term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
223 char path[MAXPATHLEN];
225 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
226 if (stat(path, &s) < 0) {
231 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
232 *atimeP = s.st_atime;
237 * do_write - actually make the connection
240 do_write(char *tty, char *mytty, uid_t myuid)
246 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
248 /* Determine our login name before we reopen() stdout */
249 if ((login = getlogin()) == NULL) {
250 if ((pwd = getpwuid(myuid)))
251 login = pwd->pw_name;
256 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
257 if ((freopen(path, "w", stdout)) == NULL)
260 (void)signal(SIGINT, done);
261 (void)signal(SIGHUP, done);
264 if (gethostname(host, sizeof(host)) < 0)
265 (void)strcpy(host, "???");
266 now = time((time_t *)NULL);
269 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
270 login, host, mytty, nows + 11);
272 while (fgets(line, sizeof(line), stdin) != NULL)
277 * done - cleanup and exit
282 (void)printf("EOF\r\n");
287 * wr_fputs - like fputs(), but makes control characters visible and
291 wr_fputs(unsigned char *s)
294 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
296 for (; *s != '\0'; ++s) {
299 } else if (((*s & 0x80) && *s < 0xA0) ||
300 /* disable upper controls */
301 (!isprint(*s) && !isspace(*s) &&
302 *s != '\a' && *s != '\b')