This commit is contained in:
Your Name 2023-10-24 15:33:08 +03:00
parent 3bbadc8f11
commit 4809a598dd
3 changed files with 65 additions and 1 deletions

View File

@ -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
View 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
View 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(&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};
#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;
}