From f5105e25f5006b8cbeec3c543c8c385ee81c5493 Mon Sep 17 00:00:00 2001 From: danny Date: Thu, 28 May 1998 00:58:29 +0000 Subject: Reviewed by: Peter Hawkins Add s and w flags to show duration in or with seconds. --- usr.bin/last/last.1 | 22 +++++++++++++++------- usr.bin/last/last.c | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 18 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/last/last.1 b/usr.bin/last/last.1 index c746d5b..3f091ee 100644 --- a/usr.bin/last/last.1 +++ b/usr.bin/last/last.1 @@ -42,7 +42,9 @@ .Op Fl Ns Ar n .Op Fl f Ar file .Op Fl h Ar host +.Op Fl s .Op Fl t Ar tty +.Op Fl w .Op user ... .Sh DESCRIPTION .Nm Last @@ -60,16 +62,22 @@ a crash or shutdown, will so indicate. .Pp .Bl -tag -width indent-two +.It Fl Ar n +Limits the report to +.Ar n +lines. .It Fl f Ar file .Nm Last reads the file .Ar file instead of the default, .Pa /var/log/wtmp . -.It Fl Ar n -Limits the report to -.Ar n -lines. +.It Fl h Ar host +.Ar Host +names may be names or internet numbers. +.It Fl s +Report the duration of the login session in seconds, instead of the +default days, hours and minutes. .It Fl t Ar tty Specify the .Ar tty . @@ -78,9 +86,9 @@ Tty names may be given fully or abbreviated, for example, is equivalent to .Dq Li "last -t tty03" . -.It Fl h Ar host -.Ar Host -names may be names or internet numbers. +.It Fl w +Widen the duration field to show seconds, as well as the +default days, hours and minutes. .El .Pp If diff --git a/usr.bin/last/last.c b/usr.bin/last/last.c index 708a086..6ad6f3b 100644 --- a/usr.bin/last/last.c +++ b/usr.bin/last/last.c @@ -83,6 +83,8 @@ struct ttytab { static long currentout, /* current logout value */ maxrec; /* records to display */ static char *file = _PATH_WTMP; /* wtmp file */ +static int sflag = 0; /* show delta in seconds */ +static int width = 5; /* show seconds in delta */ void addarg __P((int, char *)); void hostconv __P((char *)); @@ -91,6 +93,14 @@ char *ttyconv __P((char *)); int want __P((struct utmp *)); void wtmp __P((void)); +void +usage(void) +{ + (void)fprintf(stderr, + "usage: last [-#] [-f file] [-h hostname] [-t tty] [-s|w] [user ...]\n"); + exit(1); +} + int main(argc, argv) int argc; @@ -104,7 +114,7 @@ main(argc, argv) (void) setlocale(LC_TIME, ""); maxrec = -1; - while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != -1) + while ((ch = getopt(argc, argv, "0123456789f:h:st:w")) != -1) switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -129,16 +139,22 @@ main(argc, argv) hostconv(optarg); addarg(HOST_TYPE, optarg); break; + case 's': + sflag++; /* Show delta as seconds */ + break; case 't': addarg(TTY_TYPE, ttyconv(optarg)); break; + case 'w': + width = 8; + break; case '?': default: - (void)fprintf(stderr, - "usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n"); - exit(1); + usage(); } + if (sflag && width == 8) usage(); + if (argc) { setlinebuf(stdout); for (argv += optind; *argv; ++argv) { @@ -280,13 +296,18 @@ wtmp() printf("- %5.5s", ct + 11); } delta = tt->logout - bp->ut_time; - tm = gmtime(&delta); - (void) strftime(ct, sizeof(ct), "%c", tm); - if (delta < 86400) - printf(" (%5.5s)\n", ct + 11); - else - printf(" (%ld+%5.5s)\n", - delta / 86400, ct + 11); + if ( sflag ) { + printf(" (%8lu)\n", + delta); + } else { + tm = gmtime(&delta); + (void) strftime(ct, sizeof(ct), "%c", tm); + if (delta < 86400) + printf(" (%*.*s)\n", width, width, ct + 11); + else + printf(" (%ld+%*.*s)\n", + delta / 86400, width, width, ct + 11); + } } LIST_REMOVE(tt, list); free(tt); -- cgit v1.1