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
--- 33 unchanged lines hidden (view full) ---
42
43#if 0
44#ifndef lint
45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/usr.bin/write/write.c 173572 2007-11-12 20:02:21Z jhb $");
51
52#include <sys/param.h>
53#include <sys/signal.h>
54#include <sys/stat.h>
55#include <sys/file.h>
56#include <sys/time.h>
57#include <ctype.h>
58#include <err.h>
59#include <locale.h>
60#include <paths.h>
61#include <pwd.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66#include <utmp.h>
67
68void done(int);
69void do_write(char *, char *, uid_t);
70static void usage(void);
71int term_chk(char *, int *, time_t *, int);
72void wr_fputs(unsigned char *s);
73void search_utmp(char *, char *, char *, uid_t);
74int utmp_chk(char *, char *);
--- 66 unchanged lines hidden (view full) ---
141
142/*
143 * utmp_chk - checks that the given user is actually logged in on
144 * the given tty
145 */
146int
147utmp_chk(char *user, char *tty)
148{
149 struct utmp u;
150 int ufd;
151
152 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
153 return(0); /* ignore error, shouldn't happen anyway */
154
155 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
156 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
157 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
158 (void)close(ufd);
159 return(0);
160 }
161
162 (void)close(ufd);
163 return(1);
164}
165
166/*
167 * search_utmp - search utmp for the "best" terminal to write to
168 *
169 * Ignores terminals with messages disabled, and of the rest, returns
170 * the one with the most recent access time. Returns as value the number
171 * of the user's terminals with messages enabled, or -1 if the user is
172 * not logged in at all.
173 *
174 * Special case for writing to yourself - ignore the terminal you're
175 * writing from, unless that's the only terminal with messages enabled.
176 */
177void
178search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
179{
180 struct utmp u;
181 time_t bestatime, atime;
182 int ufd, nloggedttys, nttys, msgsok, user_is_me;
183 char atty[UT_LINESIZE + 1];
184
185 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
186 err(1, "utmp");
187
188 nloggedttys = nttys = 0;
189 bestatime = 0;
190 user_is_me = 0;
191 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
192 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
193 ++nloggedttys;
194 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
195 atty[UT_LINESIZE] = '\0';
196 if (term_chk(atty, &msgsok, &atime, 0))
197 continue; /* bad term? skip */
198 if (myuid && !msgsok)
199 continue; /* skip ttys with msgs off */
200 if (strcmp(atty, mytty) == 0) {
201 user_is_me = 1;
202 continue; /* don't write to yourself */
203 }
204 ++nttys;
205 if (atime > bestatime) {
206 bestatime = atime;
207 (void)strcpy(tty, atty);
208 }
209 }
210
211 (void)close(ufd);
212 if (nloggedttys == 0)
213 errx(1, "%s is not logged in", user);
214 if (nttys == 0) {
215 if (user_is_me) { /* ok, so write to yourself! */
216 (void)strcpy(tty, mytty);
217 return;
218 }
219 errx(1, "%s has messages disabled", user);
220 } else if (nttys > 1) {
221 warnx("%s is logged in more than once; writing to %s", user, tty);
222 }
223}
224
--- 104 unchanged lines hidden ---
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
--- 33 unchanged lines hidden (view full) ---
42
43#if 0
44#ifndef lint
45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/usr.bin/write/write.c 173572 2007-11-12 20:02:21Z jhb $");
51
52#include <sys/param.h>
53#include <sys/signal.h>
54#include <sys/stat.h>
55#include <sys/file.h>
56#include <sys/time.h>
57#include <ctype.h>
58#include <err.h>
59#include <locale.h>
60#include <paths.h>
61#include <pwd.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66#include <utmp.h>
67
68void done(int);
69void do_write(char *, char *, uid_t);
70static void usage(void);
71int term_chk(char *, int *, time_t *, int);
72void wr_fputs(unsigned char *s);
73void search_utmp(char *, char *, char *, uid_t);
74int utmp_chk(char *, char *);
--- 66 unchanged lines hidden (view full) ---
141
142/*
143 * utmp_chk - checks that the given user is actually logged in on
144 * the given tty
145 */
146int
147utmp_chk(char *user, char *tty)
148{
149 struct utmp u;
150 int ufd;
151
152 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
153 return(0); /* ignore error, shouldn't happen anyway */
154
155 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
156 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
157 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
158 (void)close(ufd);
159 return(0);
160 }
161
162 (void)close(ufd);
163 return(1);
164}
165
166/*
167 * search_utmp - search utmp for the "best" terminal to write to
168 *
169 * Ignores terminals with messages disabled, and of the rest, returns
170 * the one with the most recent access time. Returns as value the number
171 * of the user's terminals with messages enabled, or -1 if the user is
172 * not logged in at all.
173 *
174 * Special case for writing to yourself - ignore the terminal you're
175 * writing from, unless that's the only terminal with messages enabled.
176 */
177void
178search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
179{
180 struct utmp u;
181 time_t bestatime, atime;
182 int ufd, nloggedttys, nttys, msgsok, user_is_me;
183 char atty[UT_LINESIZE + 1];
184
185 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
186 err(1, "utmp");
187
188 nloggedttys = nttys = 0;
189 bestatime = 0;
190 user_is_me = 0;
191 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
192 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
193 ++nloggedttys;
194 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
195 atty[UT_LINESIZE] = '\0';
196 if (term_chk(atty, &msgsok, &atime, 0))
197 continue; /* bad term? skip */
198 if (myuid && !msgsok)
199 continue; /* skip ttys with msgs off */
200 if (strcmp(atty, mytty) == 0) {
201 user_is_me = 1;
202 continue; /* don't write to yourself */
203 }
204 ++nttys;
205 if (atime > bestatime) {
206 bestatime = atime;
207 (void)strcpy(tty, atty);
208 }
209 }
210
211 (void)close(ufd);
212 if (nloggedttys == 0)
213 errx(1, "%s is not logged in", user);
214 if (nttys == 0) {
215 if (user_is_me) { /* ok, so write to yourself! */
216 (void)strcpy(tty, mytty);
217 return;
218 }
219 errx(1, "%s has messages disabled", user);
220 } else if (nttys > 1) {
221 warnx("%s is logged in more than once; writing to %s", user, tty);
222 }
223}
224
--- 104 unchanged lines hidden ---