almost done
This commit is contained in:
parent
f5860b88e1
commit
2b3c0fd164
19 changed files with 753 additions and 562 deletions
|
@ -14,7 +14,13 @@ set(SOURCE_FILES
|
|||
src/flags.c
|
||||
src/sort.c
|
||||
src/output.c
|
||||
src/error.c) # sources
|
||||
src/error.c
|
||||
src/help_funcs.c
|
||||
src/stat_routine.c
|
||||
src/ft_ls_main_routine.c
|
||||
src/stat_parse.c
|
||||
src/long_output.c
|
||||
src/sort_funcs.c) # sources
|
||||
|
||||
add_executable(ft_ls ${SOURCE_FILES}) # compilation
|
||||
|
||||
|
|
2
Makefile
2
Makefile
|
@ -6,7 +6,7 @@
|
|||
# By: gtertysh <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2016/12/14 16:54:48 by gtertysh #+# #+# #
|
||||
# Updated: 2017/03/27 15:23:35 by gtertysh ### ########.fr #
|
||||
# Updated: 2017/04/01 22:10:55 by gtertysh ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
41
inc/ft_ls.h
41
inc/ft_ls.h
|
@ -6,7 +6,7 @@
|
|||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/25 18:27:54 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/03/25 21:18:30 by gtertysh ### ########.fr */
|
||||
/* Updated: 2017/04/02 06:52:30 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -33,9 +33,11 @@ typedef struct s_flags
|
|||
{
|
||||
int l;
|
||||
int a;
|
||||
int R;
|
||||
int recur;
|
||||
int size;
|
||||
int r;
|
||||
int t;
|
||||
int u;
|
||||
int col;
|
||||
int abn;
|
||||
} t_flags;
|
||||
|
@ -44,11 +46,10 @@ typedef struct s_col_len
|
|||
{
|
||||
int link_col;
|
||||
int name_col;
|
||||
int size_col;
|
||||
int size_mn_col;
|
||||
int group_col;
|
||||
} t_col_len;
|
||||
|
||||
|
||||
typedef struct s_dir_elm
|
||||
{
|
||||
char *elm_name;
|
||||
|
@ -73,27 +74,43 @@ typedef struct s_ft_ls
|
|||
} t_ft_ls;
|
||||
|
||||
void ft_ls_dir(t_ft_ls *s_ls, t_list *root, char *parent_dir);
|
||||
void ft_ls_fil(t_ft_ls *s_ls, t_list *root);
|
||||
|
||||
void ft_ls_fil(t_ft_ls *s_ls, t_list **files, char *path);
|
||||
void extract_content(char *full_path, t_ft_ls *s_ls,
|
||||
t_list **dir_content);
|
||||
void find_dirs(char *full_path, t_ft_ls *s_ls,
|
||||
t_list *cur_dir_con, t_list **dirs);
|
||||
t_list *operands_parse(t_list *ops, t_ft_ls *s_ls);
|
||||
|
||||
void print_content(t_ft_ls *s_ls, t_list *curr_dir_content,
|
||||
char *cur_path);
|
||||
|
||||
int parse_input(int ac, char **av, t_list **file_and_dirs,
|
||||
t_flags *flgs);
|
||||
|
||||
t_list *init_content_node(char *full_path, char *filename,
|
||||
t_ft_ls *s_ls);
|
||||
|
||||
void fill_path_lst(t_list **path_lst, char *path);
|
||||
void get_padding_and_blocks(t_ft_ls *s_ls, t_dir_elm *d_elem);
|
||||
char *get_time(t_dir_elm *d_elm);
|
||||
char *get_attr(t_dir_elm *d_elm);
|
||||
|
||||
void sort_content(t_list **del, t_flags *flgs);
|
||||
void sort_paths(t_list **paths);
|
||||
int by_name_lex(t_dir_elm *a, t_dir_elm *b);
|
||||
int by_name(t_dir_elm *a, t_dir_elm *b);
|
||||
int by_size(t_dir_elm *a, t_dir_elm *b);
|
||||
int by_m_time(t_dir_elm *a, t_dir_elm *b);
|
||||
int by_a_time(t_dir_elm *a, t_dir_elm *b);
|
||||
|
||||
void output(t_ft_ls *s_ls, t_list *curr_dir_content);
|
||||
void print_full_path(t_ft_ls *s_ls, char *path);
|
||||
void long_format(t_list *lst_d_elm, t_ft_ls *s_ls);
|
||||
void put_total(int total);
|
||||
void print_content(t_ft_ls *s_ls, t_list *curr_dir_content,
|
||||
char *cur_path);
|
||||
|
||||
void print_full_path(t_ft_ls *s_ls, char *path);
|
||||
void fill_path_lst(t_list **path_lst, char *path);
|
||||
int chck_flgs(char *flg, t_flags *flgs);
|
||||
void put_error(char *arg);
|
||||
void free_struct_lst(t_list **curr_cont);
|
||||
char *get_full_path(char *path, char *dir_ent_name);
|
||||
void file_node_init(t_dir_elm *de);
|
||||
int is_hidden(char *path);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lst_merge_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:44:41 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:44:43 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static t_list *merge(t_list *a, t_list *b, int (*cmp)())
|
||||
|
@ -5,12 +17,10 @@ static t_list *merge(t_list *a, t_list *b, int (*cmp)())
|
|||
t_list *result;
|
||||
|
||||
result = NULL;
|
||||
|
||||
if (a == NULL)
|
||||
return(b);
|
||||
return (b);
|
||||
else if (b == NULL)
|
||||
return(a);
|
||||
|
||||
return (a);
|
||||
if (cmp(a->content, b->content))
|
||||
{
|
||||
result = a;
|
||||
|
@ -21,7 +31,7 @@ static t_list *merge(t_list *a, t_list *b, int (*cmp)())
|
|||
result = b;
|
||||
result->next = merge(a, b->next, cmp);
|
||||
}
|
||||
return(result);
|
||||
return (result);
|
||||
}
|
||||
|
||||
static void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
|
||||
|
@ -29,7 +39,7 @@ static void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
|
|||
t_list *fast;
|
||||
t_list *slow;
|
||||
|
||||
if (source==NULL || source->next==NULL)
|
||||
if (source == NULL || source->next == NULL)
|
||||
{
|
||||
*front_ptr = source;
|
||||
*back_ptr = NULL;
|
||||
|
@ -52,6 +62,7 @@ static void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
|
|||
slow->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)())
|
||||
{
|
||||
t_list *head;
|
||||
|
@ -60,7 +71,7 @@ void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)())
|
|||
|
||||
head = *head_ptr;
|
||||
if ((head == NULL) || (head->next == NULL))
|
||||
return;
|
||||
return ;
|
||||
split(head, &a, &b);
|
||||
ft_lst_merge_sort(&a, cmp);
|
||||
ft_lst_merge_sort(&b, cmp);
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_num_len.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:46:38 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:46:41 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_num_len(long num)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/01 15:58:52 by gtertysh #+# #+# */
|
||||
/* Updated: 2016/12/01 15:59:14 by gtertysh ### ########.fr */
|
||||
/* Updated: 2017/04/02 06:47:00 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
35
src/error.c
35
src/error.c
|
@ -1,25 +1,22 @@
|
|||
#include "ft_ls.h"
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:29:46 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:30:08 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
//void cannot_acces(char *arg)
|
||||
//{
|
||||
// char *ls_ca_quo;
|
||||
// char *ls_ca;
|
||||
//
|
||||
// ls_ca = ft_strjoin("ls: cannot access '", arg);
|
||||
// ls_ca_quo = ft_strjoin(ls_ca, "'");
|
||||
// perror(ls_ca_quo);
|
||||
// free(ls_ca_quo);
|
||||
// free(ls_ca);
|
||||
//}
|
||||
#include "ft_ls.h"
|
||||
|
||||
void put_error(char *arg)
|
||||
{
|
||||
char *ls_ca_quo;
|
||||
char *ls_ca;
|
||||
char *ls_arg;
|
||||
|
||||
ls_ca = ft_strjoin("ls: cannot access '", arg);
|
||||
ls_ca_quo = ft_strjoin(ls_ca, "'");
|
||||
perror(ls_ca_quo);
|
||||
free(ls_ca_quo);
|
||||
free(ls_ca);
|
||||
ls_arg = ft_strjoin("ls: ", arg);
|
||||
perror(ls_arg);
|
||||
free(ls_arg);
|
||||
}
|
41
src/flags.c
41
src/flags.c
|
@ -1,23 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* flags.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:24:44 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:28:32 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
int chck_long_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
if (*(flg + 1) && (flg += 1))
|
||||
{
|
||||
if (flgs->abn == 0) flgs->abn = (ft_strcmp(flg, "lex") == 0)
|
||||
? 1 : 0;
|
||||
if (flgs->abn == 0)
|
||||
flgs->abn = (ft_strcmp(flg, "lex") == 0) ? 1 : 0;
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void chck_short_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
if (flgs->l == 0) flgs->l = (ft_strchr(flg, 'l') != NULL) ? 1 : 0;
|
||||
if (flgs->a == 0) flgs->a = (ft_strchr(flg, 'a') != NULL) ? 1 : 0;
|
||||
if (flgs->R == 0) flgs->R = (ft_strchr(flg, 'R') != NULL) ? 1 : 0;
|
||||
if (flgs->t == 0) flgs->t = (ft_strchr(flg, 't') != NULL) ? 1 : 0;
|
||||
if (flgs->r == 0) flgs->r = (ft_strchr(flg, 'r') != NULL) ? 1 : 0;
|
||||
if (flgs->col == 0) flgs->col = (ft_strchr(flg, '1') != NULL) ? 1 : 0;
|
||||
if (flgs->size == 0)
|
||||
flgs->size = (ft_strchr(flg, 'S') != NULL) ? 1 : 0;
|
||||
if (flgs->l == 0)
|
||||
flgs->l = (ft_strchr(flg, 'l') != NULL) ? 1 : 0;
|
||||
if (flgs->a == 0)
|
||||
flgs->a = (ft_strchr(flg, 'a') != NULL) ? 1 : 0;
|
||||
if (flgs->recur == 0)
|
||||
flgs->recur = (ft_strchr(flg, 'R') != NULL) ? 1 : 0;
|
||||
if (flgs->t == 0)
|
||||
flgs->t = (ft_strchr(flg, 't') != NULL) ? 1 : 0;
|
||||
if (flgs->r == 0)
|
||||
flgs->r = (ft_strchr(flg, 'r') != NULL) ? 1 : 0;
|
||||
if (flgs->u == 0)
|
||||
flgs->u = (ft_strchr(flg, 'u') != NULL) ? 1 : 0;
|
||||
if (flgs->col == 0)
|
||||
flgs->col = (ft_strchr(flg, '1') != NULL) ? 1 : 0;
|
||||
|
||||
}
|
||||
|
||||
int chck_flgs(char *flg, t_flags *flgs)
|
||||
|
@ -36,4 +60,3 @@ int chck_flgs(char *flg, t_flags *flgs)
|
|||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
|
345
src/ft_ls.c
345
src/ft_ls.c
|
@ -1,356 +1,61 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ls.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 03:12:44 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 03:13:40 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
int is_hidden(char *path)
|
||||
{
|
||||
char *begin;
|
||||
char *end;
|
||||
|
||||
begin = path;
|
||||
end = path + ft_strlen(path) - 1;
|
||||
if (begin != end)
|
||||
{
|
||||
while (end != begin && *end != '/')
|
||||
end--;
|
||||
if (end == begin)
|
||||
{
|
||||
if (*end == '.')
|
||||
return (1);
|
||||
}
|
||||
else if (*(end + 1) == '.')
|
||||
return (1);
|
||||
}
|
||||
else if (*end == '.')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
char *get_full_path(char *path, char *dir_ent_name)
|
||||
{
|
||||
char *tmp;
|
||||
char *tmp1;
|
||||
|
||||
if (dir_ent_name && path)
|
||||
{
|
||||
if (path[ft_strlen(path) - 1] == '/')
|
||||
{
|
||||
if (*dir_ent_name == '/')
|
||||
tmp = ft_strjoin(path, dir_ent_name + 1);
|
||||
else
|
||||
tmp = ft_strjoin(path, dir_ent_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp1 = ft_strjoin(path, "/");
|
||||
tmp = ft_strjoin(tmp1, dir_ent_name);
|
||||
free(tmp1);
|
||||
}
|
||||
return (tmp);
|
||||
}
|
||||
else
|
||||
return (ft_strdup(dir_ent_name));
|
||||
}
|
||||
|
||||
|
||||
char get_file_type(t_dir_elm *d_elm)
|
||||
{
|
||||
char ch;
|
||||
|
||||
ch = '-';
|
||||
if (S_ISDIR(d_elm->stat_copy->st_mode))
|
||||
ch = 'd';
|
||||
if (S_ISLNK(d_elm->stat_copy->st_mode))
|
||||
ch = 'l';
|
||||
if (S_ISFIFO(d_elm->stat_copy->st_mode))
|
||||
ch = 'p';
|
||||
if (S_ISSOCK(d_elm->stat_copy->st_mode))
|
||||
ch = 's';
|
||||
if (S_ISBLK(d_elm->stat_copy->st_mode))
|
||||
ch = 'b';
|
||||
if (S_ISCHR(d_elm->stat_copy->st_mode))
|
||||
ch = 'c';
|
||||
return (ch);
|
||||
}
|
||||
|
||||
void get_file_permissions(char *attr_str, t_dir_elm *d_elm)
|
||||
{
|
||||
struct stat *st;
|
||||
char r;
|
||||
char w;
|
||||
char x;
|
||||
char f;
|
||||
|
||||
r = 'r';
|
||||
w = 'w';
|
||||
x = 'x';
|
||||
f = '-';
|
||||
st = d_elm->stat_copy;
|
||||
attr_str[0] = (st->st_mode & S_IRUSR) ? r : f;
|
||||
attr_str[1] = (st->st_mode & S_IWUSR) ? w : f;
|
||||
attr_str[2] = (st->st_mode & S_IXUSR) ? x : f;
|
||||
attr_str[3] = (st->st_mode & S_IRGRP) ? r : f;
|
||||
attr_str[4] = (st->st_mode & S_IWGRP) ? w : f;
|
||||
attr_str[5] = (st->st_mode & S_IXGRP) ? x : f;
|
||||
attr_str[6] = (st->st_mode & S_IROTH) ? r : f;
|
||||
attr_str[7] = (st->st_mode & S_IWOTH) ? w : f;
|
||||
attr_str[8] = (st->st_mode & S_IXOTH) ? x : f;
|
||||
}
|
||||
|
||||
char *get_attr(t_dir_elm *d_elm)
|
||||
{
|
||||
char *attr_str;
|
||||
|
||||
attr_str = ft_memalloc(sizeof(11));
|
||||
|
||||
attr_str[10] = '\0';
|
||||
attr_str[0] = get_file_type(d_elm);
|
||||
get_file_permissions(attr_str + 1, d_elm);
|
||||
return (attr_str);
|
||||
}
|
||||
|
||||
char *get_time(t_dir_elm *d_elm)
|
||||
{
|
||||
char *time;
|
||||
char *time_tmp;
|
||||
|
||||
time_tmp = ctime(&(d_elm->stat_copy->st_mtimespec.tv_sec));
|
||||
time_tmp[16] = 0;
|
||||
time = ft_strdup(time_tmp + 4);
|
||||
return (time);
|
||||
}
|
||||
|
||||
void set_columns_padd(t_dir_elm *de)
|
||||
{
|
||||
de->cols_padd.link_col = ft_num_len(de->stat_copy->st_nlink);
|
||||
de->cols_padd.name_col = (int)ft_strlen(de->u_name);
|
||||
de->cols_padd.group_col = (int)ft_strlen(de->g_name);
|
||||
de->cols_padd.size_col = ft_num_len(de->stat_copy->st_size);
|
||||
}
|
||||
|
||||
|
||||
void get_info_from_stat(t_dir_elm *dir_elm)
|
||||
{
|
||||
dir_elm->attr_str = get_attr(dir_elm);
|
||||
dir_elm->u_name = ft_strdup(getpwuid(dir_elm->stat_copy->st_uid)->pw_name);
|
||||
dir_elm->g_name = ft_strdup(getgrgid(dir_elm->stat_copy->st_gid)->gr_name);
|
||||
dir_elm->m_time = get_time(dir_elm);
|
||||
}
|
||||
|
||||
void to_null(t_dir_elm *de)
|
||||
{
|
||||
de->stat_copy = NULL;
|
||||
de->link_name = NULL;
|
||||
de->attr_str = NULL;
|
||||
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;
|
||||
t_list *node;
|
||||
|
||||
to_null(&de);
|
||||
de.elm_name = ft_strdup(filename);
|
||||
tmp_file_path = get_full_path(full_path, filename);
|
||||
de.stat_copy = malloc(sizeof(struct stat));
|
||||
lstat(tmp_file_path, de.stat_copy);
|
||||
free(tmp_file_path);
|
||||
if (s_ls->flgs->l)
|
||||
{
|
||||
if (S_ISLNK(de.stat_copy->st_mode))
|
||||
{
|
||||
de.link_name = ft_strnew(PATH_MAX);
|
||||
if (readlink(tmp_file_path, de.link_name, PATH_MAX) == -1)
|
||||
put_error(0);
|
||||
}
|
||||
else
|
||||
de.link_name = NULL;
|
||||
get_info_from_stat(&de);
|
||||
set_columns_padd(&de);
|
||||
}
|
||||
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)
|
||||
return (read_stat(full_path, filename, s_ls));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_list *find_dirs(char *full_path, t_ft_ls *s_ls, t_list *curr_dir_content)
|
||||
{
|
||||
char *full_path_with_file;
|
||||
t_dir_elm *dir_elm;
|
||||
t_list *dirs;
|
||||
|
||||
dirs = NULL;
|
||||
while (curr_dir_content)
|
||||
{
|
||||
dir_elm = (t_dir_elm *)curr_dir_content->content;
|
||||
full_path_with_file = get_full_path(full_path, dir_elm->elm_name);
|
||||
if ((S_ISDIR(dir_elm->stat_copy->st_mode)) && (ft_strcmp(dir_elm->elm_name, ".") != 0) && (ft_strcmp(dir_elm->elm_name, "..") != 0))
|
||||
{
|
||||
if (s_ls->flgs->a)
|
||||
ft_lstadd(&dirs, ft_lstnew(dir_elm, sizeof(t_dir_elm)));
|
||||
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;
|
||||
free(full_path_with_file);
|
||||
}
|
||||
return (dirs);
|
||||
}
|
||||
|
||||
void get_padding_and_blocks(t_ft_ls *s_ls, t_dir_elm *d_elem)
|
||||
{
|
||||
if (d_elem && s_ls && s_ls->flgs->l)
|
||||
{
|
||||
if (d_elem->cols_padd.link_col > s_ls->max_cols_padd->link_col)
|
||||
s_ls->max_cols_padd->link_col = d_elem->cols_padd.link_col;
|
||||
if (d_elem->cols_padd.name_col > s_ls->max_cols_padd->name_col)
|
||||
s_ls->max_cols_padd->name_col = d_elem->cols_padd.name_col;
|
||||
if (d_elem->cols_padd.group_col > s_ls->max_cols_padd->group_col)
|
||||
s_ls->max_cols_padd->group_col = d_elem->cols_padd.group_col;
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void extract_content(char *full_path, t_ft_ls *s_ls, t_list **dir_content)
|
||||
{
|
||||
DIR *dp;
|
||||
struct dirent *de;
|
||||
t_list *lst_elm;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
closedir(dp);
|
||||
}
|
||||
}
|
||||
|
||||
void reset_s_ls(t_ft_ls *s_ls)
|
||||
{
|
||||
s_ls->max_cols_padd->group_col = 0;
|
||||
s_ls->max_cols_padd->name_col = 0;
|
||||
s_ls->max_cols_padd->link_col = 0;
|
||||
s_ls->max_cols_padd->size_col = 0;
|
||||
s_ls->max_cols_padd->size_mn_col = 0;
|
||||
s_ls->dir_content_total = 0;
|
||||
}
|
||||
|
||||
void print_content(t_ft_ls *s_ls, t_list *curr_dir_content, char *cur_path)
|
||||
{
|
||||
if ((!s_ls->one_dir && !s_ls->no_ops) || s_ls->flgs->R)
|
||||
if ((!s_ls->one_dir && !s_ls->no_ops) || s_ls->flgs->recur)
|
||||
print_full_path(s_ls, cur_path);
|
||||
output(s_ls, curr_dir_content);
|
||||
}
|
||||
|
||||
t_list *operands_parse(t_list *ops, t_ft_ls *s_ls)
|
||||
void ft_ls_fil(t_ft_ls *s_ls, t_list **files, char *path)
|
||||
{
|
||||
t_list *tmp_rnr;
|
||||
t_list *root;
|
||||
t_list *lst_elm;
|
||||
|
||||
root = NULL;
|
||||
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;
|
||||
}
|
||||
return (root);
|
||||
}
|
||||
|
||||
|
||||
void free_struct_lst(t_list *curr_cont)
|
||||
{
|
||||
t_dir_elm *tmp;
|
||||
t_list *tmp1;
|
||||
|
||||
while (curr_cont)
|
||||
{
|
||||
tmp1 = curr_cont->next;
|
||||
if (curr_cont->content)
|
||||
{
|
||||
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->stat_copy);
|
||||
free(tmp);
|
||||
}
|
||||
free(curr_cont);
|
||||
curr_cont = tmp1;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_ls_fil(t_ft_ls *s_ls, t_list *root)
|
||||
{
|
||||
sort_content(&root, s_ls->flgs);
|
||||
print_content(s_ls, root, NULL);
|
||||
free_struct_lst(root);
|
||||
sort_content(files, s_ls->flgs);
|
||||
print_content(s_ls, *files, path);
|
||||
}
|
||||
|
||||
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;
|
||||
t_list *dirs;
|
||||
|
||||
fuckin_head = root;
|
||||
curr_dir_content = NULL;
|
||||
sort_content(&root, s_ls->flgs);
|
||||
reset_s_ls(s_ls);
|
||||
while (root)
|
||||
{
|
||||
cur_path = get_full_path(parent_dir, ((t_dir_elm *)root->content)->elm_name);
|
||||
cur_path = get_full_path(parent_dir,
|
||||
((t_dir_elm *)root->content)->elm_name);
|
||||
extract_content(cur_path, s_ls, &curr_dir_content);
|
||||
if (curr_dir_content)
|
||||
ft_ls_fil(s_ls, &curr_dir_content, cur_path);
|
||||
if (s_ls->flgs->recur)
|
||||
{
|
||||
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);
|
||||
}
|
||||
find_dirs(cur_path, s_ls, curr_dir_content, &dirs);
|
||||
ft_ls_dir(s_ls, dirs, cur_path);
|
||||
free_struct_lst(&curr_dir_content);
|
||||
}
|
||||
free(cur_path);
|
||||
root = root->next;
|
||||
}
|
||||
free_struct_lst(fuckin_head);
|
||||
}
|
61
src/ft_ls_main_routine.c
Normal file
61
src/ft_ls_main_routine.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ls_main_routine.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:28:59 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:29:28 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void find_dirs(char *full_path, t_ft_ls *s_ls, t_list *cur_dir_con,
|
||||
t_list **dirs)
|
||||
{
|
||||
char *full_path_with_file;
|
||||
t_dir_elm *dir_elm;
|
||||
|
||||
*dirs = NULL;
|
||||
while (cur_dir_con)
|
||||
{
|
||||
dir_elm = (t_dir_elm *)cur_dir_con->content;
|
||||
full_path_with_file = get_full_path(full_path, dir_elm->elm_name);
|
||||
if ((S_ISDIR(dir_elm->stat_copy->st_mode)) &&
|
||||
(ft_strcmp(dir_elm->elm_name, ".") != 0) &&
|
||||
(ft_strcmp(dir_elm->elm_name, "..") != 0))
|
||||
{
|
||||
if (access(full_path_with_file, F_OK) != 0)
|
||||
put_error(full_path_with_file);
|
||||
else if (s_ls->flgs->a)
|
||||
ft_lstadd(dirs, ft_lstnew(dir_elm, sizeof(t_dir_elm)));
|
||||
else if (*dir_elm->elm_name != '.')
|
||||
ft_lstadd(dirs, ft_lstnew(dir_elm, sizeof(t_dir_elm)));
|
||||
}
|
||||
cur_dir_con = cur_dir_con->next;
|
||||
free(full_path_with_file);
|
||||
}
|
||||
}
|
||||
|
||||
void extract_content(char *full_path, t_ft_ls *s_ls, t_list **dir_content)
|
||||
{
|
||||
DIR *dp;
|
||||
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);
|
||||
}
|
||||
}
|
||||
closedir(dp);
|
||||
}
|
||||
}
|
99
src/help_funcs.c
Normal file
99
src/help_funcs.c
Normal file
|
@ -0,0 +1,99 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* help_funcs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:31:57 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:31:59 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void free_struct_lst(t_list **curr_cont)
|
||||
{
|
||||
t_dir_elm *tmp;
|
||||
t_list *tmp1;
|
||||
|
||||
while (*curr_cont)
|
||||
{
|
||||
tmp1 = (*curr_cont)->next;
|
||||
if ((*curr_cont)->content)
|
||||
{
|
||||
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->stat_copy);
|
||||
free(tmp);
|
||||
}
|
||||
free(*curr_cont);
|
||||
*curr_cont = tmp1;
|
||||
}
|
||||
}
|
||||
|
||||
int is_hidden(char *path)
|
||||
{
|
||||
char *begin;
|
||||
char *end;
|
||||
|
||||
begin = path;
|
||||
end = path + ft_strlen(path) - 1;
|
||||
if (begin != end)
|
||||
{
|
||||
while (end != begin && *end != '/')
|
||||
end--;
|
||||
if (end == begin)
|
||||
{
|
||||
if (*end == '.')
|
||||
return (1);
|
||||
}
|
||||
else if (*(end + 1) == '.')
|
||||
return (1);
|
||||
}
|
||||
else if (*end == '.')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *get_full_path(char *path, char *dir_ent_name)
|
||||
{
|
||||
char *tmp;
|
||||
char *tmp1;
|
||||
|
||||
if (dir_ent_name && path)
|
||||
{
|
||||
if (path[ft_strlen(path) - 1] == '/')
|
||||
{
|
||||
if (*dir_ent_name == '/')
|
||||
tmp = ft_strjoin(path, dir_ent_name + 1);
|
||||
else
|
||||
tmp = ft_strjoin(path, dir_ent_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp1 = ft_strjoin(path, "/");
|
||||
tmp = ft_strjoin(tmp1, dir_ent_name);
|
||||
free(tmp1);
|
||||
}
|
||||
return (tmp);
|
||||
}
|
||||
else
|
||||
return (ft_strdup(dir_ent_name));
|
||||
}
|
||||
|
||||
void file_node_init(t_dir_elm *de)
|
||||
{
|
||||
de->stat_copy = NULL;
|
||||
de->link_name = NULL;
|
||||
de->attr_str = NULL;
|
||||
de->g_name = NULL;
|
||||
de->u_name = NULL;
|
||||
de->m_time = NULL;
|
||||
de->elm_name = NULL;
|
||||
}
|
72
src/long_output.c
Normal file
72
src/long_output.c
Normal file
|
@ -0,0 +1,72 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* long_output.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:30:33 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:30:55 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void put_sp(int num)
|
||||
{
|
||||
while (num--)
|
||||
ft_putstr(" ");
|
||||
}
|
||||
|
||||
void put_total(int total)
|
||||
{
|
||||
ft_putstr("total");
|
||||
ft_putstr(" ");
|
||||
ft_putnbr(total);
|
||||
ft_putstr("\n");
|
||||
}
|
||||
|
||||
void put_link(t_dir_elm *d_elm)
|
||||
{
|
||||
char *tmp_link;
|
||||
|
||||
tmp_link = ft_strjoin(" -> ", d_elm->link_name);
|
||||
ft_putstr(tmp_link);
|
||||
free(tmp_link);
|
||||
}
|
||||
|
||||
void put_minor_major(t_dir_elm *d_elm)
|
||||
{
|
||||
ft_putnbr(major(d_elm->stat_copy->st_rdev));
|
||||
ft_putstr(",");
|
||||
ft_putstr(" ");
|
||||
ft_putnbr(minor(d_elm->stat_copy->st_rdev));
|
||||
}
|
||||
|
||||
void long_format(t_list *lst_d_elm, t_ft_ls *s_ls)
|
||||
{
|
||||
t_dir_elm *de;
|
||||
|
||||
de = (t_dir_elm *)lst_d_elm->content;
|
||||
ft_putstr(de->attr_str);
|
||||
put_sp(s_ls->max_cols_padd->link_col - de->cols_padd.link_col + 1);
|
||||
ft_putnbr((int)de->stat_copy->st_nlink);
|
||||
ft_putstr(" ");
|
||||
ft_putstr(de->u_name);
|
||||
put_sp(s_ls->max_cols_padd->name_col - de->cols_padd.name_col + 2);
|
||||
ft_putstr(de->g_name);
|
||||
put_sp(s_ls->max_cols_padd->group_col - de->cols_padd.group_col + 1);
|
||||
put_sp(s_ls->max_cols_padd->size_mn_col - de->cols_padd.size_mn_col + 2);
|
||||
if (S_ISBLK(de->stat_copy->st_mode) ||
|
||||
S_ISCHR(de->stat_copy->st_mode))
|
||||
put_minor_major(de);
|
||||
else
|
||||
ft_putnbr((int)de->stat_copy->st_size);
|
||||
ft_putstr(" ");
|
||||
ft_putstr(de->m_time);
|
||||
ft_putstr(" ");
|
||||
ft_putstr(de->elm_name);
|
||||
if (S_ISLNK(de->stat_copy->st_mode))
|
||||
put_link(de);
|
||||
ft_putstr("\n");
|
||||
}
|
22
src/main.c
22
src/main.c
|
@ -6,19 +6,17 @@
|
|||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/25 18:26:51 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/03/25 18:27:42 by gtertysh ### ########.fr */
|
||||
/* Updated: 2017/04/02 06:33:40 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
// complide with -O3 flag!!
|
||||
|
||||
|
||||
void free_str_lst(t_list *curr_cont)
|
||||
{
|
||||
char *tmp;
|
||||
t_list *tmp1;
|
||||
|
||||
while (curr_cont)
|
||||
{
|
||||
tmp1 = curr_cont->next;
|
||||
|
@ -44,25 +42,27 @@ void init_base_structs(t_ft_ls *s_ls, t_flags *flgs, t_col_len *padd,
|
|||
s_ls->dir_content_total = 0;
|
||||
}
|
||||
|
||||
void ft_ls_start(t_ft_ls *s_ls, t_list **file_and_dirs, int no_error_operands)
|
||||
void ft_ls_start(t_ft_ls *s_ls, t_list **file_and_dirs, int no_error_ops)
|
||||
{
|
||||
t_list *file_lst;
|
||||
|
||||
if (file_and_dirs[FILS])
|
||||
{
|
||||
file_lst = operands_parse(file_and_dirs[FILS], s_ls);
|
||||
s_ls->files_print = 1;
|
||||
ft_ls_fil(s_ls, operands_parse(file_and_dirs[FILS], s_ls));
|
||||
ft_ls_fil(s_ls, &file_lst, NULL);
|
||||
s_ls->files_print = 0;
|
||||
s_ls->first_print = 0;
|
||||
}
|
||||
if (file_and_dirs[DIRS])
|
||||
{
|
||||
sort_paths(&file_and_dirs[DIRS]);
|
||||
if (ft_lst_len(file_and_dirs[FILS]) == 0 &&
|
||||
ft_lst_len(file_and_dirs[DIRS]) == 1)
|
||||
if (!s_ls->flgs->R)
|
||||
if (!s_ls->flgs->recur)
|
||||
s_ls->one_dir = 1;
|
||||
ft_ls_dir(s_ls, operands_parse(file_and_dirs[DIRS], s_ls), NULL);
|
||||
}
|
||||
if (no_error_operands && (!file_and_dirs[DIRS] && !file_and_dirs[FILS]))
|
||||
if (no_error_ops && (!file_and_dirs[DIRS] && !file_and_dirs[FILS]))
|
||||
{
|
||||
s_ls->one_dir = 1;
|
||||
fill_path_lst(&file_and_dirs[DIRS], "./");
|
||||
|
@ -85,7 +85,5 @@ int main(int argc, char **argv)
|
|||
free_str_lst(file_and_dirs[FILS]);
|
||||
free_str_lst(file_and_dirs[DIRS]);
|
||||
free(file_and_dirs);
|
||||
// while (1)
|
||||
// ;
|
||||
return 0;
|
||||
return (0);
|
||||
}
|
||||
|
|
72
src/output.c
72
src/output.c
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* output.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 03:10:41 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 03:12:09 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void print_full_path(t_ft_ls *s_ls, char *path)
|
||||
|
@ -24,56 +36,10 @@ void print_full_path(t_ft_ls *s_ls, char *path)
|
|||
}
|
||||
}
|
||||
|
||||
void put_spaces(int num)
|
||||
{
|
||||
while (num--)
|
||||
ft_putstr(" ");
|
||||
}
|
||||
|
||||
void put_total(int total)
|
||||
{
|
||||
ft_putstr("total");
|
||||
ft_putstr(" ");
|
||||
ft_putnbr(total);
|
||||
ft_putstr("\n");
|
||||
}
|
||||
|
||||
void put_link(t_dir_elm *d_elm)
|
||||
{
|
||||
char *tmp_link;
|
||||
tmp_link = ft_strjoin(" -> ", d_elm->link_name);
|
||||
ft_putstr(tmp_link);
|
||||
free(tmp_link);
|
||||
}
|
||||
|
||||
void long_format(t_list *lst_d_elm, t_ft_ls *s_ls)
|
||||
{
|
||||
t_dir_elm *d_elem;
|
||||
|
||||
d_elem = (t_dir_elm *)lst_d_elm->content;
|
||||
ft_putstr(d_elem->attr_str);
|
||||
put_spaces(s_ls->max_cols_padd->link_col - d_elem->cols_padd.link_col + 1);
|
||||
ft_putnbr((int)d_elem->stat_copy->st_nlink);
|
||||
ft_putstr(" ");
|
||||
ft_putstr(d_elem->u_name);
|
||||
put_spaces(s_ls->max_cols_padd->name_col - d_elem->cols_padd.name_col + 1);
|
||||
ft_putstr(d_elem->g_name);
|
||||
put_spaces(s_ls->max_cols_padd->group_col - d_elem->cols_padd.group_col);
|
||||
put_spaces(s_ls->max_cols_padd->size_col - d_elem->cols_padd.size_col + 1);
|
||||
ft_putnbr((int)d_elem->stat_copy->st_size);
|
||||
ft_putstr(" ");
|
||||
ft_putstr(d_elem->m_time);
|
||||
ft_putstr(" ");
|
||||
ft_putstr(d_elem->elm_name);
|
||||
if (S_ISLNK(d_elem->stat_copy->st_mode))
|
||||
put_link(d_elem);
|
||||
ft_putstr("\n");
|
||||
}
|
||||
|
||||
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 = 0;
|
||||
ft_putstr(((t_dir_elm *)lst_d_elm->content)->elm_name);
|
||||
if (lst_d_elm->next)
|
||||
ft_putstr(" ");
|
||||
else
|
||||
|
@ -96,7 +62,7 @@ void out(t_ft_ls *s_ls, t_list *cdc, void (*format_fun)(t_list *, t_ft_ls *))
|
|||
{
|
||||
if (s_ls->flgs->a)
|
||||
format_fun(lst_rnr, s_ls);
|
||||
else if (*((t_dir_elm *)lst_rnr->content)->elm_name != '.')
|
||||
else if (!is_hidden(((t_dir_elm *)lst_rnr->content)->elm_name))
|
||||
format_fun(lst_rnr, s_ls);
|
||||
lst_rnr = lst_rnr->next;
|
||||
}
|
||||
|
@ -113,11 +79,9 @@ void output(t_ft_ls *s_ls, t_list *curr_dir_content)
|
|||
else if (s_ls->flgs->col)
|
||||
out(s_ls, curr_dir_content, one_column);
|
||||
else
|
||||
{
|
||||
out(s_ls, curr_dir_content, one_line);
|
||||
// else if (s_ls.flgs->col)
|
||||
// one_column(s_ls);
|
||||
// else
|
||||
// mult_column(s_ls);
|
||||
|
||||
// ft_lstiter_ret(s_ls.lst_dir_content, out);
|
||||
if (s_ls->flgs->r)
|
||||
ft_putstr("\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse_input.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 02:29:34 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 02:29:37 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
int chck_opn(char *path)
|
||||
|
@ -24,6 +36,26 @@ void fill_path_lst(t_list **path_lst, char *path)
|
|||
ft_lstadd(path_lst, ft_lstnew(path, ft_strlen(path) + 1));
|
||||
}
|
||||
|
||||
t_list *operands_parse(t_list *ops, t_ft_ls *s_ls)
|
||||
{
|
||||
t_list *tmp_rnr;
|
||||
t_list *root;
|
||||
t_list *lst_elm;
|
||||
|
||||
root = NULL;
|
||||
tmp_rnr = ops;
|
||||
while (tmp_rnr)
|
||||
{
|
||||
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;
|
||||
}
|
||||
return (root);
|
||||
}
|
||||
|
||||
int parse_input(int ac, char **av, t_list **file_and_dirs, t_flags *flgs)
|
||||
{
|
||||
int i;
|
||||
|
|
100
src/sort.c
100
src/sort.c
|
@ -1,95 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 03:13:59 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 03:14:03 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
int by_name_and_same_type(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if ((S_ISDIR(a->stat_copy->st_mode) &&
|
||||
S_ISDIR(b->stat_copy->st_mode)) ||
|
||||
(S_ISREG(a->stat_copy->st_mode) &&
|
||||
S_ISREG(b->stat_copy->st_mode)))
|
||||
if (ft_strcmp(a->elm_name, b->elm_name) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_name_lex(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (ft_strcmp_lex(a->elm_name, b->elm_name) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_name(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (ft_strcmp(a->elm_name, b->elm_name) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_size(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (a->stat_copy->st_size <= b->stat_copy->st_size)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_m_time(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
long dif;
|
||||
|
||||
dif = a->stat_copy->st_mtimespec.tv_sec - b->stat_copy->st_mtimespec.tv_sec;
|
||||
if (!dif)
|
||||
dif = a->stat_copy->st_mtimespec.tv_nsec -
|
||||
b->stat_copy->st_mtimespec.tv_nsec;
|
||||
if (dif < 0)
|
||||
return (0);
|
||||
else if (dif > 0)
|
||||
return (1);
|
||||
else
|
||||
return (by_name(a, b));
|
||||
}
|
||||
|
||||
int by_type(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (S_ISREG(b->stat_copy->st_mode) && a)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int path_by_name(char *a, char *b)
|
||||
{
|
||||
if (ft_strcmp(a, b) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int path_by_name_lex(char *a, char *b)
|
||||
{
|
||||
if (ft_strcmp_lex(a, b) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void by_type_and_name(t_list **del)
|
||||
{
|
||||
ft_lst_merge_sort(del, by_type);
|
||||
ft_lst_merge_sort(del, by_name_and_same_type);
|
||||
|
||||
}
|
||||
|
||||
void sort_content(t_list **del, t_flags *flgs)
|
||||
{
|
||||
if (flgs->t)
|
||||
{
|
||||
if (flgs->u)
|
||||
ft_lst_merge_sort(del, by_a_time);
|
||||
ft_lst_merge_sort(del, by_m_time);
|
||||
}
|
||||
else if (flgs->abn)
|
||||
ft_lst_merge_sort(del, by_name_lex);
|
||||
else if (flgs->size)
|
||||
ft_lst_merge_sort(del, by_size);
|
||||
else
|
||||
ft_lst_merge_sort(del, by_name);
|
||||
if (flgs->r)
|
||||
ft_lst_rev(del);
|
||||
}
|
||||
|
||||
void sort_paths(t_list **paths)
|
||||
{
|
||||
|
||||
ft_lst_merge_sort(paths, path_by_name);
|
||||
}
|
66
src/sort_funcs.c
Normal file
66
src/sort_funcs.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_funcs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:34:52 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:37:10 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
int by_name_lex(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (ft_strcmp_lex(a->elm_name, b->elm_name) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_name(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (ft_strcmp(a->elm_name, b->elm_name) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_size(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (a->stat_copy->st_size > b->stat_copy->st_size)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_m_time(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
long dif;
|
||||
|
||||
dif = a->stat_copy->st_mtimespec.tv_sec - b->stat_copy->st_mtimespec.tv_sec;
|
||||
if (!dif)
|
||||
dif = a->stat_copy->st_mtimespec.tv_nsec -
|
||||
b->stat_copy->st_mtimespec.tv_nsec;
|
||||
if (dif < 0)
|
||||
return (0);
|
||||
else if (dif > 0)
|
||||
return (1);
|
||||
else
|
||||
return (by_name(a, b));
|
||||
}
|
||||
|
||||
int by_a_time(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
long dif;
|
||||
|
||||
dif = a->stat_copy->st_atimespec.tv_sec - b->stat_copy->st_atimespec.tv_sec;
|
||||
if (!dif)
|
||||
dif = a->stat_copy->st_atimespec.tv_nsec -
|
||||
b->stat_copy->st_atimespec.tv_nsec;
|
||||
if (dif < 0)
|
||||
return (0);
|
||||
else if (dif > 0)
|
||||
return (1);
|
||||
else
|
||||
return (by_name(a, b));
|
||||
}
|
101
src/stat_parse.c
Normal file
101
src/stat_parse.c
Normal file
|
@ -0,0 +1,101 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* stat_parse.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 03:08:48 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 03:08:50 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void get_padding_and_blocks(t_ft_ls *s_ls, t_dir_elm *d_elem)
|
||||
{
|
||||
if (d_elem && s_ls && s_ls->flgs->l)
|
||||
{
|
||||
if (d_elem->cols_padd.link_col > s_ls->max_cols_padd->link_col)
|
||||
s_ls->max_cols_padd->link_col = d_elem->cols_padd.link_col;
|
||||
if (d_elem->cols_padd.name_col > s_ls->max_cols_padd->name_col)
|
||||
s_ls->max_cols_padd->name_col = d_elem->cols_padd.name_col;
|
||||
if (d_elem->cols_padd.group_col > s_ls->max_cols_padd->group_col)
|
||||
s_ls->max_cols_padd->group_col = d_elem->cols_padd.group_col;
|
||||
if (d_elem->cols_padd.size_mn_col > s_ls->max_cols_padd->size_mn_col)
|
||||
s_ls->max_cols_padd->size_mn_col = d_elem->cols_padd.size_mn_col;
|
||||
if (d_elem->stat_copy)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char get_file_type(t_dir_elm *d_elm)
|
||||
{
|
||||
char ch;
|
||||
|
||||
ch = '-';
|
||||
if (S_ISDIR(d_elm->stat_copy->st_mode))
|
||||
ch = 'd';
|
||||
if (S_ISLNK(d_elm->stat_copy->st_mode))
|
||||
ch = 'l';
|
||||
if (S_ISFIFO(d_elm->stat_copy->st_mode))
|
||||
ch = 'p';
|
||||
if (S_ISSOCK(d_elm->stat_copy->st_mode))
|
||||
ch = 's';
|
||||
if (S_ISBLK(d_elm->stat_copy->st_mode))
|
||||
ch = 'b';
|
||||
if (S_ISCHR(d_elm->stat_copy->st_mode))
|
||||
ch = 'c';
|
||||
return (ch);
|
||||
}
|
||||
|
||||
void get_file_permissions(char *attr_str, t_dir_elm *d_elm)
|
||||
{
|
||||
struct stat *st;
|
||||
char r;
|
||||
char w;
|
||||
char x;
|
||||
char f;
|
||||
|
||||
r = 'r';
|
||||
w = 'w';
|
||||
x = 'x';
|
||||
f = '-';
|
||||
st = d_elm->stat_copy;
|
||||
attr_str[0] = (st->st_mode & S_IRUSR) ? r : f;
|
||||
attr_str[1] = (st->st_mode & S_IWUSR) ? w : f;
|
||||
attr_str[2] = (st->st_mode & S_IXUSR) ? x : f;
|
||||
attr_str[3] = (st->st_mode & S_IRGRP) ? r : f;
|
||||
attr_str[4] = (st->st_mode & S_IWGRP) ? w : f;
|
||||
attr_str[5] = (st->st_mode & S_IXGRP) ? x : f;
|
||||
attr_str[6] = (st->st_mode & S_IROTH) ? r : f;
|
||||
attr_str[7] = (st->st_mode & S_IWOTH) ? w : f;
|
||||
attr_str[8] = (st->st_mode & S_IXOTH) ? x : f;
|
||||
}
|
||||
|
||||
char *get_attr(t_dir_elm *d_elm)
|
||||
{
|
||||
char *attr_str;
|
||||
|
||||
attr_str = ft_memalloc(sizeof(11));
|
||||
attr_str[10] = '\0';
|
||||
attr_str[0] = get_file_type(d_elm);
|
||||
get_file_permissions(attr_str + 1, d_elm);
|
||||
return (attr_str);
|
||||
}
|
||||
|
||||
char *get_time(t_dir_elm *d_elm)
|
||||
{
|
||||
char *time;
|
||||
char *time_tmp;
|
||||
|
||||
time_tmp = ctime(&(d_elm->stat_copy->st_mtimespec.tv_sec));
|
||||
time_tmp[16] = 0;
|
||||
time = ft_strdup(time_tmp + 4);
|
||||
return (time);
|
||||
}
|
91
src/stat_routine.c
Normal file
91
src/stat_routine.c
Normal file
|
@ -0,0 +1,91 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* stat_routine.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 06:33:59 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:34:00 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void set_columns_padd(t_dir_elm *de)
|
||||
{
|
||||
de->cols_padd.link_col = ft_num_len(de->stat_copy->st_nlink);
|
||||
de->cols_padd.name_col = (int)ft_strlen(de->u_name);
|
||||
de->cols_padd.group_col = (int)ft_strlen(de->g_name);
|
||||
if (S_ISBLK(de->stat_copy->st_mode) ||
|
||||
S_ISCHR(de->stat_copy->st_mode))
|
||||
{
|
||||
de->cols_padd.size_mn_col = ft_num_len(major(de->stat_copy->st_rdev)) +
|
||||
ft_num_len(minor(de->stat_copy->st_rdev)) +
|
||||
2;
|
||||
}
|
||||
else
|
||||
de->cols_padd.size_mn_col = ft_num_len(de->stat_copy->st_size);
|
||||
}
|
||||
|
||||
void get_info_from_stat(t_dir_elm *dir_elm)
|
||||
{
|
||||
dir_elm->attr_str = get_attr(dir_elm);
|
||||
dir_elm->u_name = ft_strdup(getpwuid(dir_elm->stat_copy->st_uid)->pw_name);
|
||||
dir_elm->g_name = ft_strdup(getgrgid(dir_elm->stat_copy->st_gid)->gr_name);
|
||||
dir_elm->m_time = get_time(dir_elm);
|
||||
}
|
||||
|
||||
t_list *read_stat(char *tmp_full_path, char *filename, t_ft_ls *s_ls)
|
||||
{
|
||||
t_dir_elm de;
|
||||
t_list *dir_node;
|
||||
|
||||
file_node_init(&de);
|
||||
de.elm_name = ft_strdup(filename);
|
||||
de.stat_copy = malloc(sizeof(struct stat));
|
||||
lstat(tmp_full_path, de.stat_copy);
|
||||
if (s_ls->flgs->l)
|
||||
{
|
||||
if (S_ISLNK(de.stat_copy->st_mode) && (de.link_name = ft_strnew(1024)))
|
||||
{
|
||||
if (readlink(tmp_full_path, de.link_name, PATH_MAX) == -1)
|
||||
put_error(0);
|
||||
}
|
||||
else
|
||||
de.link_name = NULL;
|
||||
get_info_from_stat(&de);
|
||||
set_columns_padd(&de);
|
||||
}
|
||||
dir_node = ft_lstnew(&de, sizeof(t_dir_elm));
|
||||
return (dir_node);
|
||||
}
|
||||
|
||||
t_list *init_read_stat(char *full_path, char *filename, t_ft_ls *s_ls)
|
||||
{
|
||||
t_list *dir_node;
|
||||
char *tmp_file_path;
|
||||
|
||||
tmp_file_path = get_full_path(full_path, filename);
|
||||
if (access(tmp_file_path, F_OK) == 0)
|
||||
{
|
||||
dir_node = read_stat(tmp_file_path, filename, s_ls);
|
||||
free(tmp_file_path);
|
||||
return (dir_node);
|
||||
}
|
||||
else
|
||||
{
|
||||
put_error(filename);
|
||||
free(tmp_file_path);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
t_list *init_content_node(char *full_path, char *filename, t_ft_ls *s_ls)
|
||||
{
|
||||
if (s_ls->flgs->a)
|
||||
return (init_read_stat(full_path, filename, s_ls));
|
||||
else if (!is_hidden(filename) || ft_strcmp(filename, ".") == 0)
|
||||
return (init_read_stat(full_path, filename, s_ls));
|
||||
return (NULL);
|
||||
}
|
Loading…
Add table
Reference in a new issue