commit b77c0aff24f7cd4d1ae2ea0aa74aae8a0b8f8ebc
parent d266c91f54877b6b278fcc3b7208671828276078
Author: qorg11 <qorg@vxempire.xyz>
Date: Tue, 29 Dec 2020 05:13:21 +0100
* Cleaned code in the recursive thing
* Print errors if any
* Fixed a code copypaste?
Diffstat:
M | src/ls.c | | | 76 | ++++++++++++++++++++++++++++++++++++++-------------------------------------- |
1 file changed, 38 insertions(+), 38 deletions(-)
diff --git a/src/ls.c b/src/ls.c
@@ -2,20 +2,24 @@
#include <dirent.h>
#include <getopt.h>
#include <string.h>
+#include <errno.h>
int
recursive_list_dirs(const char *directory)
{
-
- DIR *dir;
- struct dirent *ent;
- if (!(dir = opendir(directory)))
+ DIR *dir = opendir(directory);
+ if(dir == NULL) {
+ fprintf(stderr,"Error opening directory: %s\n",strerror(errno));
+ puts(directory);
return -1;
+ }
+ struct dirent *ent;
+
while((ent = readdir(dir)) != NULL) {
if(ent->d_type == DT_DIR) {
char path[1024];
- if (strcmp(ent->d_name, ".") == 0||strcmp(ent->d_name, "..") == 0)
+ if (!strcmp(ent->d_name, ".")||!strcmp(ent->d_name, ".."))
continue;
snprintf(path, sizeof(path), "%s/%s", directory, ent->d_name);
printf("%s/%s\n",path,ent->d_name);
@@ -31,61 +35,57 @@ recursive_list_dirs(const char *directory)
int
main(int argc, char *argv[])
{
- int c;
- int all, show_slash, show_line, recursive;
- all = show_slash = show_line = recursive = 0;
+ int c;
+ int all, show_slash, show_line, recursive;
+ all = show_slash = show_line = recursive = 0;
- while ((c = getopt(argc,argv,"lapR")) != -1)
- {
- switch(c)
- {
- case 'a':
- all = 1;
- break;
- case 'p':
- show_slash = 1;
- break;
+ while ((c = getopt(argc,argv,"lapR")) != -1)
+ {
+ switch(c)
+ {
+ case 'a':
+ all = 1;
+ break;
+ case 'p':
+ show_slash = 1;
+ break;
case 'l':
show_line = 1;
break;
case 'R':
recursive = 1;
break;
- }
+ }
- }
- if(recursive)
- {
- recursive_list_dirs(argv[1]);
}
char directory[256];
if(!argv[optind])
- strcpy(directory,".");
+ strcpy(directory,"./");
else strcpy(directory,argv[optind]); /* Very dirty code, i'll fix it
- * later */
- DIR *dir = opendir(directory);
- struct dirent *ent;
+ * later */
+ DIR *dir = opendir(directory);
+ struct dirent *ent;
if(recursive)
{
recursive_list_dirs(directory);
return 0;
}
- if(dir != NULL)
- {
- while((ent = readdir(dir)) != NULL)
- {
- if(ent->d_name[0] == '.' && !all)
- continue;
+ if(dir != NULL)
+ {
+ while((ent = readdir(dir)) != NULL)
+ {
+ if(ent->d_name[0] == '.' && !all)
+ continue;
if(!show_line) {
if(ent->d_type == DT_DIR && show_slash)
printf("%s/ ",ent->d_name);
else printf("%s ",ent->d_name);
}
else printf("%s\n",ent->d_name);
- }
- }
- puts("");
- closedir(dir);
+ }
+ }
+ puts("");
+ closedir(dir);
- return 0;
+ return 0;
}