diff --git a/include/libmu/utf8_strlen.h b/include/libmu/mu_strlen.h similarity index 77% rename from include/libmu/utf8_strlen.h rename to include/libmu/mu_strlen.h index 29e2573..138e81f 100644 --- a/include/libmu/utf8_strlen.h +++ b/include/libmu/mu_strlen.h @@ -1,7 +1,7 @@ #ifndef _UTF8_STRLEN #define _UTF8_STRLEN -size_t utf8_strlen(const char *s) { +size_t mu_strlen(const char *s) { size_t i = 0; while (*s++) diff --git a/src/coreutils/ls/ls.c b/src/coreutils/ls/ls.c index 93e6a2a..4808929 100644 --- a/src/coreutils/ls/ls.c +++ b/src/coreutils/ls/ls.c @@ -14,7 +14,7 @@ #include #include #include "mode_to_str.h" -#include "utf8_strlen.h" +#include "mu_strlen.h" #include "make_path.h" #include "get_stat.h" #include "config.h" @@ -62,7 +62,7 @@ struct d_node *stat_file(char *filename, const int lfile) { return NULL; } - file->full_name = filename; + file->full_name = (lfile) ? strdup(filename) : filename; file->name = strrchr(filename, '/'); if (file->name == NULL || lfile) file->name = filename; @@ -75,7 +75,7 @@ struct d_node *stat_file(char *filename, const int lfile) { void dfree(struct d_node **dir, const size_t files) { for (size_t i = 0; i < files; i++) { - if (dir[i]->full_name != NULL) + if (dir[i] != NULL && dir[i]->full_name != NULL) free(dir[i]->full_name); if (dir[i] != NULL) @@ -218,7 +218,7 @@ int print(const struct d_node *node) { printf("%s", color); printf("%s", node->name); - ret += utf8_strlen(node->name); + ret += mu_strlen(node->name); if (c_flag && p_flag) printf("\033[0m"); @@ -233,7 +233,7 @@ int col_print(struct d_node **node, const size_t files, const struct winsize w) /* Get max len */ size_t maxlen = 0; for (size_t i = 0; i < files; i++) { - size_t len = utf8_strlen(node[i]->name); + size_t len = mu_strlen(node[i]->name); if (len > maxlen) maxlen = len; } @@ -348,7 +348,6 @@ int ls_dir(const char *dir_name, const int label, const struct winsize w) { return 1; qsort(dir, files, sizeof(struct d_node *), sorter); - if (struct_print(dir, files, w)) ret = 1; @@ -374,36 +373,38 @@ int ls_files(int argc, char **argv, const struct winsize w) { struct stat sb; if (mu_get_lstat("ls", argv[i], &sb)) { ret = 1; - + argv[i] = NULL; continue; } if (S_ISDIR(sb.st_mode)) continue; - struct d_node **tmp = realloc(file, sizeof(struct d_node *) * (files + 1)); + struct d_node **tmp = realloc(file, sizeof(struct d_node *) * (files + 2)); if (tmp == NULL) { dfree(file, files); + argv[i] = NULL; return 1; } file = tmp; file[files] = stat_file(argv[i], 1); if (file[files] == NULL) { - dfree(file, files); - - ret = 1; - break; + argv[i] = NULL; + continue; } files++; argv[i] = NULL; } - qsort(file, files, sizeof(struct d_node *), sorter); - if (struct_print(file, files, w)) - ret = 1; + if (files) { + qsort(file, files, sizeof(struct d_node *), sorter); + if (struct_print(file, files, w)) + ret = 1; + } + dfree(file, files); return ret; }