This commit is contained in:
Your Name 2024-07-06 14:31:29 +03:00
parent 5d24364332
commit 715a7d070d
2 changed files with 17 additions and 16 deletions

View File

@ -1,7 +1,7 @@
#ifndef _UTF8_STRLEN #ifndef _UTF8_STRLEN
#define _UTF8_STRLEN #define _UTF8_STRLEN
size_t utf8_strlen(const char *s) { size_t mu_strlen(const char *s) {
size_t i = 0; size_t i = 0;
while (*s++) while (*s++)

View File

@ -14,7 +14,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "mode_to_str.h" #include "mode_to_str.h"
#include "utf8_strlen.h" #include "mu_strlen.h"
#include "make_path.h" #include "make_path.h"
#include "get_stat.h" #include "get_stat.h"
#include "config.h" #include "config.h"
@ -62,7 +62,7 @@ struct d_node *stat_file(char *filename, const int lfile) {
return NULL; return NULL;
} }
file->full_name = filename; file->full_name = (lfile) ? strdup(filename) : filename;
file->name = strrchr(filename, '/'); file->name = strrchr(filename, '/');
if (file->name == NULL || lfile) if (file->name == NULL || lfile)
file->name = filename; 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) { void dfree(struct d_node **dir, const size_t files) {
for (size_t i = 0; i < files; i++) { 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); free(dir[i]->full_name);
if (dir[i] != NULL) if (dir[i] != NULL)
@ -218,7 +218,7 @@ int print(const struct d_node *node) {
printf("%s", color); printf("%s", color);
printf("%s", node->name); printf("%s", node->name);
ret += utf8_strlen(node->name); ret += mu_strlen(node->name);
if (c_flag && p_flag) if (c_flag && p_flag)
printf("\033[0m"); 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 */ /* Get max len */
size_t maxlen = 0; size_t maxlen = 0;
for (size_t i = 0; i < files; i++) { 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) if (len > maxlen)
maxlen = len; maxlen = len;
} }
@ -348,7 +348,6 @@ int ls_dir(const char *dir_name, const int label, const struct winsize w) {
return 1; return 1;
qsort(dir, files, sizeof(struct d_node *), sorter); qsort(dir, files, sizeof(struct d_node *), sorter);
if (struct_print(dir, files, w)) if (struct_print(dir, files, w))
ret = 1; ret = 1;
@ -374,36 +373,38 @@ int ls_files(int argc, char **argv, const struct winsize w) {
struct stat sb; struct stat sb;
if (mu_get_lstat("ls", argv[i], &sb)) { if (mu_get_lstat("ls", argv[i], &sb)) {
ret = 1; ret = 1;
argv[i] = NULL;
continue; continue;
} }
if (S_ISDIR(sb.st_mode)) if (S_ISDIR(sb.st_mode))
continue; 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) { if (tmp == NULL) {
dfree(file, files); dfree(file, files);
argv[i] = NULL;
return 1; return 1;
} }
file = tmp; file = tmp;
file[files] = stat_file(argv[i], 1); file[files] = stat_file(argv[i], 1);
if (file[files] == NULL) { if (file[files] == NULL) {
dfree(file, files); argv[i] = NULL;
continue;
ret = 1;
break;
} }
files++; files++;
argv[i] = NULL; argv[i] = NULL;
} }
if (files) {
qsort(file, files, sizeof(struct d_node *), sorter); qsort(file, files, sizeof(struct d_node *), sorter);
if (struct_print(file, files, w)) if (struct_print(file, files, w))
ret = 1; ret = 1;
}
dfree(file, files);
return ret; return ret;
} }