1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:38:11 +00:00
serenity/Userland/uptime.cpp
Andreas Kling c0fe48635b Kernel: Add /proc/uptime file (number of seconds since boot.)
Also added a simple /bin/uptime to pretty-print this information. :^)
2019-04-14 15:19:45 +02:00

31 lines
754 B
C++

#include <stdio.h>
int main(int, char**)
{
FILE* fp = fopen("/proc/uptime", "r");
if (!fp) {
perror("fopen(/proc/uptime)");
return 1;
}
char buffer[BUFSIZ];
auto* p = fgets(buffer, sizeof(buffer), fp);
if (!p) {
perror("fgets");
return 1;
}
unsigned seconds;
sscanf(buffer, "%u", &seconds);
printf("Up %d day%s, ", seconds / 86400, (seconds / 86400) == 1 ? "" : "s");
seconds %= 86400;
printf("%d hour%s, ", seconds / 3600, (seconds / 3600) == 1 ? "" : "s");
seconds %= 3600;
printf("%d minute%s, ", seconds / 60, (seconds / 60) == 1 ? "" : "s");
seconds %= 60;
printf("%d second%s\n", seconds, seconds == 1 ? "" : "s");
fclose(fp);
return 0;
}