10f66f451Sopenharmony_ci/* uptime.c - Tell how long the system has been running. 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> 40f66f451Sopenharmony_ci * Copyright 2012 Luis Felipe Strano Moraes <lfelipe@profusion.mobi> 50f66f451Sopenharmony_ci * Copyright 2013 Jeroen van Rijn <jvrnix@gmail.com> 60f66f451Sopenharmony_ci 70f66f451Sopenharmony_ciUSE_UPTIME(NEWTOY(uptime, ">0ps", TOYFLAG_USR|TOYFLAG_BIN)) 80f66f451Sopenharmony_ci 90f66f451Sopenharmony_ciconfig UPTIME 100f66f451Sopenharmony_ci bool "uptime" 110f66f451Sopenharmony_ci default y 120f66f451Sopenharmony_ci depends on TOYBOX_UTMPX 130f66f451Sopenharmony_ci help 140f66f451Sopenharmony_ci usage: uptime [-ps] 150f66f451Sopenharmony_ci 160f66f451Sopenharmony_ci Tell the current time, how long the system has been running, the number 170f66f451Sopenharmony_ci of users, and the system load averages for the past 1, 5 and 15 minutes. 180f66f451Sopenharmony_ci 190f66f451Sopenharmony_ci -p Pretty (human readable) uptime 200f66f451Sopenharmony_ci -s Since when has the system been up? 210f66f451Sopenharmony_ci*/ 220f66f451Sopenharmony_ci 230f66f451Sopenharmony_ci#define FOR_uptime 240f66f451Sopenharmony_ci#include "toys.h" 250f66f451Sopenharmony_ci 260f66f451Sopenharmony_civoid uptime_main(void) 270f66f451Sopenharmony_ci{ 280f66f451Sopenharmony_ci struct sysinfo info; 290f66f451Sopenharmony_ci time_t t; 300f66f451Sopenharmony_ci struct tm *tm; 310f66f451Sopenharmony_ci unsigned int weeks, days, hours, minutes; 320f66f451Sopenharmony_ci struct utmpx *entry; 330f66f451Sopenharmony_ci int users = 0; 340f66f451Sopenharmony_ci 350f66f451Sopenharmony_ci // Obtain the data we need. 360f66f451Sopenharmony_ci sysinfo(&info); 370f66f451Sopenharmony_ci time(&t); 380f66f451Sopenharmony_ci 390f66f451Sopenharmony_ci // Just show the time of boot? 400f66f451Sopenharmony_ci if (FLAG(s)) { 410f66f451Sopenharmony_ci t -= info.uptime; 420f66f451Sopenharmony_ci tm = localtime(&t); 430f66f451Sopenharmony_ci strftime(toybuf, sizeof(toybuf), "%F %T", tm); 440f66f451Sopenharmony_ci xputs(toybuf); 450f66f451Sopenharmony_ci return; 460f66f451Sopenharmony_ci } 470f66f451Sopenharmony_ci 480f66f451Sopenharmony_ci // Current time 490f66f451Sopenharmony_ci tm = localtime(&t); 500f66f451Sopenharmony_ci // Uptime 510f66f451Sopenharmony_ci info.uptime /= 60; 520f66f451Sopenharmony_ci minutes = info.uptime%60; 530f66f451Sopenharmony_ci info.uptime /= 60; 540f66f451Sopenharmony_ci hours = info.uptime%24; 550f66f451Sopenharmony_ci days = info.uptime/24; 560f66f451Sopenharmony_ci 570f66f451Sopenharmony_ci if (FLAG(p)) { 580f66f451Sopenharmony_ci weeks = days/7; 590f66f451Sopenharmony_ci days %= 7; 600f66f451Sopenharmony_ci xprintf("up %d week%s, %d day%s, %d hour%s, %d minute%s, ", 610f66f451Sopenharmony_ci weeks, (weeks!=1)?"s":"", 620f66f451Sopenharmony_ci days, (days!=1)?"s":"", 630f66f451Sopenharmony_ci hours, (hours!=1)?"s":"", 640f66f451Sopenharmony_ci minutes, (minutes!=1)?"s":""); 650f66f451Sopenharmony_ci } else { 660f66f451Sopenharmony_ci xprintf(" %02d:%02d:%02d up ", tm->tm_hour, tm->tm_min, tm->tm_sec); 670f66f451Sopenharmony_ci if (days) xprintf("%d day%s, ", days, (days!=1)?"s":""); 680f66f451Sopenharmony_ci if (hours) xprintf("%2d:%02d, ", hours, minutes); 690f66f451Sopenharmony_ci else printf("%d min, ", minutes); 700f66f451Sopenharmony_ci 710f66f451Sopenharmony_ci // Obtain info about logged on users 720f66f451Sopenharmony_ci setutxent(); 730f66f451Sopenharmony_ci while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++; 740f66f451Sopenharmony_ci endutxent(); 750f66f451Sopenharmony_ci printf(" %d user%s, ", users, (users!=1) ? "s" : ""); 760f66f451Sopenharmony_ci } 770f66f451Sopenharmony_ci 780f66f451Sopenharmony_ci printf(" load average: %.02f, %.02f, %.02f\n", info.loads[0]/65536.0, 790f66f451Sopenharmony_ci info.loads[1]/65536.0, info.loads[2]/65536.0); 800f66f451Sopenharmony_ci} 81