diff --git a/Makefile b/Makefile index 59a9995..b2ab3f2 100644 --- a/Makefile +++ b/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 diff --git a/procps/Makefile b/procps/Makefile new file mode 100644 index 0000000..76c8c29 --- /dev/null +++ b/procps/Makefile @@ -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 $@ $< diff --git a/procps/uptime.c b/procps/uptime.c new file mode 100644 index 0000000..5a0e3b4 --- /dev/null +++ b/procps/uptime.c @@ -0,0 +1,53 @@ +#include +#include + +#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; +}