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 8 USE_UPTIME(NEWTOY(uptime, NULL, TOYFLAG_USR|TOYFLAG_BIN)) 9 10 config UPTIME 11 bool "uptime" 12 default y 13 depends on TOYBOX_UTMPX 14 help 15 usage: uptime 16 17 Tell how long the system has been running and the system load 18 averages for the past 1, 5 and 15 minutes. 19 */ 20 21 #include "toys.h" 22 23 void uptime_main(void) 24 { 25 struct sysinfo info; 26 time_t tmptime; 27 struct tm * now; 28 unsigned int days, hours, minutes; 29 struct utmpx *entry; 30 int users = 0; 31 32 // Obtain the data we need. 33 sysinfo(&info); 34 time(&tmptime); 35 now = localtime(&tmptime); 36 37 // Obtain info about logged on users 38 setutxent(); 39 while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++; 40 endutxent(); 41 42 // Time 43 xprintf(" %02d:%02d:%02d up ", now->tm_hour, now->tm_min, now->tm_sec); 44 // Uptime 45 info.uptime /= 60; 46 minutes = info.uptime%60; 47 info.uptime /= 60; 48 hours = info.uptime%24; 49 days = info.uptime/24; 50 if (days) xprintf("%d day%s, ", days, (days!=1)?"s":""); 51 if (hours) xprintf("%2d:%02d, ", hours, minutes); 52 else printf("%d min, ", minutes); 53 printf(" %d user%s, ", users, (users!=1) ? "s" : ""); 54 printf(" load average: %.02f, %.02f, %.02f\n", info.loads[0]/65536.0, 55 info.loads[1]/65536.0, info.loads[2]/65536.0); 56 } 57