Cleanup this code a bit by attempting to sync it up with NetBSD and

with each other.

Reviewed by:	markm, dwmalone
This commit is contained in:
Kris Kennaway 2000-11-26 22:36:35 +00:00
parent f313a39933
commit 56e7ae90cb
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=69231
2 changed files with 33 additions and 26 deletions

View file

@ -61,6 +61,7 @@ static const char rcsid[] =
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utmp.h>
@ -70,8 +71,9 @@ static const char rcsid[] =
int mbufsize;
char *mbuf;
void makemsg __P((char *));
void makemsg __P((char *));
static void usage __P((void));
char *ttymsg __P((struct iovec *, int, char *, int));
/* ARGSUSED */
int
@ -120,7 +122,7 @@ main(argc, argv)
static void
usage()
{
fprintf(stderr, "usage: rwall hostname [file]\n");
(void)fprintf(stderr, "usage: rwall hostname [file]\n");
exit(1);
}
@ -131,14 +133,15 @@ makemsg(fname)
struct tm *lt;
struct passwd *pw;
struct stat sbuf;
time_t now, time();
time_t now;
FILE *fp;
int fd;
char *whom, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
char *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
const char *whom;
snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
errx(1, "can't open temporary file");
(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
err(1, "can't open temporary file");
(void)unlink(tmpname);
if (!(whom = getlogin()))
@ -156,23 +159,26 @@ makemsg(fname)
*/
(void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
whom, hostname);
(void)fprintf(fp, " (%s) at %d:%02d ...\n", ttyname(2),
tty = ttyname(STDERR_FILENO);
if (tty == NULL)
tty = "no tty";
(void)fprintf(fp, " (%s) at %d:%02d ...\n", tty,
lt->tm_hour, lt->tm_min);
putc('\n', fp);
if (fname && !(freopen(fname, "r", stdin)))
errx(1, "can't read %s", fname);
err(1, "can't read %s", fname);
while (fgets(lbuf, sizeof(lbuf), stdin))
fputs(lbuf, fp);
rewind(fp);
if (fstat(fd, &sbuf))
errx(1, "can't stat temporary file");
err(1, "can't stat temporary file");
mbufsize = sbuf.st_size;
if (!(mbuf = malloc((u_int)mbufsize)))
errx(1, "out of memory");
err(1, "out of memory");
if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
errx(1, "can't read temporary file");
err(1, "can't read temporary file");
(void)close(fd);
}

View file

@ -68,6 +68,7 @@ static const char rcsid[] =
void makemsg __P((char *));
static void usage __P((void));
char *ttymsg __P((struct iovec *, int, char *, int));
#define IGNOREUSER "sleeper"
@ -85,7 +86,7 @@ main(argc, argv)
struct iovec iov;
struct utmp utmp;
FILE *fp;
char *p, *ttymsg();
char *p;
char line[sizeof(utmp.ut_line) + 1];
(void)setlocale(LC_CTYPE, "");
@ -109,7 +110,7 @@ main(argc, argv)
makemsg(*argv);
if (!(fp = fopen(_PATH_UTMP, "r")))
errx(1, "cannot read %s", _PATH_UTMP);
err(1, "cannot read %s", _PATH_UTMP);
iov.iov_base = mbuf;
iov.iov_len = mbufsize;
/* NOSTRICT */
@ -144,17 +145,17 @@ makemsg(fname)
time_t now;
FILE *fp;
int fd;
char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
const char *whom;
snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
errx(1, "can't open temporary file");
(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
err(1, "can't open temporary file");
(void)unlink(tmpname);
if (!nobanner) {
char *tty = ttyname(2);
if (!tty)
tty = ttyname(STDERR_FILENO);
if (tty == NULL)
tty = "no tty";
if (!(whom = getlogin()))
@ -175,7 +176,7 @@ makemsg(fname)
"Broadcast Message from %s@%s",
whom, hostname);
(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
(void)snprintf(lbuf, sizeof(lbuf),
(void)snprintf(lbuf, sizeof(lbuf),
" (%s) at %d:%02d %s...", tty,
lt->tm_hour, lt->tm_min, lt->tm_zone);
(void)fprintf(fp, "%-79.79s\r\n", lbuf);
@ -183,7 +184,7 @@ makemsg(fname)
(void)fprintf(fp, "%79s\r\n", " ");
if (fname && !(freopen(fname, "r", stdin)))
errx(1, "can't read %s", fname);
err(1, "can't read %s", fname);
while (fgets(lbuf, sizeof(lbuf), stdin))
for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
if (ch == '\r') {
@ -232,11 +233,11 @@ makemsg(fname)
rewind(fp);
if (fstat(fd, &sbuf))
errx(1, "can't stat temporary file");
err(1, "can't stat temporary file");
mbufsize = sbuf.st_size;
if (!(mbuf = malloc((u_int)mbufsize)))
errx(1, "out of memory");
err(1, "out of memory");
if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
errx(1, "can't read temporary file");
err(1, "can't read temporary file");
(void)close(fd);
}