Use err(3).

This commit is contained in:
Philippe Charnier 1997-10-27 12:21:10 +00:00
parent 6fd3c7fdea
commit dc763e50ee
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=30773
2 changed files with 38 additions and 30 deletions

View File

@ -19,26 +19,26 @@ allows the superuser to examine all data coming through a specified tty.
writes to standard output.
.Pp
The options are as follows:
.Bl -tag -width "-l nul "
.Bl -tag -width indent
.It Fl c
Reconnect on close. If the tty observed by
.Nm watch
.Nm
is closed, automatically reattach to the same tty.
If this option is not specified,
.Nm watch
.Nm
will request a new tty if running in interactive mode or exit if running
without a controlling tty.
.It Fl i
Force interactive mode.
Interactive mode is a default if
.Nm watch
.Nm
is started from a tty.
If output is redirected to a file, interactive mode can still be requested
by specifying this option.
.It Fl o
Reconnect on overflow.
The behavior of
.Nm watch
.Nm
if the observed tty overflows is similar to the behavior if the observed tty
is closed.
For more info see
@ -63,14 +63,14 @@ While running in interactive mode, all user input is discarded except for:
.Bl -tag -width "XXXX" -compact
.It Sy "<control-G>"
Exit
.Nm watch .
.Nm Ns .
.It Sy "<control-W>"
Clear screen.
.It Sy "<control-X>"
Change attached tty.
.Sh RESTRICTIONS
Only the superuser can run
.Nm watch .
.Nm Ns .
.Sh SEE ALSO
.Xr pty 4 ,
.Xr sio 4 ,
@ -79,7 +79,7 @@ Only the superuser can run
No terminal emulation is performed.
All user output is reproduced as-is.
.Sh AUTHOR
Ugen J.S. Antsilevich <ugen@NetVision.net.il>
.An Ugen J.S. Antsilevich Aq ugen@NetVision.net.il
.Sh HISTORY
.Nm Watch
first appeared in

View File

@ -13,12 +13,18 @@
* Snoop stuff.
*/
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/filio.h>
#include <sys/snoop.h>
#include <sys/stat.h>
#include <err.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
@ -125,7 +131,8 @@ fatal(err, buf)
{
unset_tty();
if (buf)
fprintf(stderr, "Fatal: %s\n", buf);
errx(err, "fatal: %s", buf);
else
exit(err);
}
@ -147,7 +154,8 @@ open_snp()
continue;
return f;
}
fatal(EX_OSFILE, "Cannot open snoop device.");
fatal(EX_OSFILE, "cannot open snoop device");
return (0);
}
@ -162,10 +170,10 @@ cleanup()
}
void
show_usage()
static void
usage()
{
printf("watch -[ciotnW] [tty name]\n");
fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
exit(EX_USAGE);
}
@ -194,7 +202,8 @@ ctoh(c)
if (c >= 'a' && c <= 'f')
return (int) (c - 'a' + 10);
fatal(EX_DATAERR, "Bad tty number.");
fatal(EX_DATAERR, "bad tty number");
return (0);
}
@ -211,7 +220,7 @@ void
attach_snp()
{
if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
fatal(EX_UNAVAILABLE, "Cannot attach to tty.");
fatal(EX_UNAVAILABLE, "cannot attach to tty");
if (opt_timestamp)
timestamp("Logging Started.");
}
@ -235,10 +244,10 @@ set_dev(name)
}
if (*name == '\0' || stat(buf, &sb) < 0)
fatal(EX_DATAERR, "Bad device name.");
fatal(EX_DATAERR, "bad device name");
if ((sb.st_mode & S_IFMT) != S_IFCHR)
fatal(EX_DATAERR, "Must be a character device.");
fatal(EX_DATAERR, "must be a character device");
snp_tty = sb.st_rdev;
attach_snp();
@ -313,8 +322,7 @@ main(ac, av)
break;
case '?':
default:
show_usage();
exit(1);
usage();
}
signal(SIGINT, cleanup);
@ -326,14 +334,14 @@ main(ac, av)
if (opt_interactive && !opt_no_switch)
ask_dev(dev_name, MSG_INIT);
else
fatal(EX_DATAERR, "No device name given.");
fatal(EX_DATAERR, "no device name given");
} else
strncpy(dev_name, *av, DEV_NAME_LEN);
set_dev(dev_name);
if (!(buf = (char *) malloc(b_size)))
fatal(EX_UNAVAILABLE, "Cannot malloc().");
fatal(EX_UNAVAILABLE, "malloc failed");
FD_ZERO(&fd_s);
@ -345,11 +353,11 @@ main(ac, av)
if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
fatal(EX_OSERR, "ioctl() failed.");
fatal(EX_OSERR, "ioctl(FIONREAD)");
if (nread > READB_LEN)
nread = READB_LEN;
if (read(std_in,chb,nread)!=nread)
fatal(EX_IOERR, "read (stdin) failed.");
fatal(EX_IOERR, "read (stdin) failed");
switch (chb[0]) {
case CHR_CLEAR:
@ -367,7 +375,7 @@ main(ac, av)
if (write(snp_io,chb,nread) != nread) {
detach_snp();
if (opt_no_switch)
fatal(EX_IOERR, "Write failed.");
fatal(EX_IOERR, "write failed");
ask_dev(dev_name, MSG_NOWRITE);
set_dev(dev_name);
}
@ -379,7 +387,7 @@ main(ac, av)
continue;
if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
fatal(EX_OSERR, "ioctl() failed.");
fatal(EX_OSERR, "ioctl(FIONREAD)");
switch (nread) {
case SNP_OFLOW:
@ -403,19 +411,19 @@ main(ac, av)
if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
free(buf);
if (!(buf = (char *) malloc(b_size / 2)))
fatal(EX_UNAVAILABLE, "Cannot malloc()");
fatal(EX_UNAVAILABLE, "malloc failed");
b_size = b_size / 2;
}
if (nread > b_size) {
b_size = (nread % 2) ? (nread + 1) : (nread);
free(buf);
if (!(buf = (char *) malloc(b_size)))
fatal(EX_UNAVAILABLE, "Cannot malloc()");
fatal(EX_UNAVAILABLE, "malloc failed");
}
if (read(snp_io, buf, nread) < nread)
fatal(EX_IOERR, "read failed.");
fatal(EX_IOERR, "read failed");
if (write(std_out, buf, nread) < nread)
fatal(EX_IOERR, "write failed.");
fatal(EX_IOERR, "write failed");
}
} /* While */
}