diff options
author | Connor Lane Smith <cls@lubutu.com> | 2011-10-17 01:44:07 +0100 |
---|---|---|
committer | Connor Lane Smith <cls@lubutu.com> | 2011-10-17 01:44:07 +0100 |
commit | 7bbd4c56ebe1c91b90561e7f22e875f58e7facc9 (patch) | |
tree | f53c8dd308b6d8aea6d0fac5331bd5689d9b8342 | |
parent | c71abdc65c8a7ff9597f7b6b5bf60a59be174224 (diff) |
lsx: detect read errors
-rw-r--r-- | lsx.c | 16 |
1 files changed, 9 insertions, 7 deletions
@@ -1,5 +1,6 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | 1 | /* See LICENSE file for copyright and license details. */ |
2 | #include <dirent.h> | 2 | #include <dirent.h> |
3 | #include <errno.h> | ||
3 | #include <limits.h> | 4 | #include <limits.h> |
4 | #include <stdio.h> | 5 | #include <stdio.h> |
5 | #include <stdlib.h> | 6 | #include <stdlib.h> |
@@ -28,14 +29,15 @@ lsx(const char *dir) { | |||
28 | struct stat st; | 29 | struct stat st; |
29 | DIR *dp; | 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 | status = EXIT_FAILURE; | 38 | status = EXIT_FAILURE; |
33 | perror(dir); | 39 | perror(dir); |
34 | return; | ||
35 | } | 40 | } |
36 | while((d = readdir(dp))) | 41 | if(dp) |
37 | if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf | 42 | closedir(dp); |
38 | && stat(buf, &st) == 0 && S_ISREG(st.st_mode) && access(buf, X_OK) == 0) | ||
39 | puts(d->d_name); | ||
40 | closedir(dp); | ||
41 | } | 43 | } |