Skip to content

Commit

Permalink
Replaced deprecated readdir_r with readdir to improve portability and…
Browse files Browse the repository at this point in the history
… simplify the code (#39)
  • Loading branch information
lssno authored Jun 21, 2024
1 parent 9e1463e commit 960c238
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions linux/cpu_memory_by_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 4 additions & 6 deletions linux/system_stats_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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++;
Expand All @@ -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;
Expand Down

0 comments on commit 960c238

Please sign in to comment.