From f5860b88e14d7f8c7ce61f670ea2dbacfc728a20 Mon Sep 17 00:00:00 2001 From: Gregory Tertyshny Date: Sat, 1 Apr 2017 21:16:41 +0300 Subject: [PATCH] broke everything --- CMakeLists.txt | 2 +- inc/ft_ls.h | 1 + libft/ft_strdup.c | 16 +++++++++---- src/ft_ls.c | 58 ++++++++++++++++++++++++++++++----------------- src/main.c | 27 +++++++++++++++++++--- src/output.c | 4 ++-- src/sort.c | 6 ++--- 7 files changed, 79 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fc3f7ed..143ec37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.6) project(ft_ls) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -O3 -g") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -g") link_directories(libft) # libraries include_directories(inc libft/includes) # headers diff --git a/inc/ft_ls.h b/inc/ft_ls.h index 4ac5a94..d62613a 100644 --- a/inc/ft_ls.h +++ b/inc/ft_ls.h @@ -24,6 +24,7 @@ # include # include # include +# include # define FILS 0 # define DIRS 1 diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c index 3a8468f..438b89a 100644 --- a/libft/ft_strdup.c +++ b/libft/ft_strdup.c @@ -17,10 +17,16 @@ char *ft_strdup(const char *s1) char *new; size_t size; - size = ft_strlen(s1); - if (!(new = (char *)malloc(sizeof(char) * (size + 1)))) - return (0); - ft_memcpy(new, s1, size); - *(new + size) = '\0'; + new = 0; + if (s1) + { + if ((size = ft_strlen(s1)) != 0) + { + if (!(new = (char *)malloc(sizeof(char) * (size + 1)))) + return (0); + ft_memcpy(new, s1, size); + *(new + size) = '\0'; + } + } return (new); } diff --git a/src/ft_ls.c b/src/ft_ls.c index 294939b..c95edc8 100644 --- a/src/ft_ls.c +++ b/src/ft_ls.c @@ -19,8 +19,7 @@ int is_hidden(char *path) else if (*(end + 1) == '.') return (1); } - else - if (*end == '.') + else if (*end == '.') return (1); return (0); } @@ -114,7 +113,7 @@ char *get_time(t_dir_elm *d_elm) char *time; char *time_tmp; - time_tmp = ctime(&(d_elm->stat_copy->st_mtim.tv_sec)); + time_tmp = ctime(&(d_elm->stat_copy->st_mtimespec.tv_sec)); time_tmp[16] = 0; time = ft_strdup(time_tmp + 4); return (time); @@ -145,12 +144,14 @@ void to_null(t_dir_elm *de) de->g_name = NULL; de->u_name = NULL; de->m_time = NULL; + de->elm_name = NULL; } t_list *read_stat(char *full_path, char *filename, t_ft_ls *s_ls) { t_dir_elm de; - char *tmp_file_path;; + char *tmp_file_path; + t_list *node; to_null(&de); de.elm_name = ft_strdup(filename); @@ -171,14 +172,15 @@ t_list *read_stat(char *full_path, char *filename, t_ft_ls *s_ls) get_info_from_stat(&de); set_columns_padd(&de); } - return (ft_lstnew(&de, sizeof(t_dir_elm))); + node = ft_lstnew(&de, sizeof(t_dir_elm)); + return (node); } t_list *init_content_node(char *full_path, char *filename, t_ft_ls *s_ls) { if (s_ls->flgs->a) return (read_stat(full_path, filename, s_ls)); - else if (!is_hidden(filename) || ft_strcmp(filename, ".") == 0) + else if (!is_hidden(filename) || ft_strcmp(filename, "./") == 0) return (read_stat(full_path, filename, s_ls)); return (NULL); } @@ -198,7 +200,7 @@ t_list *find_dirs(char *full_path, t_ft_ls *s_ls, t_list *curr_dir_content) { if (s_ls->flgs->a) ft_lstadd(&dirs, ft_lstnew(dir_elm, sizeof(t_dir_elm))); - else if (*dir_elm->elm_name != '.') + else if (!is_hidden(dir_elm->elm_name)) ft_lstadd(&dirs, ft_lstnew(dir_elm, sizeof(t_dir_elm))); } curr_dir_content = curr_dir_content->next; @@ -220,7 +222,12 @@ void get_padding_and_blocks(t_ft_ls *s_ls, t_dir_elm *d_elem) if (d_elem->cols_padd.size_col > s_ls->max_cols_padd->size_col) s_ls->max_cols_padd->size_col = d_elem->cols_padd.size_col; if (d_elem->stat_copy) - s_ls->dir_content_total += d_elem->stat_copy->st_blocks; + { + if (s_ls->flgs->a) + s_ls->dir_content_total += d_elem->stat_copy->st_blocks * 2; + else if (!is_hidden(d_elem->elm_name)) + s_ls->dir_content_total += d_elem->stat_copy->st_blocks * 2; + } } } @@ -230,16 +237,15 @@ void extract_content(char *full_path, t_ft_ls *s_ls, t_list **dir_content) struct dirent *de; t_list *lst_elm; - *dir_content = NULL; if ((dp = opendir(full_path))) { while ((de = readdir(dp))) { - if ((lst_elm = init_content_node(full_path, de->d_name, s_ls))) - { - get_padding_and_blocks(s_ls, lst_elm->content); - ft_lstadd(dir_content, lst_elm); - } + if ((lst_elm = init_content_node(full_path, de->d_name, s_ls))) + { + get_padding_and_blocks(s_ls, lst_elm->content); + ft_lstadd(dir_content, lst_elm); + } } closedir(dp); } @@ -271,12 +277,17 @@ t_list *operands_parse(t_list *ops, t_ft_ls *s_ls) tmp_rnr = ops; while (tmp_rnr) { + if (ft_strcmp(tmp_rnr->content, ".") == 0) + { + free(tmp_rnr->content); + tmp_rnr->content = ft_strdup("./"); + } if ((lst_elm = init_content_node(NULL, tmp_rnr->content, s_ls))) { get_padding_and_blocks(s_ls, lst_elm->content); ft_lstadd(&root, lst_elm); - tmp_rnr = tmp_rnr->next; } + tmp_rnr = tmp_rnr->next; } return (root); } @@ -286,6 +297,7 @@ void free_struct_lst(t_list *curr_cont) { t_dir_elm *tmp; t_list *tmp1; + while (curr_cont) { tmp1 = curr_cont->next; @@ -293,11 +305,11 @@ void free_struct_lst(t_list *curr_cont) { tmp = (t_dir_elm *)curr_cont->content; free(tmp->attr_str); + free(tmp->link_name); free(tmp->elm_name); free(tmp->g_name); free(tmp->u_name); free(tmp->m_time); - free(tmp->link_name); free(tmp->stat_copy); free(tmp); } @@ -317,7 +329,9 @@ void ft_ls_dir(t_ft_ls *s_ls, t_list *root, char *parent_dir) { char *cur_path; t_list *curr_dir_content; + t_list *fuckin_head; + fuckin_head = root; curr_dir_content = NULL; sort_content(&root, s_ls->flgs); reset_s_ls(s_ls); @@ -329,12 +343,14 @@ void ft_ls_dir(t_ft_ls *s_ls, t_list *root, char *parent_dir) { sort_content(&curr_dir_content, s_ls->flgs); print_content(s_ls, curr_dir_content, cur_path); + if (s_ls->flgs->R) + { + ft_ls_dir(s_ls, find_dirs(cur_path, s_ls, curr_dir_content), cur_path); + free_struct_lst(curr_dir_content); + } } - if (s_ls->flgs->R) - ft_ls_dir(s_ls, find_dirs(cur_path, s_ls, curr_dir_content), cur_path); - root = root->next; free(cur_path); + root = root->next; } - free_struct_lst(curr_dir_content); - free_struct_lst(root); + free_struct_lst(fuckin_head); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index cefbb16..dd8d7bf 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,24 @@ // complide with -O3 flag!! + +void free_str_lst(t_list *curr_cont) +{ + char *tmp; + t_list *tmp1; + while (curr_cont) + { + tmp1 = curr_cont->next; + if (curr_cont->content) + { + tmp = (char *)curr_cont->content; + free(tmp); + } + free(curr_cont); + curr_cont = tmp1; + } +} + void init_base_structs(t_ft_ls *s_ls, t_flags *flgs, t_col_len *padd, t_list **fil_dir) { @@ -23,6 +41,7 @@ void init_base_structs(t_ft_ls *s_ls, t_flags *flgs, t_col_len *padd, s_ls->max_cols_padd = padd; s_ls->first_print = 1; s_ls->files_print = 0; + s_ls->dir_content_total = 0; } void ft_ls_start(t_ft_ls *s_ls, t_list **file_and_dirs, int no_error_operands) @@ -46,7 +65,7 @@ void ft_ls_start(t_ft_ls *s_ls, t_list **file_and_dirs, int no_error_operands) if (no_error_operands && (!file_and_dirs[DIRS] && !file_and_dirs[FILS])) { s_ls->one_dir = 1; - fill_path_lst(&file_and_dirs[DIRS], "."); + fill_path_lst(&file_and_dirs[DIRS], "./"); ft_ls_dir(s_ls, operands_parse(file_and_dirs[DIRS], s_ls), NULL); } } @@ -63,8 +82,10 @@ int main(int argc, char **argv) init_base_structs(&s_ls, &flgs, &padding, file_and_dirs); no_error_operands = parse_input(argc, argv, file_and_dirs, s_ls.flgs); ft_ls_start(&s_ls, file_and_dirs, no_error_operands); - free(file_and_dirs[FILS]); - free(file_and_dirs[DIRS]); + free_str_lst(file_and_dirs[FILS]); + free_str_lst(file_and_dirs[DIRS]); free(file_and_dirs); +// while (1) +// ; return 0; } diff --git a/src/output.c b/src/output.c index 63dde15..9cc6349 100644 --- a/src/output.c +++ b/src/output.c @@ -73,7 +73,7 @@ void long_format(t_list *lst_d_elm, t_ft_ls *s_ls) void one_line(t_list *lst_d_elm, t_ft_ls *s_ls) { ft_putstr(((t_dir_elm *)lst_d_elm->content)->elm_name); - s_ls = s_ls; + s_ls = 0; if (lst_d_elm->next) ft_putstr(" "); else @@ -82,7 +82,7 @@ void one_line(t_list *lst_d_elm, t_ft_ls *s_ls) void one_column(t_list *lst_d_elm, t_ft_ls *s_ls) { - s_ls = s_ls; + s_ls = 0; ft_putstr(((t_dir_elm *)lst_d_elm->content)->elm_name); ft_putstr("\n"); } diff --git a/src/sort.c b/src/sort.c index fdc3218..2c4d1b0 100644 --- a/src/sort.c +++ b/src/sort.c @@ -36,10 +36,10 @@ int by_m_time(t_dir_elm *a, t_dir_elm *b) { long dif; - dif = a->stat_copy->st_mtim.tv_sec - b->stat_copy->st_mtim.tv_sec; + dif = a->stat_copy->st_mtimespec.tv_sec - b->stat_copy->st_mtimespec.tv_sec; if (!dif) - dif = a->stat_copy->st_mtim.tv_nsec - - b->stat_copy->st_mtim.tv_nsec; + dif = a->stat_copy->st_mtimespec.tv_nsec - + b->stat_copy->st_mtimespec.tv_nsec; if (dif < 0) return (0); else if (dif > 0)