From 1a8570a63d854d7a5ad2e3addd3587d686efe054 Mon Sep 17 00:00:00 2001 From: toni Date: Fri, 23 Dec 2016 15:21:17 +0100 Subject: better user online output --- dummyshell.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/dummyshell.c b/dummyshell.c index 5723381..60d73b9 100644 --- a/dummyshell.c +++ b/dummyshell.c @@ -1,3 +1,8 @@ +/* + * build with: gcc -Wall -O2 -D_HAS_SIGNAL=1 -D_HAS_UTMP=1 -D_HAS_SYSINFO -ffunction-sections -fdata-sections -ffast-math -fomit-frame-pointer dummyshell.c -o dummyshell + * strip -s dummyshell + */ + #include #include #include @@ -37,17 +42,25 @@ static volatile unsigned char doLoop = 1; #ifdef _HAS_UTMP +static size_t +strnlen(const char *str, size_t maxlen) +{ + const char *cp; + for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--); + return (size_t)(cp - str); +} + static void print_utmp(void) { int utmpfd = open("/var/run/utmp", O_RDONLY); if (utmpfd >= 0) { struct utmp ut; memset(&ut, '\0', sizeof(struct utmp)); - printf("\33[2K\ruser online...: "); - while (read(utmpfd, &ut, sizeof(struct utmp)) == sizeof(struct utmp)) { - printf("%.*s ", UT_NAMESIZE, ut.ut_user); + const char uonline[] = "\33[2K\ruser online...: "; + size_t i = 0; + while ( read(utmpfd, &ut, sizeof(struct utmp)) == sizeof(struct utmp) && strnlen(ut.ut_user, UT_NAMESIZE) > 0 ) { + printf("%s[%lu] %.*s from %.*s\n", uonline, (long unsigned int)++i, UT_NAMESIZE, ut.ut_user, UT_HOSTSIZE, ut.ut_host); } - printf("\n"); } } #endif -- cgit v1.2.3