X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=dus.git;a=blobdiff_plain;f=dus.c;h=e00d3df9325222a459344b52d49e53594f44ae3f;hp=63cc183c30acadfd523cd1002953f6c7621b128b;hb=b2a5aa36454c6b0d66eb0f1451ac0e07aad3b242;hpb=540879d0b9cdd2961d56c601156f8ed7d589cea2 diff --git a/dus.c b/dus.c index 63cc183..e00d3df 100644 --- a/dus.c +++ b/dus.c @@ -22,7 +22,7 @@ * */ -#define VERSION_NUMBER "1.1" +#define VERSION_NUMBER "1.2" #define _BSD_SOURCE @@ -167,6 +167,26 @@ struct entry_node *push_entry(char *name, struct entry_node *head) { return result; } +struct entry_node *push_dir_content(char *name, struct entry_node *head) { + char subname[PATH_MAX]; + DIR *dir; + struct dirent *dir_e; + dir = opendir(name); + if(dir) { + while((dir_e = readdir(dir))) { + if(!ignore_entry(dir_e->d_name)) { + snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name); + head = push_entry(subname, head); + } + } + closedir(dir); + } else { + fprintf(stderr, "Can not open directory %s: %s\n", name, strerror(errno)); + exit (EXIT_FAILURE); + } + return head; +} + void destroy_entry_list(struct entry_node *node) { struct entry_node *next; while(node) { @@ -429,23 +449,15 @@ int main(int argc, char **argv) { if (optind < argc) { while (optind < argc) { - root = push_entry(argv[optind++], root); - } - } else { - DIR *dir; - struct dirent *dir_e; - dir = opendir("."); - if(dir) { - while((dir_e = readdir(dir))) { - if(!ignore_entry(dir_e->d_name)) { - root = push_entry(dir_e->d_name, root); - } + if(argv[optind][strlen(argv[optind]) - 1] == '/') { + argv[optind][strlen(argv[optind]) - 1] = '\0'; + root = push_dir_content(argv[optind++], root); + } else { + root = push_entry(argv[optind++], root); } - closedir(dir); - } else { - fprintf(stderr, "Can not open ./: %s\n", strerror(errno)); - exit (EXIT_FAILURE); } + } else { + root = push_dir_content(".", root); } if(isatty(STDOUT_FILENO) &&