From 960c238e258913fef524ebb710e2b4befc7984d3 Mon Sep 17 00:00:00 2001 From: Alysson Oliveira Date: Fri, 21 Jun 2024 02:19:21 -0300 Subject: [PATCH] Replaced deprecated readdir_r with readdir to improve portability and simplify the code (#39) --- linux/cpu_memory_by_process.c | 4 ++-- linux/system_stats_utils.c | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/linux/cpu_memory_by_process.c b/linux/cpu_memory_by_process.c index 88ee631..6b27d26 100644 --- a/linux/cpu_memory_by_process.c +++ b/linux/cpu_memory_by_process.c @@ -197,7 +197,7 @@ uint64 ReadTotalCPUUsage() void ReadCPUMemoryUsage(int sample) { FILE *fpstat; - struct dirent *ent, dbuf; + struct dirent *ent; char file_name[MAXPGPATH]; long utime_ticks, stime_ticks; char process_name[MAXPGPATH] = {0}; @@ -227,7 +227,7 @@ void ReadCPUMemoryUsage(int sample) return; } - while (readdir_r(dirp, &dbuf, &ent) == 0) + while ((ent = readdir(dirp)) != NULL) { memset(file_name, 0x00, MAXPGPATH); diff --git a/linux/system_stats_utils.c b/linux/system_stats_utils.c index 2be6e64..9703d3c 100644 --- a/linux/system_stats_utils.c +++ b/linux/system_stats_utils.c @@ -138,7 +138,7 @@ bool read_process_status(int *active_processes, int *running_processes, { FILE *fpstat; DIR *dirp; - struct dirent *ent, dbuf; + struct dirent *ent; char file_name[MIN_BUFFER_SIZE]; char process_type; unsigned int running_threads; @@ -156,14 +156,11 @@ bool read_process_status(int *active_processes, int *running_processes, } /* Read the proc directory for process status */ - while (readdir_r(dirp, &dbuf, &ent) == 0) + while ((ent = readdir(dirp)) != NULL) { memset(file_name, 0x00, MIN_BUFFER_SIZE); process_type = '\0'; - if (!ent) - break; - /* Iterate only digit as name because it is process id */ if (!isdigit(*ent->d_name)) continue; @@ -182,7 +179,7 @@ bool read_process_status(int *active_processes, int *running_processes, if (process_type == 'R') running_pro++; - else if(process_type == 'S' || process_type == 'D') + else if (process_type == 'S' || process_type == 'D') sleeping_pro++; else if (process_type == 'T') stopped_pro++; @@ -209,6 +206,7 @@ bool read_process_status(int *active_processes, int *running_processes, return true; } + void ReadFileContent(const char *file_name, uint64 *data) { FILE *fp = NULL;