Skip to content

Commit 1c24cb3

Browse files
apocelipesCarterLi
authored andcommitted
refactor: simplify parsing
1 parent 0662af1 commit 1c24cb3

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

src/detection/cpuusage/cpuusage_linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
4141
};
4242
}
4343
else
44-
break; // because we read the whole /proc/stat, we can safely quit when the line does not start with "cpuN"
44+
break;
4545
start = token + 1;
4646
}
4747

src/detection/memory/memory_linux.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ const char* ffDetectMemory(FFMemoryResult* ram)
2121

2222
char *token = NULL;
2323
if((token = strstr(buf, "MemTotal:")) != NULL)
24-
sscanf(token, "MemTotal: %" PRIu64, &memTotal);
24+
memTotal = strtoul(token + strlen("MemTotal:"), NULL, 10);
2525

2626
if((token = strstr(buf, "MemFree:")) != NULL)
27-
sscanf(token, "MemFree: %" PRIu64, &memFree);
27+
memFree = strtoul(token + strlen("MemFree:"), NULL, 10);
2828

2929
if((token = strstr(buf, "Buffers:")) != NULL)
30-
sscanf(token, "Buffers: %" PRIu64, &buffers);
30+
buffers = strtoul(token + strlen("Buffers:"), NULL, 10);
3131

3232
if((token = strstr(buf, "Cached:")) != NULL)
33-
sscanf(token, "Cached: %" PRIu64, &cached);
33+
cached = strtoul(token + strlen("Cached:"), NULL, 10);
3434

3535
if((token = strstr(buf, "Shmem:")) != NULL)
36-
sscanf(token, "Shmem: %" PRIu64, &shmem);
36+
shmem = strtoul(token + strlen("Shmem:"), NULL, 10);
3737

3838
if((token = strstr(buf, "SReclaimable:")) != NULL)
39-
sscanf(token, "SReclaimable: %" PRIu64, &sReclaimable);
39+
sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10);
4040

4141
ram->bytesTotal = memTotal * 1024lu;
4242
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * 1024lu;

src/detection/swap/swap_linux.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const char* ffDetectSwap(FFSwapResult* swap)
1818

1919
char *token = NULL;
2020
if ((token = strstr(buf, "SwapTotal:")) != NULL)
21-
sscanf(token, "SwapTotal: %" PRIu64, &swapTotal);
21+
swapTotal = strtoul(token + strlen("SwapTotal:"), NULL, 10);
2222

2323
if ((token = strstr(buf, "SwapFree:")) != NULL)
24-
sscanf(token, "SwapFree: %" PRIu64, &swapFree);
24+
swapFree = strtoul(token + strlen("SwapFree:"), NULL, 10);
2525

2626
swap->bytesTotal = swapTotal * 1024lu;
2727
swap->bytesUsed = (swapTotal - swapFree) * 1024lu;

src/detection/uptime/uptime_linux.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ const char* ffDetectUptime(FFUptimeResult* result)
1313
return "ffReadFileData(\"/proc/uptime\", sizeof(buf) - 1, buf) failed";
1414
buf[nRead] = '\0';
1515

16-
double sec;
17-
if(sscanf(buf, "%lf", &sec) > 0)
16+
char *err = NULL;
17+
double sec = strtod(buf, &err);
18+
if(err != buf)
1819
result->uptime = (uint64_t) (sec * 1000);
1920
else
20-
return "sscanf(buf.chars, \"%lf\", &sec) failed";
21+
return "strtod(buf, &err) failed";
2122

2223
result->bootTime = ffTimeGetNow() + result->uptime;
2324

0 commit comments

Comments
 (0)