diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-07-06 01:56:43 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-07-06 01:56:43 +0200 |
| commit | 74b7f7a902d9916dddb07d6adb26c63e765a6349 (patch) | |
| tree | 7e95de445702c1ab83cd594ef1faf97a8a15fa73 /cftw.c | |
| parent | 36ff7d81432308ba430414180c0ab7a41af228ba (diff) | |
cftw.c: Check errors from readdir(3)
According to POSIX.1-2017, applications are advised to assign errno(3)
to 0 before a call to readdir(3), and compare errno(3) after the call to
check for errors.
Diffstat (limited to 'cftw.c')
| -rw-r--r-- | cftw.c | 14 |
1 files changed, 12 insertions, 2 deletions
@@ -14,7 +14,6 @@ int cftw(const char *const dirpath, int (*const fn)(const char *, { int ret = -1; DIR *const d = opendir(dirpath); - struct dirent *de; if (!d) { @@ -22,8 +21,19 @@ int cftw(const char *const dirpath, int (*const fn)(const char *, goto end; } - while ((de = readdir(d))) + for (;;) { + errno = 0; + struct dirent *const de = readdir(d); + + if (errno) + { + fprintf(stderr, "%s: readdir(3): %s\n", __func__, strerror(errno)); + goto end; + } + else if (!de) + break; + const char *const path = de->d_name; if (!strcmp(path, ".") || !strcmp(path, "..")) |
