Skip to content

Commit 960c238

Browse files
authoredJun 21, 2024
Replaced deprecated readdir_r with readdir to improve portability and simplify the code (#39)
1 parent 9e1463e commit 960c238

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed
 

‎linux/cpu_memory_by_process.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ uint64 ReadTotalCPUUsage()
197197
void ReadCPUMemoryUsage(int sample)
198198
{
199199
FILE *fpstat;
200-
struct dirent *ent, dbuf;
200+
struct dirent *ent;
201201
char file_name[MAXPGPATH];
202202
long utime_ticks, stime_ticks;
203203
char process_name[MAXPGPATH] = {0};
@@ -227,7 +227,7 @@ void ReadCPUMemoryUsage(int sample)
227227
return;
228228
}
229229

230-
while (readdir_r(dirp, &dbuf, &ent) == 0)
230+
while ((ent = readdir(dirp)) != NULL)
231231
{
232232
memset(file_name, 0x00, MAXPGPATH);
233233

‎linux/system_stats_utils.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ bool read_process_status(int *active_processes, int *running_processes,
138138
{
139139
FILE *fpstat;
140140
DIR *dirp;
141-
struct dirent *ent, dbuf;
141+
struct dirent *ent;
142142
char file_name[MIN_BUFFER_SIZE];
143143
char process_type;
144144
unsigned int running_threads;
@@ -156,14 +156,11 @@ bool read_process_status(int *active_processes, int *running_processes,
156156
}
157157

158158
/* Read the proc directory for process status */
159-
while (readdir_r(dirp, &dbuf, &ent) == 0)
159+
while ((ent = readdir(dirp)) != NULL)
160160
{
161161
memset(file_name, 0x00, MIN_BUFFER_SIZE);
162162
process_type = '\0';
163163

164-
if (!ent)
165-
break;
166-
167164
/* Iterate only digit as name because it is process id */
168165
if (!isdigit(*ent->d_name))
169166
continue;
@@ -182,7 +179,7 @@ bool read_process_status(int *active_processes, int *running_processes,
182179

183180
if (process_type == 'R')
184181
running_pro++;
185-
else if(process_type == 'S' || process_type == 'D')
182+
else if (process_type == 'S' || process_type == 'D')
186183
sleeping_pro++;
187184
else if (process_type == 'T')
188185
stopped_pro++;
@@ -209,6 +206,7 @@ bool read_process_status(int *active_processes, int *running_processes,
209206
return true;
210207
}
211208

209+
212210
void ReadFileContent(const char *file_name, uint64 *data)
213211
{
214212
FILE *fp = NULL;

0 commit comments

Comments
 (0)
Please sign in to comment.