diff --git a/programs/CMakeLists.txt b/programs/CMakeLists.txt index d998277..ffc1890 100644 --- a/programs/CMakeLists.txt +++ b/programs/CMakeLists.txt @@ -68,6 +68,7 @@ set(PROGRAMS sleep.c man.c edit.c + uptime.c ) foreach(PROGRAM ${PROGRAMS}) # Prepare the program name. diff --git a/programs/uptime.c b/programs/uptime.c new file mode 100644 index 0000000..d3846f3 --- /dev/null +++ b/programs/uptime.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +int main(){ + int uptime_fd = open("/proc/uptime", O_RDONLY, 0600); + if(uptime_fd == -1){ + printf("Impossible to retrieve /proc/uptime file"); + return -1; + } + + // Read the content of /proc/uptime + + char buffer[64]; + if(read(uptime_fd, buffer, sizeof(char)*64) == -1){ + printf("Impossible to read /proc/uptime content"); + return -1; + } + + int uptime = atoi(buffer); + + // Transform uptime into hours, days, mins, and seconds + + int updays = uptime / 86400; + int uphours = (uptime - (updays * 86400)) / 3600; + int upmins = (uptime - (updays * 86400) - (uphours * 3600)) / 60; + int upseconds = (uptime - (updays * 86400) - (uphours * 3600) - (upmins * 60)); + + printf("Days: %d Hours: %d Minutes: %d Seconds: %d \n", updays, uphours, upmins, upseconds); + close(uptime_fd); + return 0; +}