1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:27:45 +00:00

Kernel: Add /proc/uptime file (number of seconds since boot.)

Also added a simple /bin/uptime to pretty-print this information. :^)
This commit is contained in:
Andreas Kling 2019-04-14 15:19:45 +02:00
parent 0e30f4e412
commit c0fe48635b
5 changed files with 47 additions and 0 deletions

31
Userland/uptime.cpp Normal file
View file

@ -0,0 +1,31 @@
#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;
}