merge lsx -> default
5e0156c0
1 file(s) · +13 −8
| 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> |
|
| 7 | 8 | #include <sys/stat.h> |
|
| 8 | 9 | ||
| 9 | 10 | static void lsx(const char *dir); |
|
| 11 | + | ||
| 12 | + | static int status = EXIT_SUCCESS; |
|
| 10 | 13 | ||
| 11 | 14 | int |
|
| 12 | 15 | main(int argc, char *argv[]) { |
|
| 16 | 19 | lsx("."); |
|
| 17 | 20 | else for(i = 1; i < argc; i++) |
|
| 18 | 21 | lsx(argv[i]); |
|
| 19 | - | return EXIT_SUCCESS; |
|
| 22 | + | return status; |
|
| 20 | 23 | } |
|
| 21 | 24 | ||
| 22 | 25 | void |
|
| 26 | 29 | struct stat st; |
|
| 27 | 30 | DIR *dp; |
|
| 28 | 31 | ||
| 29 | - | 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) { |
|
| 38 | + | status = EXIT_FAILURE; |
|
| 30 | 39 | perror(dir); |
|
| 31 | - | return; |
|
| 32 | 40 | } |
|
| 33 | - | while((d = readdir(dp))) |
|
| 34 | - | if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf |
|
| 35 | - | && !stat(buf, &st) && S_ISREG(st.st_mode) && access(buf, X_OK) == 0) |
|
| 36 | - | puts(d->d_name); |
|
| 37 | - | closedir(dp); |
|
| 41 | + | if(dp) |
|
| 42 | + | closedir(dp); |
|
| 38 | 43 | } |
|