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 231586 2012-02-13 14:40:15Z glebius $"); |
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#include <wchar.h> 64#include <wctype.h> |
65 66void done(int); 67void do_write(char *, char *, uid_t); 68static void usage(void); 69int term_chk(char *, int *, time_t *, int); |
70void wr_fputs(wchar_t *s); |
71void search_utmp(char *, char *, char *, uid_t); 72int utmp_chk(char *, char *); 73 74int 75main(int argc, char **argv) 76{ 77 time_t atime; 78 uid_t myuid; --- 161 unchanged lines hidden (view full) --- 240 */ 241void 242do_write(char *tty, char *mytty, uid_t myuid) 243{ 244 const char *login; 245 char *nows; 246 struct passwd *pwd; 247 time_t now; |
248 char path[MAXPATHLEN], host[MAXHOSTNAMELEN]; 249 wchar_t line[512]; |
250 251 /* Determine our login name before we reopen() stdout */ 252 if ((login = getlogin()) == NULL) { 253 if ((pwd = getpwuid(myuid))) 254 login = pwd->pw_name; 255 else 256 login = "???"; 257 } --- 9 unchanged lines hidden (view full) --- 267 if (gethostname(host, sizeof(host)) < 0) 268 (void)strcpy(host, "???"); 269 now = time((time_t *)NULL); 270 nows = ctime(&now); 271 nows[16] = '\0'; 272 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 273 login, host, mytty, nows + 11); 274 |
275 while (fgetws(line, sizeof(line)/sizeof(wchar_t), stdin) != NULL) |
276 wr_fputs(line); 277} 278 279/* 280 * done - cleanup and exit 281 */ 282void 283done(int n __unused) 284{ 285 (void)printf("EOF\r\n"); 286 exit(0); 287} 288 289/* 290 * wr_fputs - like fputs(), but makes control characters visible and 291 * turns \n into \r\n 292 */ 293void |
294wr_fputs(wchar_t *s) |
295{ 296 |
297#define PUTC(c) if (putwchar(c) == WEOF) err(1, NULL); |
298 |
299 for (; *s != L'\0'; ++s) { 300 if (*s == L'\n') { 301 PUTC(L'\r'); 302 PUTC(L'\n'); 303 } else if (iswprint(*s) || iswspace(*s)) { 304 PUTC(*s); 305 } else { 306 wprintf(L"<0x%X>", *s); |
307 } |
308 } 309 return; 310#undef PUTC 311} |