aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoni <matzeton@googlemail.com>2016-12-23 15:21:17 +0100
committertoni <matzeton@googlemail.com>2016-12-23 15:21:17 +0100
commit1a8570a63d854d7a5ad2e3addd3587d686efe054 (patch)
tree05bfcb2ee2af9a831d1e76d102a5787f8c10d34a
parenta341f409c1ccc16a0f0b3eee3f2b4bac0eaad1f5 (diff)
better user online output
-rw-r--r--dummyshell.c21
1 files 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -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