Files
fastfetch/src/detection/uptime/uptime_linux.c
T

39 lines
949 B
C
Raw Normal View History

2022-10-13 10:39:11 +08:00
#include "uptime.h"
2023-09-12 14:50:38 +08:00
#include "common/time.h"
2023-11-24 09:45:53 +08:00
#include "common/io/io.h"
#include <inttypes.h>
2022-12-28 17:18:08 +08:00
2023-09-08 11:08:29 +08:00
const char* ffDetectUptime(FFUptimeResult* result)
2022-10-13 10:39:11 +08:00
{
2024-03-02 18:33:14 +08:00
#ifndef __ANDROID__ // cat: /proc/uptime: Permission denied
2023-11-24 09:45:53 +08:00
// #620
char buf[64];
ssize_t nRead = ffReadFileData("/proc/uptime", sizeof(buf) - 1, buf);
2024-03-02 18:33:14 +08:00
if(nRead > 0)
{
buf[nRead] = '\0';
2023-09-08 11:08:29 +08:00
2024-03-02 18:33:14 +08:00
char *err = NULL;
double sec = strtod(buf, &err);
if(err != buf)
{
result->uptime = (uint64_t) (sec * 1000);
result->bootTime = ffTimeGetNow() + result->uptime;
return NULL;
}
}
2023-09-08 11:08:29 +08:00
2024-03-02 18:33:14 +08:00
#endif
struct timespec uptime;
if (clock_gettime(CLOCK_BOOTTIME, &uptime) != 0)
return "clock_gettime(CLOCK_BOOTTIME) failed";
result->uptime = (uint64_t) uptime.tv_sec * 1000 + (uint64_t) uptime.tv_nsec / 1000000;
2023-09-12 14:50:38 +08:00
result->bootTime = ffTimeGetNow() + result->uptime;
2023-09-08 11:08:29 +08:00
2023-04-25 19:39:35 +08:00
return NULL;
2022-10-13 10:39:11 +08:00
}