From f82a2a730c0a76d15214dcad8115de7506581664 Mon Sep 17 00:00:00 2001 From: dds Date: Wed, 4 Apr 2007 16:04:58 +0000 Subject: A dash as an argument to the -f option will now cause lastcomm to read data from the standard input. This allows tail -f to pipe data to lastcomm, and thereby real-time monitoring of executed commands. The manual page includes the exact incantation. MFC after: 2 weeks --- usr.bin/lastcomm/lastcomm.c | 49 ++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'usr.bin/lastcomm/lastcomm.c') diff --git a/usr.bin/lastcomm/lastcomm.c b/usr.bin/lastcomm/lastcomm.c index 6672e8f..c513aec 100644 --- a/usr.bin/lastcomm/lastcomm.c +++ b/usr.bin/lastcomm/lastcomm.c @@ -132,27 +132,40 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - /* Open the file. */ - if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb)) - err(1, "could not open %s", acctfile); - - /* - * Round off to integral number of accounting records, probably - * not necessary, but it doesn't hurt. - */ - size = sb.st_size - sb.st_size % sizeof(struct acct); - - /* Check if any records to display. */ - if ((unsigned)size < sizeof(struct acct)) - exit(0); + if (strcmp(acctfile, "-") == 0) + fp = stdin; + else { + /* Open the file. */ + if ((fp = fopen(acctfile, "r")) == NULL || + fstat(fileno(fp), &sb)) + err(1, "could not open %s", acctfile); + + /* + * Round off to integral number of accounting records, + * probably not necessary, but it doesn't hurt. + */ + size = sb.st_size - sb.st_size % sizeof(struct acct); + + /* Check if any records to display. */ + if ((unsigned)size < sizeof(struct acct)) + exit(0); + } do { int rv; - size -= sizeof(struct acct); - if (fseeko(fp, size, SEEK_SET) == -1) - err(1, "seek %s failed", acctfile); - if ((rv = fread(&ab, sizeof(struct acct), 1, fp)) != 1) - err(1, "read %s returned %d", acctfile, rv); + + if (fp != stdin) { + size -= sizeof(struct acct); + if (fseeko(fp, size, SEEK_SET) == -1) + err(1, "seek %s failed", acctfile); + } + + if ((rv = fread(&ab, sizeof(struct acct), 1, fp)) != 1) { + if (feof(fp)) + break; + else + err(1, "read %s returned %d", acctfile, rv); + } if (ab.ac_comm[0] == '\0') { ab.ac_comm[0] = '?'; -- cgit v1.1