uptime
This commit is contained in:
parent
3bbadc8f11
commit
4809a598dd
2
Makefile
2
Makefile
@ -1,4 +1,4 @@
|
||||
objects = coreutils console-tools networking miscutils
|
||||
objects = coreutils console-tools networking miscutils procps
|
||||
|
||||
CFLAGS?=-s -Os -flto -pedantic -Wall -Wextra
|
||||
CC?=cc
|
||||
|
11
procps/Makefile
Normal file
11
procps/Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
C_SOURCES:=$(wildcard ./*.c)
|
||||
C_TARGETS:=$(patsubst ./%.c, ../bin/%, $(C_SOURCES))
|
||||
TARGETS:=$(C_TARGETS)
|
||||
|
||||
CFLAGS?=-s -Os -flto -pedantic -Wall -Wextra
|
||||
CC?=cc
|
||||
|
||||
all: ../bin $(TARGETS)
|
||||
|
||||
../bin/%: %.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
53
procps/uptime.c
Normal file
53
procps/uptime.c
Normal file
@ -0,0 +1,53 @@
|
||||
#include <stdio.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(¤t_secs);
|
||||
|
||||
struct tm *current_time = localtime(¤t_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};
|
||||
|
||||
#ifndef __ANDROID__
|
||||
if (getloadavg(avg, sizeof(avg) / sizeof(avg[0])) < 0) {
|
||||
fprintf(stderr, "getloadavg: getloadavg() failed\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
printf(" load average: %.2f %.2f %.2f\n", avg[0], avg[1], avg[2]);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user