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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <time.h>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
FILE* fp = fopen("/dev/uptime", "r");
|
struct timespec tp;
|
||||||
if (!fp)
|
clock_gettime(CLOCK_MONOTONIC, &tp); // On Luna, CLOCK_MONOTONIC starts at boot.
|
||||||
{
|
|
||||||
perror("fopen");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char buf[BUFSIZ];
|
printf("up for %ld seconds\n", tp.tv_sec);
|
||||||
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);
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user