edit for 42 and bug fix
This commit is contained in:
parent
69aefc764d
commit
9e57c380fd
|
@ -3,7 +3,7 @@
|
|||
-includeinc/main.h
|
||||
-Wall
|
||||
-Wextra
|
||||
-std=c23
|
||||
-std=c2x
|
||||
-iquoteinc
|
||||
-iquotelib
|
||||
-g
|
||||
|
|
19
inc/main.h
19
inc/main.h
|
@ -10,12 +10,12 @@
|
|||
#include <sys/ioctl.h>
|
||||
|
||||
typedef struct {
|
||||
bool l;
|
||||
bool a;
|
||||
bool R;
|
||||
bool r;
|
||||
bool t;
|
||||
bool dashdash;
|
||||
_Bool l;
|
||||
_Bool a;
|
||||
_Bool R;
|
||||
_Bool r;
|
||||
_Bool t;
|
||||
_Bool dashdash;
|
||||
int error[2];
|
||||
} flags_t;
|
||||
|
||||
|
@ -23,7 +23,7 @@ typedef struct {
|
|||
DIR *dir;
|
||||
char *path;
|
||||
char *name;
|
||||
bool is_file;
|
||||
_Bool is_file;
|
||||
struct stat stat;
|
||||
struct stat lstat;
|
||||
struct passwd *psswd;
|
||||
|
@ -33,6 +33,9 @@ typedef struct {
|
|||
extern flags_t flags;
|
||||
extern struct winsize win;
|
||||
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
// Parsing
|
||||
void get_flags(char **argv);
|
||||
void parsing(char **argv, t_list ***files, t_list ***dirs, i32 *max_size, i32 *len, i32 *error);
|
||||
|
@ -51,7 +54,7 @@ void space_between_files(t_list **files, dir_t *file);
|
|||
void strmode(mode_t mode, char *buf);
|
||||
void delete_directory(void *content);
|
||||
t_list **get_all_in(dir_t *dir);
|
||||
void add_sort(t_list **head, dir_t *new_dir, bool file_priority);
|
||||
void add_sort(t_list **head, dir_t *new_dir, _Bool file_priority);
|
||||
|
||||
// Free
|
||||
void free_files(t_list **files);
|
||||
|
|
|
@ -5,14 +5,14 @@ static void delete(t_list **deleted, void (*del)(void*));
|
|||
|
||||
void ft_lstremove_back(t_list **lst, void (*del)(void*))
|
||||
{
|
||||
if (lst == nullptr || del == nullptr)
|
||||
if (lst == NULL || del == NULL)
|
||||
return;
|
||||
|
||||
if ((*lst)->next == nullptr)
|
||||
if ((*lst)->next == NULL)
|
||||
return delete(lst, del);
|
||||
|
||||
t_list *cur = *lst;
|
||||
while (cur->next != nullptr && cur->next->next != nullptr)
|
||||
while (cur->next != NULL && cur->next->next != NULL)
|
||||
cur = cur->next;
|
||||
|
||||
delete(&(cur->next), del);
|
||||
|
@ -21,5 +21,5 @@ void ft_lstremove_back(t_list **lst, void (*del)(void*))
|
|||
static void delete(t_list **deleted, void (*del)(void*)) {
|
||||
del(*deleted);
|
||||
free(*deleted);
|
||||
*deleted = nullptr;
|
||||
*deleted = NULL;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
void ft_lstremove_front(t_list **lst, void (*del)(void*))
|
||||
{
|
||||
if (lst == nullptr || del == nullptr)
|
||||
if (lst == NULL || del == NULL)
|
||||
return;
|
||||
|
||||
t_list *next = (*lst)->next;
|
||||
|
|
|
@ -27,7 +27,9 @@ t_list **
|
|||
get_all_in(dir_t *dir) {
|
||||
struct dirent *in_dir;
|
||||
t_list **head = ft_calloc(1, sizeof(t_list*));
|
||||
while ((in_dir = readdir(dir->dir)) != nullptr) {
|
||||
if (!head)
|
||||
return NULL;
|
||||
while ((in_dir = readdir(dir->dir)) != NULL) {
|
||||
if (!flags.a && ft_strncmp(".", in_dir->d_name, 1) == 0)
|
||||
continue;
|
||||
remove_all_end_character(dir->path, '/');
|
||||
|
@ -36,9 +38,10 @@ get_all_in(dir_t *dir) {
|
|||
free(new_path);
|
||||
if (!new_dir)
|
||||
continue;
|
||||
new_dir->name = in_dir->d_name;
|
||||
free(new_dir->name);
|
||||
new_dir->name = ft_strdup(in_dir->d_name);
|
||||
|
||||
if (ft_strcmp(new_dir->name, "switchpro_rstick_click_md.png") == 0)
|
||||
if (ft_strcmp(new_dir->name, "\n") == 0)
|
||||
ft_printf("");
|
||||
|
||||
add_sort(head, new_dir, false);
|
||||
|
@ -47,13 +50,13 @@ get_all_in(dir_t *dir) {
|
|||
}
|
||||
|
||||
|
||||
static bool compare_name(char *s1, char *s2);
|
||||
static bool compare_time(dir_t *dir1, dir_t *dir2);
|
||||
static _Bool compare_name(char *s1, char *s2);
|
||||
static _Bool compare_time(dir_t *dir1, dir_t *dir2);
|
||||
|
||||
void
|
||||
add_sort(t_list **head, dir_t *new_dir, bool file_priority) {
|
||||
add_sort(t_list **head, dir_t *new_dir, _Bool file_priority) {
|
||||
t_list *cur = *head, *prev = NULL;
|
||||
for (; cur != nullptr; prev = cur, cur = cur->next) {
|
||||
for (; cur != NULL; prev = cur, cur = cur->next) {
|
||||
dir_t *cur_dir = ((dir_t *)cur->content);
|
||||
if (file_priority && !cur_dir->is_file && new_dir->is_file)
|
||||
break;
|
||||
|
@ -76,14 +79,14 @@ add_sort(t_list **head, dir_t *new_dir, bool file_priority) {
|
|||
new->next = cur;
|
||||
}
|
||||
|
||||
static bool compare_name(char *s1, char *s2) {
|
||||
static _Bool compare_name(char *s1, char *s2) {
|
||||
i32 cmp = ft_strcmp(s1, s2);
|
||||
if ((flags.r && cmp > 0) || (!flags.r && cmp < 0))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool compare_time(dir_t *dir1, dir_t *dir2) {
|
||||
static _Bool compare_time(dir_t *dir1, dir_t *dir2) {
|
||||
time_t dir1_sec = dir1->lstat.st_mtim.tv_sec;
|
||||
time_t dir2_sec = dir2->lstat.st_mtim.tv_sec;
|
||||
time_t dir1_nsec = dir1->lstat.st_mtim.tv_nsec;
|
||||
|
|
|
@ -8,7 +8,7 @@ void space_between_files(t_list **files, dir_t *file) {
|
|||
}
|
||||
|
||||
u32 max = 0, i = 0, index_file = 0;
|
||||
for (t_list *cur = *files; cur != nullptr; cur = cur->next) {
|
||||
for (t_list *cur = *files; cur != NULL; cur = cur->next) {
|
||||
dir_t *cur_file = (dir_t*)(cur)->content;
|
||||
max = MAX(ft_strlen(cur_file->name), max);
|
||||
if (ft_strcmp(cur_file->name, file->name) == 0)
|
||||
|
@ -16,12 +16,12 @@ void space_between_files(t_list **files, dir_t *file) {
|
|||
i++;
|
||||
}
|
||||
u32 col = MAX(win.ws_col / max, 1);
|
||||
u32 cal = index_file % col;
|
||||
u32 line = index_file % col;
|
||||
if (col >= i && index_file != i - 1)
|
||||
ft_printf(" ");
|
||||
else if (col >= i && index_file == i - 1)
|
||||
ft_printf("\n");
|
||||
else if (cal == col - 1 || index_file == i - 1)
|
||||
else if (line == col - 1 || index_file == i - 1)
|
||||
ft_printf("\n");
|
||||
else
|
||||
for (i = 0; i < max - ft_strlen(file->name) + 1; i++)
|
||||
|
@ -41,6 +41,8 @@ display_directory(dir_t *dir) {
|
|||
}
|
||||
else {
|
||||
t_list **files = get_all_in(dir);
|
||||
if (!files)
|
||||
return;
|
||||
display_files(files);
|
||||
free_files(files);
|
||||
}
|
||||
|
@ -52,11 +54,11 @@ display_files(t_list **files) {
|
|||
if (flags.l)
|
||||
return display_files_stat(files);
|
||||
else {
|
||||
for (t_list *cur = *files; cur != nullptr; cur = cur->next) {
|
||||
for (t_list *cur = *files; cur != NULL; cur = cur->next) {
|
||||
dir_t *dir = (dir_t*)(cur)->content;
|
||||
display_name(dir);
|
||||
drawed++;
|
||||
if (cur->next != nullptr)
|
||||
if (cur->next != NULL)
|
||||
space_between_files(files, dir);
|
||||
}
|
||||
if (drawed > 0)
|
||||
|
@ -83,12 +85,14 @@ display_recursive(dir_t *dir) {
|
|||
to_print_return_of_line();
|
||||
ft_printf("%s:\n", dir->path);
|
||||
t_list **files = get_all_in(dir);
|
||||
if (!files)
|
||||
return;
|
||||
display_files(files);
|
||||
for (t_list *cur = *files; cur != nullptr; cur = cur->next) {
|
||||
for (t_list *cur = *files; cur != NULL; cur = cur->next) {
|
||||
dir_t *cur_dir = (dir_t*)cur->content;
|
||||
if (!cur_dir->is_file
|
||||
&& ft_strcmp(".", cur_dir->name) != 0
|
||||
&& ft_strcmp("..", cur_dir->name) != 0)
|
||||
&& ft_strcmp(".", cur_dir->path + ft_strlen(cur_dir->path) - 1) != 0
|
||||
&& ft_strcmp("..", cur_dir->path + ft_strlen(cur_dir->path) - 2) != 0)
|
||||
display_recursive(cur_dir);
|
||||
}
|
||||
free_files(files);
|
||||
|
|
|
@ -17,7 +17,7 @@ display_files_stat(t_list **files) {
|
|||
i32 max_size = ft_get_size(((dir_t*)(*files)->content)->lstat.st_size);
|
||||
i32 total = 0;
|
||||
t_list *cur;
|
||||
for (cur = *files; cur != nullptr; cur = cur->next) {
|
||||
for (cur = *files; cur != NULL; cur = cur->next) {
|
||||
dir_t *dir = (dir_t*)(cur)->content;
|
||||
max_size = MAX(ft_get_size(dir->lstat.st_size), max_size);
|
||||
total += dir->lstat.st_blocks;
|
||||
|
@ -26,7 +26,7 @@ display_files_stat(t_list **files) {
|
|||
ft_printf("total %d\n", total / 2);
|
||||
|
||||
i32 drawed = 0;
|
||||
for (cur = *files; cur != nullptr; cur = cur->next) {
|
||||
for (cur = *files; cur != NULL; cur = cur->next) {
|
||||
dir_t *dir = (dir_t*)(cur)->content;
|
||||
display_single_stat(dir, max_size);
|
||||
drawed++;
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
void
|
||||
free_files(t_list **files) {
|
||||
for (t_list *cur = *files; cur != nullptr;) {
|
||||
for (t_list *cur = *files; cur != NULL;) {
|
||||
dir_t *file = (dir_t*)cur->content;
|
||||
if (!file->is_file)
|
||||
closedir(file->dir);
|
||||
free(file->path);
|
||||
file->path = NULL;
|
||||
if (file->name)
|
||||
free(file->name);
|
||||
free(file);
|
||||
|
||||
t_list *next = cur->next;
|
||||
|
|
17
src/main.c
17
src/main.c
|
@ -4,7 +4,6 @@ struct winsize win;
|
|||
|
||||
int
|
||||
main(int argc, char **argv) {
|
||||
|
||||
if (argc > 1) {
|
||||
get_flags(argv);
|
||||
if (flags.error[0] != -1) {
|
||||
|
@ -18,12 +17,22 @@ main(int argc, char **argv) {
|
|||
errno = 0;
|
||||
|
||||
t_list **files = ft_calloc(ft_strslen(argv) + 1, sizeof(t_list*));
|
||||
if (!files)
|
||||
return 1;
|
||||
t_list **dirs = ft_calloc(ft_strslen(argv) + 1, sizeof(t_list*));
|
||||
if (!dirs) {
|
||||
free(files);
|
||||
return 1;
|
||||
}
|
||||
i32 max_size = 0, len = 0, error = 0;
|
||||
parsing(argv, &files, &dirs, &max_size, &len, &error);
|
||||
|
||||
if (len == 0 && error == 0)
|
||||
add_sort(dirs, get_directory_files("."), true);
|
||||
if (len == 0 && error == 0) {
|
||||
dir_t *default_dir = get_directory_files(".");
|
||||
if (!default_dir)
|
||||
return 1;
|
||||
add_sort(dirs, default_dir, true);
|
||||
}
|
||||
|
||||
i32 left = len;
|
||||
if (ft_lstsize(*files) > 0)
|
||||
|
@ -33,7 +42,7 @@ main(int argc, char **argv) {
|
|||
if (left != len && left > 0)
|
||||
ft_printf("\n");
|
||||
|
||||
for (t_list *cur = *dirs; cur != nullptr; cur = cur->next) {
|
||||
for (t_list *cur = *dirs; cur != NULL; cur = cur->next) {
|
||||
dir_t *dir = (dir_t*)cur->content;
|
||||
if (len > 1 && !flags.R)
|
||||
ft_printf("%s:\n", dir->path);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
void
|
||||
get_flags(char **argv) {
|
||||
for (i32 i = 1; argv[i] != nullptr; i++) {
|
||||
for (i32 i = 1; argv[i] != NULL; i++) {
|
||||
if (ft_strcmp(argv[i], "--") == 0)
|
||||
return;
|
||||
if (argv[i][0] == '-') {
|
||||
|
@ -28,13 +28,13 @@ get_flags(char **argv) {
|
|||
|
||||
void
|
||||
parsing(char **argv, t_list ***files, t_list ***dirs, i32 *max_size, i32 *len, i32 *error) {
|
||||
for (i32 i = 0; argv[i + 1] != nullptr; i++) {
|
||||
if (ft_strcmp(argv[i + 1], "--") == 0) {
|
||||
for (i32 i = 0; argv[i + 1] != NULL; i++) {
|
||||
if (ft_strcmp(argv[i + 1], "--") == 0 && !flags.dashdash) {
|
||||
flags.dashdash = true;
|
||||
continue;
|
||||
}
|
||||
dir_t *dir = get_directory_files(argv[i + 1]);
|
||||
if (dir != nullptr) {
|
||||
if (dir != NULL) {
|
||||
(*len)++;
|
||||
if (dir->is_file) {
|
||||
*max_size = MAX(ft_get_size(dir->lstat.st_size), *max_size);
|
||||
|
@ -53,20 +53,19 @@ static void*
|
|||
free_dir_return_null(dir_t *dir) {
|
||||
free(dir->path);
|
||||
free(dir);
|
||||
return nullptr;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dir_t *
|
||||
get_directory_files(char *path) {
|
||||
dir_t *dir = nullptr;
|
||||
|
||||
if (!flags.dashdash)
|
||||
if (path[0] == '-' && ft_strlen(path) != 1)
|
||||
return nullptr;
|
||||
return NULL;
|
||||
|
||||
dir = ft_calloc(1, sizeof(dir_t));
|
||||
dir_t *dir = ft_calloc(1, sizeof(dir_t));
|
||||
if (!dir)
|
||||
return NULL;
|
||||
dir->path = ft_strdup(path);
|
||||
dir->name = dir->path;
|
||||
dir->name = ft_strdup(path);
|
||||
stat(path, &dir->stat);
|
||||
lstat(path, &dir->lstat);
|
||||
dir->psswd = getpwuid(dir->stat.st_uid);
|
||||
|
@ -83,7 +82,7 @@ get_directory_files(char *path) {
|
|||
}
|
||||
|
||||
dir->dir = opendir(path);
|
||||
if (dir->dir == nullptr) {
|
||||
if (dir->dir == NULL) {
|
||||
if (errno == EACCES)
|
||||
ft_printf_fd(2, "Permission denied\n");
|
||||
else if (errno == ENOENT)
|
||||
|
|
Loading…
Reference in a new issue