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
--- 29 unchanged lines hidden (view full) ---
38
39#if 0
40#ifndef lint
41static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
42#endif
43#endif
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/usr.bin/write/write.c 216370 2010-12-11 08:32:16Z joel $");
47
48#include <sys/param.h>
49#include <sys/signal.h>
50#include <sys/stat.h>
51#include <sys/file.h>
52#include <sys/time.h>
53#include <ctype.h>
54#include <err.h>
55#include <locale.h>
56#include <paths.h>
57#include <pwd.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62#include <utmpx.h>
63
64void done(int);
65void do_write(char *, char *, uid_t);
66static void usage(void);
67int term_chk(char *, int *, time_t *, int);
68void wr_fputs(unsigned char *s);
69void search_utmp(char *, char *, char *, uid_t);
70int utmp_chk(char *, char *);
71
72int
73main(int argc, char **argv)
74{
75 time_t atime;
76 uid_t myuid;
--- 161 unchanged lines hidden (view full) ---
238 */
239void
240do_write(char *tty, char *mytty, uid_t myuid)
241{
242 const char *login;
243 char *nows;
244 struct passwd *pwd;
245 time_t now;
246 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
247
248 /* Determine our login name before we reopen() stdout */
249 if ((login = getlogin()) == NULL) {
250 if ((pwd = getpwuid(myuid)))
251 login = pwd->pw_name;
252 else
253 login = "???";
254 }
--- 9 unchanged lines hidden (view full) ---
264 if (gethostname(host, sizeof(host)) < 0)
265 (void)strcpy(host, "???");
266 now = time((time_t *)NULL);
267 nows = ctime(&now);
268 nows[16] = '\0';
269 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
270 login, host, mytty, nows + 11);
271
272 while (fgets(line, sizeof(line), stdin) != NULL)
273 wr_fputs(line);
274}
275
276/*
277 * done - cleanup and exit
278 */
279void
280done(int n __unused)
281{
282 (void)printf("EOF\r\n");
283 exit(0);
284}
285
286/*
287 * wr_fputs - like fputs(), but makes control characters visible and
288 * turns \n into \r\n
289 */
290void
291wr_fputs(unsigned char *s)
292{
293
294#define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
295
296 for (; *s != '\0'; ++s) {
297 if (*s == '\n') {
298 PUTC('\r');
299 } else if (((*s & 0x80) && *s < 0xA0) ||
300 /* disable upper controls */
301 (!isprint(*s) && !isspace(*s) &&
302 *s != '\a' && *s != '\b')
303 ) {
304 if (*s & 0x80) {
305 *s &= ~0x80;
306 PUTC('M');
307 PUTC('-');
308 }
309 if (iscntrl(*s)) {
310 *s ^= 0x40;
311 PUTC('^');
312 }
313 }
314 PUTC(*s);
315 }
316 return;
317#undef PUTC
318}
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
--- 29 unchanged lines hidden (view full) ---
38
39#if 0
40#ifndef lint
41static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
42#endif
43#endif
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/usr.bin/write/write.c 216370 2010-12-11 08:32:16Z joel $");
47
48#include <sys/param.h>
49#include <sys/signal.h>
50#include <sys/stat.h>
51#include <sys/file.h>
52#include <sys/time.h>
53#include <ctype.h>
54#include <err.h>
55#include <locale.h>
56#include <paths.h>
57#include <pwd.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62#include <utmpx.h>
63
64void done(int);
65void do_write(char *, char *, uid_t);
66static void usage(void);
67int term_chk(char *, int *, time_t *, int);
68void wr_fputs(unsigned char *s);
69void search_utmp(char *, char *, char *, uid_t);
70int utmp_chk(char *, char *);
71
72int
73main(int argc, char **argv)
74{
75 time_t atime;
76 uid_t myuid;
--- 161 unchanged lines hidden (view full) ---
238 */
239void
240do_write(char *tty, char *mytty, uid_t myuid)
241{
242 const char *login;
243 char *nows;
244 struct passwd *pwd;
245 time_t now;
246 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
247
248 /* Determine our login name before we reopen() stdout */
249 if ((login = getlogin()) == NULL) {
250 if ((pwd = getpwuid(myuid)))
251 login = pwd->pw_name;
252 else
253 login = "???";
254 }
--- 9 unchanged lines hidden (view full) ---
264 if (gethostname(host, sizeof(host)) < 0)
265 (void)strcpy(host, "???");
266 now = time((time_t *)NULL);
267 nows = ctime(&now);
268 nows[16] = '\0';
269 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
270 login, host, mytty, nows + 11);
271
272 while (fgets(line, sizeof(line), stdin) != NULL)
273 wr_fputs(line);
274}
275
276/*
277 * done - cleanup and exit
278 */
279void
280done(int n __unused)
281{
282 (void)printf("EOF\r\n");
283 exit(0);
284}
285
286/*
287 * wr_fputs - like fputs(), but makes control characters visible and
288 * turns \n into \r\n
289 */
290void
291wr_fputs(unsigned char *s)
292{
293
294#define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
295
296 for (; *s != '\0'; ++s) {
297 if (*s == '\n') {
298 PUTC('\r');
299 } else if (((*s & 0x80) && *s < 0xA0) ||
300 /* disable upper controls */
301 (!isprint(*s) && !isspace(*s) &&
302 *s != '\a' && *s != '\b')
303 ) {
304 if (*s & 0x80) {
305 *s &= ~0x80;
306 PUTC('M');
307 PUTC('-');
308 }
309 if (iscntrl(*s)) {
310 *s ^= 0x40;
311 PUTC('^');
312 }
313 }
314 PUTC(*s);
315 }
316 return;
317#undef PUTC
318}