Merge pull request #13 from seekbytes/uptime

Added uptime command
This commit is contained in:
Enrico Fraccaroli
2022-07-07 13:10:42 -04:00
committed by GitHub
2 changed files with 35 additions and 0 deletions
+1
View File
@@ -68,6 +68,7 @@ set(PROGRAMS
sleep.c
man.c
edit.c
uptime.c
)
foreach(PROGRAM ${PROGRAMS})
# Prepare the program name.
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/unistd.h>
#include <sys/stat.h>
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;
}