Home | History | Annotate | Download | only in other
      1 /* uptime.c - Tell how long the system has been running.
      2  *
      3  * Copyright 2012 Elie De Brauwer <eliedebrauwer (at) gmail.com>
      4  * Copyright 2012 Luis Felipe Strano Moraes <lfelipe (at) profusion.mobi>
      5  * Copyright 2013 Jeroen van Rijn <jvrnix (at) gmail.com>
      6 
      7 USE_UPTIME(NEWTOY(uptime, ">0s", TOYFLAG_USR|TOYFLAG_BIN))
      8 
      9 config UPTIME
     10   bool "uptime"
     11   default y
     12   depends on TOYBOX_UTMPX
     13   help
     14     usage: uptime [-s]
     15 
     16     Tell the current time, how long the system has been running, the number
     17     of users, and the system load averages for the past 1, 5 and 15 minutes.
     18 
     19     -s	Since when has the system been up?
     20 */
     21 
     22 #define FOR_uptime
     23 #include "toys.h"
     24 
     25 void uptime_main(void)
     26 {
     27   struct sysinfo info;
     28   time_t t;
     29   struct tm *tm;
     30   unsigned int days, hours, minutes;
     31   struct utmpx *entry;
     32   int users = 0;
     33 
     34   // Obtain the data we need.
     35   sysinfo(&info);
     36   time(&t);
     37 
     38   // Just show the time of boot?
     39   if (toys.optflags & FLAG_s) {
     40     t -= info.uptime;
     41     tm = localtime(&t);
     42     strftime(toybuf, sizeof(toybuf), "%F %T", tm);
     43     xputs(toybuf);
     44     return;
     45   }
     46 
     47   // Obtain info about logged on users
     48   setutxent();
     49   while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++;
     50   endutxent();
     51 
     52   // Current time
     53   tm = localtime(&t);
     54   xprintf(" %02d:%02d:%02d up ", tm->tm_hour, tm->tm_min, tm->tm_sec);
     55   // Uptime
     56   info.uptime /= 60;
     57   minutes = info.uptime%60;
     58   info.uptime /= 60;
     59   hours = info.uptime%24;
     60   days = info.uptime/24;
     61   if (days) xprintf("%d day%s, ", days, (days!=1)?"s":"");
     62   if (hours) xprintf("%2d:%02d, ", hours, minutes);
     63   else printf("%d min, ", minutes);
     64   printf(" %d user%s, ", users, (users!=1) ? "s" : "");
     65   printf(" load average: %.02f, %.02f, %.02f\n", info.loads[0]/65536.0,
     66     info.loads[1]/65536.0, info.loads[2]/65536.0);
     67 }
     68