mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:57:44 +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:
parent
0e30f4e412
commit
c0fe48635b
5 changed files with 47 additions and 0 deletions
1
Userland/.gitignore
vendored
1
Userland/.gitignore
vendored
|
@ -43,3 +43,4 @@ tc
|
|||
host
|
||||
qs
|
||||
mv
|
||||
uptime
|
||||
|
|
|
@ -39,6 +39,7 @@ OBJS = \
|
|||
host.o \
|
||||
qs.o \
|
||||
mv.o \
|
||||
uptime.o \
|
||||
rm.o
|
||||
|
||||
APPS = \
|
||||
|
@ -83,6 +84,7 @@ APPS = \
|
|||
host \
|
||||
qs \
|
||||
mv \
|
||||
uptime \
|
||||
rm
|
||||
|
||||
ARCH_FLAGS =
|
||||
|
@ -228,6 +230,9 @@ qs: qs.o
|
|||
mv: mv.o
|
||||
$(LD) -o $@ $(LDFLAGS) $< -lc
|
||||
|
||||
uptime: uptime.o
|
||||
$(LD) -o $@ $(LDFLAGS) $< -lc
|
||||
|
||||
.cpp.o:
|
||||
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||
|
||||
|
|
31
Userland/uptime.cpp
Normal file
31
Userland/uptime.cpp
Normal 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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue