micro-utils/procps/uptime.c
2023-10-24 17:45:55 +03:00

53 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#if defined(CLOCK_BOOTTIME)
#define CLOCK CLOCK_BOOTTIME
#elif defined(CLOCK_UPTIME)
#define CLOCK CLOCK_UPTIME
#elif defined(__APPLE__)
#define CLOCK CLOCK_MONOTONIC
#endif
int main(void) {
/* Now date */
time_t current_secs;
time(&current_secs);
struct tm *current_time = localtime(&current_secs);
printf("%02u:%02u:%02u ", current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
/* How long system has beep up */
struct timespec uptime;
if (clock_gettime(CLOCK, &uptime) != -1) {
int days = uptime.tv_sec / 86400;
int hours = uptime.tv_sec / 3600;
int mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60);
printf("up:");
if (days > 0)
printf(" %d days", days);
if (hours > 0)
printf(" %d hours", hours);
printf(" %d mins", mins);
}
/* Print 1, 5 and 15 minute load averages */
double avg[3] = {0, 0, 0};
if (getloadavg(avg, sizeof(avg) / sizeof(avg[0])) < 0) {
fprintf(stderr, "getloadavg: getloadavg() failed\n");
return 1;
}
printf(" load average: %.2f %.2f %.2f\n", avg[0], avg[1], avg[2]);
return 0;
}