Bug 137365 - [patch] let last(1) read from stdin via "-f -"
Summary: [patch] let last(1) read from stdin via "-f -"
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 1.0-CURRENT
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-bugs mailing list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-08-02 22:40 UTC by jschauma
Modified: 2017-12-31 22:27 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jschauma 2009-08-02 22:40:03 UTC
last -f <file>  lets you read from a specified file.  However, there's no way to read from a pipe.  NetBSD's last(1) let's you use "-f -" to specify reading from stdin -- port this to FreeBSD.

Fix: 

LIST_INIT(&ttylist);

-       if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
+       if (!strcmp(file, "-")) {
+               wfd = STDIN_FILENO;
+               file = "<stdin>";
+       } else if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
                err(1, "%s", file);
        bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);

@@ -226,9 +229,11 @@
        (void)signal(SIGQUIT, onintr);

        while (--bl >= 0) {
-               if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
-                   (bytes = read(wfd, buf, sizeof(buf))) == -1)
+               if (((lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1) &&
+                       (ESPIPE != errno)) ||
+                               ((bytes = read(wfd, buf, sizeof(buf))) == -1)) {
                        err(1, "%s", file);
+               }
                for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp)
                        doentry(bp);
        }--GEqkZjTzuL3fmqJT9CPadUw6r47H3HSkIZyzY8N4Ak3T7lyR
Content-Type: text/plain; name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="file.diff"

Index: last.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/last/last.c,v
retrieving revision 1.34
diff -b -u -r1.34 last.c
--- last.c      8 Aug 2004 18:59:19 -0000       1.34
+++ last.c      2 Aug 2009 21:24:51 -0000
@@ -216,7 +216,10 @@
How-To-Repeat: cat /var/tmp/wtmp | last -f -
Comment 1 Guy Helmer freebsd_committer 2013-05-23 21:17:42 UTC
State Changed
From-To: open->feedback

Since last(1) was changed to use utmpx, this patch no longer applies. 
It seems lib/libc/gen/getutxent.c would need to be changed to support 
this functionality. I would suggest either closing this PR and submitting 
a new patch for -current, or attach a new patch for -current to this bug.
Comment 2 Eitan Adler freebsd_committer freebsd_triage 2017-12-31 08:01:12 UTC
For bugs matching the following criteria:

Status: In Progress Changed: (is less than) 2014-06-01

Reset to default assignee and clear in-progress tags.

Mail being skipped