From 942072d72f169ebe9c549e5347d26abdede8c07e Mon Sep 17 00:00:00 2001 From: SeekBytes Date: Sat, 30 Apr 2022 17:02:10 +0200 Subject: [PATCH 1/3] Create uptime.c --- programs/uptime.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 programs/uptime.c 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; +} From b78d09fc24ebfb9c56b837ccc5095da1922f4458 Mon Sep 17 00:00:00 2001 From: SeekBytes Date: Sat, 30 Apr 2022 17:02:38 +0200 Subject: [PATCH 2/3] Added uptime command to print the uptime of MentOS since boot --- programs/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/programs/CMakeLists.txt b/programs/CMakeLists.txt index 234a912..0438e8d 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. From 630e80f7243785bca46438239fc1aebd4dca59ae Mon Sep 17 00:00:00 2001 From: SeekBytes Date: Sun, 1 May 2022 12:05:05 +0200 Subject: [PATCH 3/3] Missing close --- programs/uptime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/uptime.c b/programs/uptime.c index 5d283c3..d3846f3 100644 --- a/programs/uptime.c +++ b/programs/uptime.c @@ -29,6 +29,6 @@ int main(){ 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; }