Skip to content

Commit 85a014d

Browse files
committed
desktop: avoid using readdir()->d_type
...as it is non-POSIX and creates unnecessary build trouble on FreeBSD. Have timed before/after and cannot detect a difference in speed.
1 parent 38d08a6 commit 85a014d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

desktop.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdlib.h>
1212
#include <string.h>
1313
#include <strings.h>
14+
#include <sys/stat.h>
1415
#include <dirent.h>
1516
#include <stdbool.h>
1617
#include "desktop.h"
@@ -294,26 +295,31 @@ process_file(char *filename, const char *path)
294295
}
295296

296297
static void
297-
traverse_directory(const char *path)
298+
traverse_directory(const char *dirname)
298299
{
300+
char path[4096] = {0};
299301
struct dirent *entry;
300302
DIR *dp;
301303

302-
dp = opendir(path);
304+
dp = opendir(dirname);
303305
if (!dp) {
304306
return;
305307
}
306308
while ((entry = readdir(dp))) {
307-
if (entry->d_type == DT_DIR) {
309+
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
310+
311+
struct stat sb;
312+
stat(path, &sb);
313+
if (S_ISDIR(sb.st_mode)) {
308314
if (entry->d_name[0] != '.') {
309315
char new_path[PATH_MAX];
310316

311-
snprintf(new_path, PATH_MAX, "%s%s/", path,
317+
snprintf(new_path, PATH_MAX, "%s%s/", dirname,
312318
entry->d_name);
313319
traverse_directory(new_path);
314320
}
315-
} else if (entry->d_type == DT_REG || entry->d_type == DT_LNK) {
316-
process_file(entry->d_name, path);
321+
} else {
322+
process_file(entry->d_name, dirname);
317323
}
318324
}
319325
closedir(dp);

0 commit comments

Comments
 (0)