uptime: Use clock_gettime instead of reading from /dev/uptime

This commit is contained in:
apio 2022-10-30 09:09:03 +01:00
parent 324fb42ee2
commit e2b5c1bfdd

View File

@ -1,27 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
FILE* fp = fopen("/dev/uptime", "r");
if (!fp)
{
perror("fopen");
return 1;
}
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp); // On Luna, CLOCK_MONOTONIC starts at boot.
char buf[BUFSIZ];
fgets(buf, sizeof(buf), fp);
if (ferror(fp))
{
perror("fgets");
return 1;
}
long ms_uptime = atol(buf);
printf("up for %ld seconds\n", ms_uptime / 1000);
fclose(fp);
printf("up for %ld seconds\n", tp.tv_sec);
}