uptime: Use clock_gettime instead of reading from /dev/uptime
This commit is contained in:
parent
324fb42ee2
commit
e2b5c1bfdd
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user