diff options
-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 | } |