who: reformat and add the ability to run it as 'users' as well
authorAlan Cox <alan@linux.intel.com>
Sun, 12 Nov 2017 18:58:28 +0000 (18:58 +0000)
committerAlan Cox <alan@linux.intel.com>
Sun, 12 Nov 2017 18:58:28 +0000 (18:58 +0000)
Applications/util/who.c

index e97a75a..b50edc9 100644 (file)
  */
 
 
-#include <fcntl.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <utmp.h>
 
 void main(int argc, char *argv[])
 {
-    register struct utmp * entry;
-    char * timestr;
+       register struct utmp *entry;
+       char *timestr;
+       uint8_t fmt = 0;
 
-    setutent();
-    while ((entry = getutent()) != NULL)
-       if (entry->ut_type == USER_PROCESS) {
-           timestr = ctime(&entry->ut_time);
-           timestr[strlen(timestr) - 1] = '\0';
-           printf("%s  tty%c%c %s %s\n", entry->ut_user,
-                       entry->ut_id[0],
-                       entry->ut_id[1] ? entry->ut_id[1] : 0,
-                       timestr,
-                       entry->ut_host);
+       if (strcmp(argv[0], "users") == 0)
+               fmt = 1;
+       setutent();
+       while ((entry = getutent()) != NULL) {
+               if (entry->ut_type == USER_PROCESS) {
+                       if (fmt == 0) {
+                               timestr = ctime(&entry->ut_time);
+                               timestr[strlen(timestr) - 1] = '\0';
+                               printf("%s      tty%c%c %s %s\n", entry->ut_user, entry->ut_id[0], entry->ut_id[1] ? entry->ut_id[1] : 0, timestr, entry->ut_host);
+                       } else {
+                               if (fmt == 2)
+                                       putchar(' ');
+                               printf("%s", entry->ut_user);
+                               fmt = 2;
+                       }
+               }
        }
-    exit(0);
+       if (fmt == 2)
+               putchar('\n');
+       exit(0);
 }