delete now unused check_header_gzip.c
[oweals/busybox.git] / coreutils / who.c
index 2773e1a8b1613b26b43c4c6baee80f703d776b6d..546050aee0b89276179caa69b8eea19f902357f5 100644 (file)
  *    http://www.gnu.org/copyleft/gpl.html
  *
  * Copyright (c) 2002 AYR Networks, Inc.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ *
  *----------------------------------------------------------------------
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include "libbb.h"
 #include <utmp.h>
-#include <sys/stat.h>
 #include <time.h>
-#include "busybox.h"
 
-int who_main(int argc, char **argv)
+static void idle_string(char *str6, time_t t)
 {
-    struct utmp *ut;
-    struct stat st;
-    time_t     idle;
-    char *name;
+       t = time(NULL) - t;
 
-    if (argc > 1)
-       bb_show_usage();
-
-    setutent();
-    printf("USER       TTY      IDLE      FROM           HOST\n");
-
-    while ((ut = getutent()) != NULL) {
+       /*if (t < 60) {
+               str6[0] = '.';
+               str6[1] = '\0';
+               return;
+       }*/
+       if (t >= 0 && t < (24 * 60 * 60)) {
+               sprintf(str6, "%02d:%02d",
+                               (int) (t / (60 * 60)),
+                               (int) ((t % (60 * 60)) / 60));
+               return;
+       }
+       strcpy(str6, "old");
+}
 
-       if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) {
-               /* ut->ut_line is device name of tty - "/dev/" */
-           printf("%-10s %-8s ", ut->ut_user, ut->ut_line);
+int who_main(int argc, char **argv);
+int who_main(int argc, char **argv)
+{
+       char str6[6];
+       struct utmp *ut;
+       struct stat st;
+       char *name;
 
-               name = concat_path_file("/dev/", ut->ut_line);
-           if (stat(name, &st) == 0) {
-               idle = time(NULL) -  st.st_atime;
+       if (argc > 1) {
+               bb_show_usage();
+       }
 
-               if (idle < 60)
-                   printf("00:00m    ");
-               else if (idle < (60 * 60))
-                   printf("00:%02dm    ", (int)(idle / 60));
-               else if (idle < (24 * 60 * 60))
-                   printf("%02d:%02dm    ", (int)(idle / (60 * 60)),
-                          (int)(idle % (60 * 60)) / 60);
-               else if (idle < (24 * 60 * 60 * 365))
-                   printf("%03ddays   ", (int)(idle / (24 * 60 * 60)));
-               else
-                   printf("%02dyears   ", (int) (idle / (24 * 60 * 60 * 365)));
-           } else
-               printf("%-8s  ", "?");
+       setutent();
+       printf("USER       TTY      IDLE      TIME           HOST\n");
+       while ((ut = getutent()) != NULL) {
+               if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) {
+                       time_t thyme = ut->ut_tv.tv_sec;
 
-           printf("%-12.12s   %s\n", ctime((time_t*)&(ut->ut_tv.tv_sec)) + 4, ut->ut_host);
-               free(name);
+                       /* ut->ut_line is device name of tty - "/dev/" */
+                       name = concat_path_file("/dev", ut->ut_line);
+                       str6[0] = '?';
+                       str6[1] = '\0';
+                       if (stat(name, &st) == 0)
+                               idle_string(str6, st.st_atime);
+                       printf("%-10s %-8s %-9s %-14.14s %s\n",
+                                       ut->ut_user, ut->ut_line, str6,
+                                       ctime(&thyme) + 4, ut->ut_host);
+                       if (ENABLE_FEATURE_CLEAN_UP)
+                               free(name);
+               }
        }
-    }
-    endutent();
-
-    return 0;
+       if (ENABLE_FEATURE_CLEAN_UP)
+               endutent();
+       return 0;
 }