wall.c (8fa0b743820f61c661ba5f3ea0e3be0dc137910e) | wall.c (586fbee6432751a00e43d94107d9890a2694292e) |
---|---|
1/* 2 * Copyright (c) 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 48 unchanged lines hidden (view full) --- 57#include <paths.h> 58#include <pwd.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <time.h> 63#include <unistd.h> 64#include <utmpx.h> | 1/* 2 * Copyright (c) 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 48 unchanged lines hidden (view full) --- 57#include <paths.h> 58#include <pwd.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <time.h> 63#include <unistd.h> 64#include <utmpx.h> |
65#include <wchar.h> 66#include <wctype.h> |
|
65 66#include "ttymsg.h" 67 68static void makemsg(char *); 69static void usage(void); 70 71static struct wallgroup { 72 struct wallgroup *next; --- 107 unchanged lines hidden (view full) --- 180 (void)fprintf(stderr, "usage: wall [-g group] [file]\n"); 181 exit(1); 182} 183 184void 185makemsg(char *fname) 186{ 187 int cnt; | 67 68#include "ttymsg.h" 69 70static void makemsg(char *); 71static void usage(void); 72 73static struct wallgroup { 74 struct wallgroup *next; --- 107 unchanged lines hidden (view full) --- 182 (void)fprintf(stderr, "usage: wall [-g group] [file]\n"); 183 exit(1); 184} 185 186void 187makemsg(char *fname) 188{ 189 int cnt; |
188 unsigned char ch; | 190 wchar_t ch; |
189 struct tm *lt; 190 struct passwd *pw; 191 struct stat sbuf; 192 time_t now; 193 FILE *fp; 194 int fd; | 191 struct tm *lt; 192 struct passwd *pw; 193 struct stat sbuf; 194 time_t now; 195 FILE *fp; 196 int fd; |
195 char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; | 197 char hostname[MAXHOSTNAMELEN], tmpname[64]; 198 wchar_t *p, *tmp, lbuf[256], codebuf[13]; |
196 const char *tty; 197 const char *whom; 198 gid_t egid; 199 200 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 201 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 202 err(1, "can't open temporary file"); 203 (void)unlink(tmpname); --- 11 unchanged lines hidden (view full) --- 215 216 /* 217 * all this stuff is to blank out a square for the message; 218 * we wrap message lines at column 79, not 80, because some 219 * terminals wrap after 79, some do not, and we can't tell. 220 * Which means that we may leave a non-blank character 221 * in column 80, but that can't be helped. 222 */ | 199 const char *tty; 200 const char *whom; 201 gid_t egid; 202 203 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 204 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+"))) 205 err(1, "can't open temporary file"); 206 (void)unlink(tmpname); --- 11 unchanged lines hidden (view full) --- 218 219 /* 220 * all this stuff is to blank out a square for the message; 221 * we wrap message lines at column 79, not 80, because some 222 * terminals wrap after 79, some do not, and we can't tell. 223 * Which means that we may leave a non-blank character 224 * in column 80, but that can't be helped. 225 */ |
223 (void)fprintf(fp, "\r%79s\r\n", " "); 224 (void)snprintf(lbuf, sizeof(lbuf), 225 "Broadcast Message from %s@%s", | 226 (void)fwprintf(fp, L"\r%79s\r\n", " "); 227 (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t), 228 L"Broadcast Message from %s@%s", |
226 whom, hostname); | 229 whom, hostname); |
227 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 228 (void)snprintf(lbuf, sizeof(lbuf), 229 " (%s) at %d:%02d %s...", tty, | 230 (void)fwprintf(fp, L"%-79.79S\007\007\r\n", lbuf); 231 (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t), 232 L" (%s) at %d:%02d %s...", tty, |
230 lt->tm_hour, lt->tm_min, lt->tm_zone); | 233 lt->tm_hour, lt->tm_min, lt->tm_zone); |
231 (void)fprintf(fp, "%-79.79s\r\n", lbuf); | 234 (void)fwprintf(fp, L"%-79.79S\r\n", lbuf); |
232 } | 235 } |
233 (void)fprintf(fp, "%79s\r\n", " "); | 236 (void)fwprintf(fp, L"%79s\r\n", " "); |
234 235 if (fname) { 236 egid = getegid(); 237 setegid(getgid()); | 237 238 if (fname) { 239 egid = getegid(); 240 setegid(getgid()); |
238 if (freopen(fname, "r", stdin) == NULL) | 241 if (freopen(fname, "r", stdin) == NULL) |
239 err(1, "can't read %s", fname); 240 setegid(egid); 241 } 242 cnt = 0; | 242 err(1, "can't read %s", fname); 243 setegid(egid); 244 } 245 cnt = 0; |
243 while (fgets(lbuf, sizeof(lbuf), stdin)) { 244 for (p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 245 if (ch == '\r') { 246 putc('\r', fp); | 246 while (fgetws(lbuf, sizeof(lbuf)/sizeof(wchar_t), stdin)) { 247 for (p = lbuf; (ch = *p) != L'\0'; ++p, ++cnt) { 248 if (ch == L'\r') { 249 putwc(L'\r', fp); |
247 cnt = 0; 248 continue; | 250 cnt = 0; 251 continue; |
249 } else if (ch == '\n') { | 252 } else if (ch == L'\n') { |
250 for (; cnt < 79; ++cnt) | 253 for (; cnt < 79; ++cnt) |
251 putc(' ', fp); 252 putc('\r', fp); 253 putc('\n', fp); | 254 putwc(L' ', fp); 255 putwc(L'\r', fp); 256 putwc(L'\n', fp); |
254 break; 255 } 256 if (cnt == 79) { | 257 break; 258 } 259 if (cnt == 79) { |
257 putc('\r', fp); 258 putc('\n', fp); | 260 putwc(L'\r', fp); 261 putwc(L'\n', fp); |
259 cnt = 0; 260 } | 262 cnt = 0; 263 } |
261 if (((ch & 0x80) && ch < 0xA0) || 262 /* disable upper controls */ 263 (!isprint(ch) && !isspace(ch) && 264 ch != '\a' && ch != '\b') 265 ) { 266 if (ch & 0x80) { 267 ch &= 0x7F; 268 putc('M', fp); | 264 if (iswprint(ch) || iswspace(ch) || ch == L'\a' || ch == L'\b') { 265 putwc(ch, fp); 266 } else { 267 (void)swprintf(codebuf, sizeof(codebuf)/sizeof(wchar_t), L"<0x%X>", ch); 268 for (tmp = codebuf; *tmp != L'\0'; ++tmp) { 269 putwc(*tmp, fp); |
269 if (++cnt == 79) { | 270 if (++cnt == 79) { |
270 putc('\r', fp); 271 putc('\n', fp); | 271 putwc(L'\r', fp); 272 putwc(L'\n', fp); |
272 cnt = 0; 273 } | 273 cnt = 0; 274 } |
274 putc('-', fp); 275 if (++cnt == 79) { 276 putc('\r', fp); 277 putc('\n', fp); 278 cnt = 0; 279 } | |
280 } | 275 } |
281 if (iscntrl(ch)) { 282 ch ^= 040; 283 putc('^', fp); 284 if (++cnt == 79) { 285 putc('\r', fp); 286 putc('\n', fp); 287 cnt = 0; 288 } 289 } | 276 --cnt; |
290 } | 277 } |
291 putc(ch, fp); | |
292 } 293 } | 278 } 279 } |
294 (void)fprintf(fp, "%79s\r\n", " "); | 280 (void)fwprintf(fp, L"%79s\r\n", " "); |
295 rewind(fp); 296 297 if (fstat(fd, &sbuf)) 298 err(1, "can't stat temporary file"); 299 mbufsize = sbuf.st_size; 300 if (!(mbuf = malloc((u_int)mbufsize))) 301 err(1, "out of memory"); 302 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 303 err(1, "can't read temporary file"); 304 (void)close(fd); 305} | 281 rewind(fp); 282 283 if (fstat(fd, &sbuf)) 284 err(1, "can't stat temporary file"); 285 mbufsize = sbuf.st_size; 286 if (!(mbuf = malloc((u_int)mbufsize))) 287 err(1, "out of memory"); 288 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 289 err(1, "can't read temporary file"); 290 (void)close(fd); 291} |