diff --git a/programs/uptime.c b/programs/uptime.c new file mode 100644 index 0000000..5d283c3 --- /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); + + return 0; +}