Deleted Added
full compact
ttymsg.c (73752) ttymsg.c (83242)
1/*
2 * Copyright (c) 1989, 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

--- 22 unchanged lines hidden (view full) ---

31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93";
37#endif
38static const char rcsid[] =
1/*
2 * Copyright (c) 1989, 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

--- 22 unchanged lines hidden (view full) ---

31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93";
37#endif
38static const char rcsid[] =
39 "$FreeBSD: head/usr.bin/wall/ttymsg.c 73752 2001-03-05 14:10:15Z ache $";
39 "$FreeBSD: head/usr.bin/wall/ttymsg.c 83242 2001-09-09 14:23:31Z dd $";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/uio.h>
44#include <dirent.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <paths.h>
48#include <signal.h>
49#include <stdio.h>
50#include <string.h>
51#include <stdlib.h>
52#include <unistd.h>
53
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/uio.h>
44#include <dirent.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <paths.h>
48#include <signal.h>
49#include <stdio.h>
50#include <string.h>
51#include <stdlib.h>
52#include <unistd.h>
53
54#include "ttymsg.h"
55
54/*
55 * Display the contents of a uio structure on a terminal. Used by wall(1),
56 * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
57 * waiting up to tmout seconds. Returns pointer to error string on unexpected
58 * error; string is not newline-terminated. Various "normal" errors are
59 * ignored (exclusive-use, lack of permission, etc.).
60 */
56/*
57 * Display the contents of a uio structure on a terminal. Used by wall(1),
58 * syslogd(8), and talkd(8). Forks and finishes in child if write would block,
59 * waiting up to tmout seconds. Returns pointer to error string on unexpected
60 * error; string is not newline-terminated. Various "normal" errors are
61 * ignored (exclusive-use, lack of permission, etc.).
62 */
61char *
63const char *
62ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
63{
64 struct iovec localiov[7];
64ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
65{
66 struct iovec localiov[7];
65 int cnt, fd, left, wret;
67 ssize_t left, wret;
68 int cnt, fd;
66 static char device[MAXNAMLEN] = _PATH_DEV;
67 static char errbuf[1024];
68 int forked;
69
70 forked = 0;
69 static char device[MAXNAMLEN] = _PATH_DEV;
70 static char errbuf[1024];
71 int forked;
72
73 forked = 0;
71 if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
74 if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
72 return ("too many iov's (change code in wall/ttymsg.c)");
73
74 strlcpy(device + sizeof(_PATH_DEV) - 1, line, sizeof(device));
75 if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
76 /* A slash is an attempt to break security... */
77 (void) snprintf(errbuf, sizeof(errbuf),
78 "Too many '/' in \"%s\"", device);
79 return (errbuf);

--- 6 unchanged lines hidden (view full) ---

86 if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
87 if (errno == EBUSY || errno == EACCES)
88 return (NULL);
89 (void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
90 strerror(errno));
91 return (errbuf);
92 }
93
75 return ("too many iov's (change code in wall/ttymsg.c)");
76
77 strlcpy(device + sizeof(_PATH_DEV) - 1, line, sizeof(device));
78 if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
79 /* A slash is an attempt to break security... */
80 (void) snprintf(errbuf, sizeof(errbuf),
81 "Too many '/' in \"%s\"", device);
82 return (errbuf);

--- 6 unchanged lines hidden (view full) ---

89 if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
90 if (errno == EBUSY || errno == EACCES)
91 return (NULL);
92 (void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
93 strerror(errno));
94 return (errbuf);
95 }
96
94 for (cnt = left = 0; cnt < iovcnt; ++cnt)
97 for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
95 left += iov[cnt].iov_len;
96
97 for (;;) {
98 wret = writev(fd, iov, iovcnt);
99 if (wret >= left)
100 break;
101 if (wret >= 0) {
102 left -= wret;
103 if (iov != localiov) {
104 bcopy(iov, localiov,
105 iovcnt * sizeof(struct iovec));
106 iov = localiov;
107 }
98 left += iov[cnt].iov_len;
99
100 for (;;) {
101 wret = writev(fd, iov, iovcnt);
102 if (wret >= left)
103 break;
104 if (wret >= 0) {
105 left -= wret;
106 if (iov != localiov) {
107 bcopy(iov, localiov,
108 iovcnt * sizeof(struct iovec));
109 iov = localiov;
110 }
108 for (cnt = 0; wret >= iov->iov_len; ++cnt) {
111 for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
109 wret -= iov->iov_len;
110 ++iov;
111 --iovcnt;
112 }
113 if (wret) {
114 iov->iov_base += wret;
115 iov->iov_len -= wret;
116 }

--- 48 unchanged lines hidden ---
112 wret -= iov->iov_len;
113 ++iov;
114 --iovcnt;
115 }
116 if (wret) {
117 iov->iov_base += wret;
118 iov->iov_len -= wret;
119 }

--- 48 unchanged lines hidden ---