From 44fce86dde57c1a4ad5375f1378b33f5088610e4 Mon Sep 17 00:00:00 2001 From: Jesse Smith Date: Tue, 30 Oct 2018 21:46:04 -0300 Subject: [PATCH] Added -l flag to "last" command. When used it allows "last" to display usernames longer than 8 characters long. --- doc/Changelog | 4 +++- man/last.1 | 4 ++++ src/last.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index 4185d2b..71fde60 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -27,7 +27,9 @@ sysvinit (2.92) unreleased; urgency=low * shutdown command was setting environment variable INIT_HALT to "POWERDOWN", this should have been "POWERDOWN" as specified in the manual page. Fixed code to match manual page and init scripts. - + Closes Debian bugs #717531 and ##590895 + * Added -l flag to "last" command. When used it allows "last" to + display usernames longer than 8 characters long. sysvinit (2.91) world; urgency=low diff --git a/man/last.1 b/man/last.1 index 20655e6..c57743c 100644 --- a/man/last.1 +++ b/man/last.1 @@ -93,6 +93,10 @@ Print full login and logout times and dates. .IP \fB\-i\fP This option is like \fB-d\fP in that it displays the IP number of the remote host, but it displays the IP number in numbers-and-dots notation. +.IP \fB\-l\fP +This option allows the display of usernames longer than 8 characters. +This may mess up formatting in some programs and make the output wider than +the standard 80 characters. .IP \fB\-o\fP Read an old-type wtmp file (written by linux-libc5 applications). .IP \fB\-w\fP diff --git a/src/last.c b/src/last.c index dba5d4a..9c15b39 100644 --- a/src/last.c +++ b/src/last.c @@ -77,6 +77,7 @@ int maxrecs = 0; /* Maximum number of records to list. */ int recsdone = 0; /* Number of records listed */ int showhost = 1; /* Show hostname too? */ int altlist = 0; /* Show hostname at the end. */ +int allow_long_username = 0; /* Show usernames longer than 8 characters */ int usedns = 0; /* Use DNS to lookup the hostname. */ int useip = 0; /* Print IP address in number format */ int fulltime = 0; /* Print full dates and times */ @@ -470,28 +471,69 @@ int list(struct utmp *p, time_t t, int what) if (!usedns && (s = strchr(p->ut_host, '.')) != NULL && strcmp(s + 1, domainname) == 0) *s = 0; #endif +#define str(s) # s +#define xstr(s) str(s) if (!altlist) { + if (allow_long_username) + { + len = snprintf(final, sizeof(final), + oldfmt ? "%-" xstr(OLD_NAMESIZE) "." xstr(OLD_NAMESIZE) "s %-12.12s " + "%-16.16s %-16.16s %-7.7s %-12.12s\n" + : "%-" xstr(UT_NAMESIZE) "." xstr(UT_NAMESIZE) "s %-12.12s " + "%-16.16s %-16.16s %-7.7s %-12.12s\n", + p->ut_name, utline, + domain, logintime, logouttime, length); + } + else /* show short username */ + { len = snprintf(final, sizeof(final), fulltime ? - "%-8.*s %-12.12s %-16.*s %-24.24s %-26.26s %-12.12s\n" : + "%-8.*s %-12.12s %-16.*s %-24.24s %-26.26s %-12.12s\n" : "%-8.*s %-12.12s %-16.*s %-16.16s %-7.7s %-12.12s\n", name_len, p->ut_name, utline, domain_len, domain, logintime, logouttime, length); + } /* show short username */ } else { + if (allow_long_username) + { + len = snprintf(final, sizeof(final), + oldfmt ? "%-" xstr(OLD_NAMESIZE) "." xstr(OLD_NAMESIZE) "s %-12.12s " + "%-16.16s %-7.7s %-16.16s %s\n" + : "%-" xstr(UT_NAMESIZE) "." xstr(UT_NAMESIZE) "s %-12.12s " + "%-16.16s %-7.7s %-16.16s %s\n", + p->ut_name, utline, + logintime, logouttime, length, domain); + } + else /* show short username */ + { len = snprintf(final, sizeof(final), fulltime ? "%-8.*s %-12.12s %-24.24s %-26.26s %-12.12s %s\n" : "%-8.*s %-12.12s %-16.16s %-7.7s %-12.12s %s\n", name_len, p->ut_name, utline, logintime, logouttime, length, domain); + } /* done showing short username */ } } else + if (allow_long_username) + { + len = snprintf(final, sizeof(final), + oldfmt ? "%-" xstr(OLD_NAMESIZE) "." xstr(OLD_NAMESIZE) "s %-12.12s " + "%-16.16s %-7.7s %-12.12s\n" + : "%-" xstr(UT_NAMESIZE) "." xstr(UT_NAMESIZE) "s %-12.12s " + "%-16.16s %-7.7s %-12.12s\n", + p->ut_name, utline, + logintime, logouttime, length); + } + else /* show short username */ + { len = snprintf(final, sizeof(final), fulltime ? "%-8.*s %-12.12s %-24.24s %-26.26s %-12.12s\n" : "%-8.*s %-12.12s %-16.16s %-7.7s %-12.12s\n", name_len, p->ut_name, utline, logintime, logouttime, length); + } /* end of showing short username */ #if defined(__GLIBC__) # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) @@ -598,7 +640,7 @@ int main(int argc, char **argv) progname = mybasename(argv[0]); /* Process the arguments. */ - while((c = getopt(argc, argv, "f:n:RxadFiot:0123456789w")) != EOF) + while((c = getopt(argc, argv, "f:n:RxadFliot:0123456789w")) != EOF) switch(c) { case 'R': showhost = 0; @@ -632,6 +674,9 @@ int main(int argc, char **argv) case 'F': fulltime++; break; + case 'l': + allow_long_username = 1; + break; case 't': if ((until = parsetm(optarg)) == (time_t)-1) { fprintf(stderr, "%s: Invalid time value \"%s\"\n",