lsx: detect read errors
7bbd4c56
1 file(s) · +9 −7
| 1 | 1 | /* See LICENSE file for copyright and license details. */ |
|
| 2 | 2 | #include <dirent.h> |
|
| 3 | + | #include <errno.h> |
|
| 3 | 4 | #include <limits.h> |
|
| 4 | 5 | #include <stdio.h> |
|
| 5 | 6 | #include <stdlib.h> |
|
| 28 | 29 | struct stat st; |
|
| 29 | 30 | DIR *dp; |
|
| 30 | 31 | ||
| 31 | - | if(!(dp = opendir(dir))) { |
|
| 32 | + | for(dp = opendir(dir); dp && (d = readdir(dp)); errno = 0) |
|
| 33 | + | if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf |
|
| 34 | + | && access(buf, X_OK) == 0 && stat(buf, &st) == 0 && S_ISREG(st.st_mode)) |
|
| 35 | + | puts(d->d_name); |
|
| 36 | + | ||
| 37 | + | if(errno != 0) { |
|
| 32 | 38 | status = EXIT_FAILURE; |
|
| 33 | 39 | perror(dir); |
|
| 34 | - | return; |
|
| 35 | 40 | } |
|
| 36 | - | while((d = readdir(dp))) |
|
| 37 | - | if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf |
|
| 38 | - | && stat(buf, &st) == 0 && S_ISREG(st.st_mode) && access(buf, X_OK) == 0) |
|
| 39 | - | puts(d->d_name); |
|
| 40 | - | closedir(dp); |
|
| 41 | + | if(dp) |
|
| 42 | + | closedir(dp); |
|
| 41 | 43 | } |
|