partial working. Without -r, permission check and bonuses
This commit is contained in:
parent
6c97f1251c
commit
1e2d17cebd
7 changed files with 299 additions and 240 deletions
36
inc/ft_ls.h
36
inc/ft_ls.h
|
@ -25,6 +25,9 @@
|
|||
# include <sys/types.h>
|
||||
# include <errno.h>
|
||||
|
||||
# define FILS 0
|
||||
# define DIRS 1
|
||||
|
||||
typedef struct s_flags
|
||||
{
|
||||
int l;
|
||||
|
@ -48,43 +51,42 @@ typedef struct s_col_len
|
|||
typedef struct s_dir_elm
|
||||
{
|
||||
char *elm_name;
|
||||
char *link_name;
|
||||
char *m_time;
|
||||
char *g_name;
|
||||
char *u_name;
|
||||
char *attr_str;
|
||||
int link_fd;
|
||||
struct stat *stat_buf_struc;
|
||||
t_flags *flags;
|
||||
t_col_len columns_len;
|
||||
struct stat *stat_copy;
|
||||
t_col_len cols_padd;
|
||||
} t_dir_elm;
|
||||
|
||||
typedef struct s_ft_ls
|
||||
{
|
||||
t_list *lst_fil_names;
|
||||
t_list *lst_dir_paths;
|
||||
t_list *lst_fil_meta;
|
||||
t_list *lst_dir_content;
|
||||
t_flags *flgs;
|
||||
t_col_len *max_cols_padd;
|
||||
int dir_content_total;
|
||||
int one_dir;
|
||||
int no_ops;
|
||||
int first_print;
|
||||
t_flags *flgs;
|
||||
t_col_len *padding;
|
||||
int files_print;
|
||||
} t_ft_ls;
|
||||
|
||||
void ft_ls(t_ft_ls s_ls);
|
||||
void ft_ls(t_ft_ls *s_ls, t_list *paths, char *curr_path);
|
||||
|
||||
void ft_ls_fil(t_ft_ls *s_ls, char *full_path);
|
||||
void print_content(t_ft_ls *s_ls, char *full_path_curr);
|
||||
|
||||
void ft_ls_dir(t_ft_ls s_ls, char *full_path);
|
||||
int parse_input(int ac, char **av, t_list **file_and_dirs,
|
||||
t_flags *flgs);
|
||||
|
||||
int parse_input(int ac, char **av, t_ft_ls *ft_ls_strct);
|
||||
|
||||
void init_file_meta_lst(t_ft_ls *s_ls, char *full_path);
|
||||
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 sort_files(t_list **del, t_flags *flgs);
|
||||
void sort_dirs(t_list **dirs, t_flags *flgs);
|
||||
void sort_paths(t_list **dirs, t_flags *flgs);
|
||||
|
||||
void output(t_ft_ls s_ls);
|
||||
void output(t_ft_ls *s_ls);
|
||||
void print_full_path(t_ft_ls *s_ls, char *path);
|
||||
|
||||
int chck_flgs(char *flg, t_flags *flgs);
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
void ft_lstadd(t_list **alst, t_list *new)
|
||||
{
|
||||
if (alst)
|
||||
if (alst && new)
|
||||
{
|
||||
new->next = *alst;
|
||||
*alst = new;
|
||||
|
|
243
src/ft_ls.c
243
src/ft_ls.c
|
@ -32,17 +32,17 @@ char get_file_type(t_dir_elm *d_elm)
|
|||
char ch;
|
||||
|
||||
ch = '-';
|
||||
if (S_ISDIR(d_elm->stat_buf_struc->st_mode))
|
||||
if (S_ISDIR(d_elm->stat_copy->st_mode))
|
||||
ch = 'd';
|
||||
if (S_ISLNK(d_elm->stat_buf_struc->st_mode))
|
||||
if (S_ISLNK(d_elm->stat_copy->st_mode))
|
||||
ch = 'l';
|
||||
if (S_ISFIFO(d_elm->stat_buf_struc->st_mode))
|
||||
if (S_ISFIFO(d_elm->stat_copy->st_mode))
|
||||
ch = 'p';
|
||||
if (S_ISSOCK(d_elm->stat_buf_struc->st_mode))
|
||||
if (S_ISSOCK(d_elm->stat_copy->st_mode))
|
||||
ch = 's';
|
||||
if (S_ISBLK(d_elm->stat_buf_struc->st_mode))
|
||||
if (S_ISBLK(d_elm->stat_copy->st_mode))
|
||||
ch = 'b';
|
||||
if (S_ISCHR(d_elm->stat_buf_struc->st_mode))
|
||||
if (S_ISCHR(d_elm->stat_copy->st_mode))
|
||||
ch = 'c';
|
||||
return (ch);
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ void get_file_permissions(char *attr_str, t_dir_elm *d_elm)
|
|||
w = 'w';
|
||||
x = 'x';
|
||||
f = '-';
|
||||
st = d_elm->stat_buf_struc;
|
||||
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;
|
||||
|
@ -88,133 +88,188 @@ char *get_time(t_dir_elm *d_elm)
|
|||
char *time;
|
||||
char *time_tmp;
|
||||
|
||||
time_tmp = ctime(&(d_elm->stat_buf_struc->st_mtim.tv_sec));
|
||||
time_tmp = ctime(&(d_elm->stat_copy->st_mtim.tv_sec));
|
||||
time_tmp[16] = 0;
|
||||
time = ft_strdup(time_tmp + 4);
|
||||
return (time);
|
||||
}
|
||||
|
||||
void get_padding(t_ft_ls *s_ls, t_dir_elm *d_elem)
|
||||
void get_max_padd(t_ft_ls *s_ls, t_dir_elm *d_elem)
|
||||
{
|
||||
int links_len;
|
||||
int name_len;
|
||||
int group_len;
|
||||
int size_len;
|
||||
|
||||
links_len = ft_num_len(d_elem->stat_buf_struc->st_nlink);
|
||||
name_len = (int)ft_strlen(d_elem->u_name);
|
||||
group_len = (int)ft_strlen(d_elem->g_name);
|
||||
size_len = ft_num_len(d_elem->stat_buf_struc->st_size);
|
||||
if (links_len > s_ls->padding->link_col)
|
||||
s_ls->padding->link_col = links_len;
|
||||
if (name_len > s_ls->padding->name_col)
|
||||
s_ls->padding->name_col = name_len;
|
||||
if (group_len > s_ls->padding->group_col)
|
||||
s_ls->padding->group_col = group_len;
|
||||
if (size_len > s_ls->padding->size_col)
|
||||
s_ls->padding->size_col = size_len;
|
||||
if (d_elem && s_ls)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void get_info_from_stat(t_dir_elm *dir_elm, t_ft_ls *s_ls)
|
||||
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_buf_struc->st_uid)->pw_name);
|
||||
dir_elm->g_name = ft_strdup(getgrgid(dir_elm->stat_buf_struc->st_gid)->gr_name);
|
||||
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);
|
||||
get_padding(s_ls, dir_elm);
|
||||
}
|
||||
|
||||
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 init_file_meta_lst(t_ft_ls *s_ls, char *full_path)
|
||||
t_list *read_stat(char *full_path, char *filename, t_ft_ls *s_ls)
|
||||
{
|
||||
t_dir_elm de;
|
||||
char *tmp_path;
|
||||
t_list *lst_rnr;
|
||||
char *tmp_file_path;
|
||||
|
||||
lst_rnr = s_ls->lst_fil_names;
|
||||
s_ls->lst_fil_meta = NULL;
|
||||
while (lst_rnr)
|
||||
de.elm_name = ft_strdup(filename);
|
||||
if (s_ls->flgs->l)
|
||||
{
|
||||
de.stat_buf_struc = malloc(sizeof(struct stat));
|
||||
de.elm_name = (char *)lst_rnr->content;
|
||||
tmp_path = get_full_path(full_path, de.elm_name);
|
||||
stat(tmp_path, de.stat_buf_struc);
|
||||
get_info_from_stat(&de, s_ls);
|
||||
ft_lstadd(&s_ls->lst_fil_meta, ft_lstnew(&de, sizeof(t_dir_elm)));
|
||||
free(tmp_path);
|
||||
lst_rnr = lst_rnr->next;
|
||||
tmp_file_path = get_full_path(full_path, filename);
|
||||
de.stat_copy = malloc(sizeof(struct stat));
|
||||
lstat(tmp_file_path, de.stat_copy);
|
||||
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);
|
||||
}
|
||||
get_info_from_stat(&de);
|
||||
set_columns_padd(&de);
|
||||
free(tmp_file_path);
|
||||
}
|
||||
return (ft_lstnew(&de, sizeof(t_dir_elm)));
|
||||
}
|
||||
|
||||
t_list *extract_filenames_from_dir(char *full_path)
|
||||
t_list *init_content_node(char *full_path, char *filename, t_ft_ls *s_ls)
|
||||
{
|
||||
DIR *dp;
|
||||
struct dirent *dir_ent;
|
||||
t_list *dir_files;
|
||||
|
||||
dp = opendir(full_path);
|
||||
dir_files = NULL;
|
||||
if (dp != NULL)
|
||||
while ((dir_ent = readdir(dp)))
|
||||
fill_path_lst(&dir_files, dir_ent->d_name);
|
||||
closedir(dp);
|
||||
return (dir_files);
|
||||
if (s_ls->flgs->a)
|
||||
return (read_stat(full_path, filename, s_ls));
|
||||
else if (*filename != '.')
|
||||
return (read_stat(full_path, filename, s_ls));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_ft_ls extract_dirs_from_filenames(t_ft_ls s_ls, char *full_path)
|
||||
t_list *extract_dirs_from_filenames(char *full_path, t_ft_ls *s_ls)
|
||||
{
|
||||
char *full_path_with_file;
|
||||
t_dir_elm *dir_elm;
|
||||
t_list *dir_paths;
|
||||
t_list *dir_content;
|
||||
|
||||
s_ls.lst_dir_paths = NULL;
|
||||
while (s_ls.lst_fil_names)
|
||||
dir_content = s_ls->lst_dir_content;
|
||||
dir_paths = NULL;
|
||||
while (dir_content)
|
||||
{
|
||||
full_path_with_file = get_full_path(full_path, s_ls.lst_fil_names->content);
|
||||
if (access(full_path_with_file, F_OK))
|
||||
if (access(full_path_with_file, R_OK) && (ft_strcmp(s_ls.lst_fil_names->content, ".") != 0) && (ft_strcmp(s_ls.lst_fil_names->content, "..") != 0))
|
||||
{
|
||||
if (s_ls.flgs->a)
|
||||
fill_path_lst(&s_ls.lst_dir_paths, s_ls.lst_fil_names->content);
|
||||
else if (*(char *)s_ls.lst_fil_names->content != '.')
|
||||
fill_path_lst(&s_ls.lst_dir_paths, s_ls.lst_fil_names->content);
|
||||
}
|
||||
s_ls.lst_fil_names = s_ls.lst_fil_names->next;
|
||||
dir_elm = (t_dir_elm *)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)
|
||||
// fill_path_lst(&dir_paths, dir_elm->elm_name);
|
||||
// else if (*dir_elm->elm_name != '.')
|
||||
// fill_path_lst(&dir_paths, dir_elm->elm_name);
|
||||
// }
|
||||
if (opendir(full_path_with_file) && (ft_strcmp(dir_elm->elm_name, ".") != 0) && (ft_strcmp(dir_elm->elm_name, "..") != 0))
|
||||
{
|
||||
if (s_ls->flgs->a)
|
||||
fill_path_lst(&dir_paths, dir_elm->elm_name);
|
||||
else if (*dir_elm->elm_name != '.')
|
||||
fill_path_lst(&dir_paths, dir_elm->elm_name);
|
||||
}
|
||||
dir_content = dir_content->next;
|
||||
free(full_path_with_file);
|
||||
}
|
||||
return (s_ls);
|
||||
return (dir_paths);
|
||||
}
|
||||
|
||||
void ft_ls_dir(t_ft_ls s_ls, char *full_path)
|
||||
void get_padding_and_blocks(t_ft_ls *s_ls, t_dir_elm *d_elem)
|
||||
{
|
||||
char *full_path_next;
|
||||
t_list *tmp_rnr;
|
||||
|
||||
sort_dirs(&s_ls.lst_dir_paths, s_ls.flgs);
|
||||
tmp_rnr = s_ls.lst_dir_paths;
|
||||
while (tmp_rnr)
|
||||
if (d_elem && s_ls)
|
||||
{
|
||||
full_path_next = get_full_path(full_path, tmp_rnr->content);
|
||||
s_ls.lst_fil_names = extract_filenames_from_dir(full_path_next);
|
||||
ft_ls_fil(&s_ls, full_path_next);
|
||||
if (s_ls.flgs->R)
|
||||
ft_ls_dir(extract_dirs_from_filenames(s_ls, full_path_next), full_path_next);
|
||||
tmp_rnr = tmp_rnr->next;
|
||||
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)
|
||||
s_ls->dir_content_total += d_elem->stat_copy->st_blocks;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_ls_fil(t_ft_ls *s_ls, char *full_path)
|
||||
void extract_content(char *full_path, t_ft_ls *s_ls)
|
||||
{
|
||||
DIR *dp;
|
||||
struct dirent *de;
|
||||
t_list *lst_elm;
|
||||
|
||||
init_file_meta_lst(s_ls, full_path);
|
||||
sort_files(&s_ls->lst_fil_meta, s_ls->flgs);
|
||||
if ((!s_ls->one_dir && !s_ls->no_ops) || s_ls->flgs->R)
|
||||
print_full_path(s_ls, full_path);
|
||||
output(*s_ls);
|
||||
s_ls->lst_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_max_padd(s_ls, lst_elm->content);
|
||||
get_padding_and_blocks(s_ls, lst_elm->content);
|
||||
ft_lstadd(&s_ls->lst_dir_content, lst_elm);
|
||||
}
|
||||
}
|
||||
closedir(dp);
|
||||
}
|
||||
else
|
||||
{
|
||||
lst_elm = init_content_node(NULL, full_path, s_ls);
|
||||
get_max_padd(s_ls, lst_elm->content);
|
||||
ft_lstadd(&s_ls->lst_dir_content, lst_elm);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_ls(t_ft_ls s_ls)
|
||||
void reset_max_cols(t_ft_ls *s_ls)
|
||||
{
|
||||
if (s_ls.lst_fil_names)
|
||||
ft_ls_fil(&s_ls, NULL);
|
||||
if (s_ls.lst_dir_paths)
|
||||
ft_ls_dir(s_ls, NULL);
|
||||
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;
|
||||
}
|
||||
|
||||
void print_content(t_ft_ls *s_ls, char *full_path_curr)
|
||||
{
|
||||
t_list *dirs;
|
||||
|
||||
sort_files(&s_ls->lst_dir_content, s_ls->flgs);
|
||||
if ((!s_ls->one_dir && !s_ls->no_ops) || s_ls->flgs->R)
|
||||
print_full_path(s_ls, full_path_curr);
|
||||
output(s_ls);
|
||||
if (s_ls->flgs->R)
|
||||
{
|
||||
dirs = extract_dirs_from_filenames(full_path_curr, s_ls);
|
||||
reset_max_cols(s_ls);
|
||||
ft_ls(s_ls, dirs, full_path_curr);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_ls(t_ft_ls *s_ls, t_list *paths, char *curr_path)
|
||||
{
|
||||
char *cur_filename;
|
||||
|
||||
sort_paths(&paths, s_ls->flgs);
|
||||
while (paths)
|
||||
{
|
||||
cur_filename = get_full_path(curr_path, paths->content);
|
||||
extract_content(cur_filename, s_ls);
|
||||
print_content(s_ls, cur_filename);
|
||||
paths = paths->next;
|
||||
}
|
||||
}
|
69
src/main.c
69
src/main.c
|
@ -12,29 +12,58 @@
|
|||
|
||||
#include "ft_ls.h"
|
||||
|
||||
// complide with -O3 flag!!
|
||||
|
||||
void init_base_structs(t_ft_ls *s_ls, t_flags *flgs, t_col_len *padd,
|
||||
t_list **fil_dir)
|
||||
{
|
||||
fil_dir[FILS] = NULL;
|
||||
fil_dir[DIRS] = NULL;
|
||||
s_ls->flgs = flgs;
|
||||
s_ls->max_cols_padd = padd;
|
||||
s_ls->first_print = 1;
|
||||
s_ls->files_print = 0;
|
||||
}
|
||||
|
||||
void ft_ls_start(t_ft_ls *s_ls, t_list **file_and_dirs, int no_error_operands)
|
||||
{
|
||||
if (file_and_dirs[FILS])
|
||||
{
|
||||
s_ls->files_print = 1;
|
||||
sort_paths(&file_and_dirs[FILS], s_ls->flgs);
|
||||
ft_ls(s_ls, file_and_dirs[FILS], NULL);
|
||||
s_ls->files_print = 0;
|
||||
s_ls->first_print = 0;
|
||||
}
|
||||
if (file_and_dirs[DIRS])
|
||||
{
|
||||
sort_paths(&file_and_dirs[DIRS], s_ls->flgs);
|
||||
if (ft_lst_len(file_and_dirs[FILS]) == 0 &&
|
||||
ft_lst_len(file_and_dirs[DIRS]) == 1)
|
||||
if (!s_ls->flgs->R)
|
||||
s_ls->one_dir = 1;
|
||||
ft_ls(s_ls, file_and_dirs[DIRS], NULL);
|
||||
}
|
||||
if (no_error_operands && (!file_and_dirs[DIRS] && !file_and_dirs[FILS]))
|
||||
{
|
||||
s_ls->one_dir = 1;
|
||||
fill_path_lst(&file_and_dirs[DIRS], ".");
|
||||
ft_ls(s_ls, file_and_dirs[DIRS], NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
static t_flags flgs;
|
||||
static t_flags flgs;
|
||||
static t_col_len padding;
|
||||
static t_ft_ls s_ls;
|
||||
int no_errors_and_valid_args;
|
||||
static t_ft_ls s_ls;
|
||||
t_list **file_and_dirs;
|
||||
int no_error_operands;
|
||||
|
||||
s_ls.flgs = &flgs;
|
||||
s_ls.padding = &padding;
|
||||
s_ls.first_print = 1;
|
||||
no_errors_and_valid_args = parse_input(argc, argv, &s_ls);
|
||||
if (s_ls.lst_dir_paths || s_ls.lst_fil_names)
|
||||
{
|
||||
if (ft_lst_len(s_ls.lst_fil_names) == 0 && ft_lst_len(s_ls.lst_dir_paths) == 1)
|
||||
if (!s_ls.flgs->R)
|
||||
s_ls.one_dir = 1;
|
||||
ft_ls(s_ls);
|
||||
}
|
||||
else if (no_errors_and_valid_args)
|
||||
{
|
||||
s_ls.no_ops = 1;
|
||||
fill_path_lst(&s_ls.lst_dir_paths, "./");
|
||||
ft_ls(s_ls);
|
||||
}
|
||||
file_and_dirs = malloc(sizeof(t_list *) * 2);
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
|
161
src/output.c
161
src/output.c
|
@ -1,85 +1,27 @@
|
|||
#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);
|
||||
}
|
||||
|
||||
//int out(t_list *entity)
|
||||
//{
|
||||
// t_dir_elm *d_elm;
|
||||
//
|
||||
// d_elm = (t_dir_elm *)entity->content;
|
||||
// if (d_elm->flags->a)
|
||||
// {
|
||||
// column_or_line(entity);
|
||||
// return (1);
|
||||
// }
|
||||
// else
|
||||
// if (!is_hidden(d_elm->elm_name))
|
||||
// {
|
||||
// column_or_line(entity);
|
||||
// return (1);
|
||||
// }
|
||||
// return (0);
|
||||
//}
|
||||
|
||||
void print_full_path(t_ft_ls *s_ls, char *path)
|
||||
{
|
||||
char *cur_dir_semi_n;
|
||||
char *n_cur_dir;
|
||||
|
||||
n_cur_dir = NULL;
|
||||
if (!path)
|
||||
s_ls->first_print = 0;
|
||||
if (s_ls->first_print)
|
||||
if (!s_ls->files_print)
|
||||
{
|
||||
cur_dir_semi_n = ft_strjoin(path, ":\n");
|
||||
s_ls->first_print = 0;
|
||||
if (s_ls->first_print)
|
||||
{
|
||||
s_ls->first_print = 0;
|
||||
cur_dir_semi_n = ft_strjoin(path, ":\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
n_cur_dir = ft_strjoin("\n", path);
|
||||
cur_dir_semi_n = ft_strjoin(n_cur_dir, ":\n");
|
||||
}
|
||||
ft_putstr(cur_dir_semi_n);
|
||||
free(cur_dir_semi_n);
|
||||
free(n_cur_dir);
|
||||
}
|
||||
else
|
||||
{
|
||||
n_cur_dir = ft_strjoin("\n", path);
|
||||
cur_dir_semi_n = ft_strjoin(n_cur_dir, ":\n");
|
||||
}
|
||||
ft_putstr(cur_dir_semi_n);
|
||||
free(cur_dir_semi_n);
|
||||
free(n_cur_dir);
|
||||
}
|
||||
|
||||
|
||||
int ft_lstiter_ret(t_list *lst, int (*f)(t_list *elem))
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (lst && f)
|
||||
{
|
||||
if (f(lst))
|
||||
i = 1;
|
||||
lst = lst->next;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
void put_spaces(int num)
|
||||
|
@ -88,54 +30,85 @@ void put_spaces(int num)
|
|||
ft_putstr(" ");
|
||||
}
|
||||
|
||||
void format_string(t_dir_elm *d_elem, t_col_len *padd)
|
||||
void put_total(int total)
|
||||
{
|
||||
int links_len;
|
||||
int owner_len;
|
||||
int group_len;
|
||||
int size_len;
|
||||
ft_putstr("total");
|
||||
ft_putstr(" ");
|
||||
ft_putnbr(total);
|
||||
ft_putstr("\n");
|
||||
}
|
||||
|
||||
links_len = ft_num_len(d_elem->stat_buf_struc->st_nlink);
|
||||
owner_len = (int)ft_strlen(d_elem->u_name);
|
||||
group_len = (int)ft_strlen(d_elem->g_name);
|
||||
size_len = ft_num_len(d_elem->stat_buf_struc->st_size);
|
||||
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(padd->link_col - links_len + 1);
|
||||
ft_putnbr((int)d_elem->stat_buf_struc->st_nlink);
|
||||
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(padd->name_col - owner_len + 1);
|
||||
put_spaces(s_ls->max_cols_padd->name_col - d_elem->cols_padd.name_col + 1);
|
||||
ft_putstr(d_elem->g_name);
|
||||
put_spaces(padd->group_col - group_len);
|
||||
put_spaces(padd->size_col - size_len + 1);
|
||||
ft_putnbr((int)d_elem->stat_buf_struc->st_size);
|
||||
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 long_output(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;
|
||||
if (lst_d_elm->next)
|
||||
ft_putstr(" ");
|
||||
else
|
||||
ft_putstr("\n");
|
||||
}
|
||||
|
||||
void out(t_ft_ls *s_ls, void (*format_fun)(t_list *, t_ft_ls *))
|
||||
{
|
||||
t_list *lst_rnr;
|
||||
|
||||
lst_rnr = s_ls.lst_fil_meta;
|
||||
lst_rnr = s_ls->lst_dir_content;
|
||||
while (lst_rnr)
|
||||
{
|
||||
format_string(lst_rnr->content, s_ls.padding);
|
||||
if (s_ls->flgs->a)
|
||||
format_fun(lst_rnr, s_ls);
|
||||
else if (*((t_dir_elm *)lst_rnr->content)->elm_name != '.')
|
||||
format_fun(lst_rnr, s_ls);
|
||||
lst_rnr = lst_rnr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void output(t_ft_ls s_ls)
|
||||
void output(t_ft_ls *s_ls)
|
||||
{
|
||||
if (s_ls.flgs->l)
|
||||
long_output(s_ls);
|
||||
if (s_ls->flgs->l)
|
||||
{
|
||||
if (!s_ls->files_print)
|
||||
put_total(s_ls->dir_content_total);
|
||||
out(s_ls, long_format);
|
||||
}
|
||||
else
|
||||
out(s_ls, one_line);
|
||||
// else if (s_ls.flgs->col)
|
||||
// one_column(s_ls);
|
||||
// else
|
||||
// mult_column(s_ls);
|
||||
|
||||
// ft_lstiter_ret(s_ls.lst_fil_meta, out);
|
||||
// ft_lstiter_ret(s_ls.lst_dir_content, out);
|
||||
}
|
|
@ -24,7 +24,7 @@ void fill_path_lst(t_list **path_lst, char *path)
|
|||
ft_lstadd(path_lst, ft_lstnew(path, ft_strlen(path) + 1));
|
||||
}
|
||||
|
||||
int parse_input(int ac, char **av, t_ft_ls *ft_ls_strct)
|
||||
int parse_input(int ac, char **av, t_list **file_and_dirs, t_flags *flgs)
|
||||
{
|
||||
int i;
|
||||
int open_type;
|
||||
|
@ -36,13 +36,13 @@ int parse_input(int ac, char **av, t_ft_ls *ft_ls_strct)
|
|||
return (no_errors);
|
||||
while (i < ac - 1)
|
||||
{
|
||||
if (chck_flgs(av[i + 1], ft_ls_strct->flgs))
|
||||
if (chck_flgs(av[i + 1], flgs))
|
||||
{
|
||||
open_type = chck_opn(av[i + 1]);
|
||||
if (open_type == 0)
|
||||
fill_path_lst(&ft_ls_strct->lst_fil_names, av[i + 1]);
|
||||
fill_path_lst(&file_and_dirs[FILS], av[i + 1]);
|
||||
else if (open_type == 1)
|
||||
fill_path_lst(&ft_ls_strct->lst_dir_paths, av[i + 1]);
|
||||
fill_path_lst(&file_and_dirs[DIRS], av[i + 1]);
|
||||
else if (open_type == -1)
|
||||
no_errors = 0;
|
||||
}
|
||||
|
|
20
src/sort.c
20
src/sort.c
|
@ -2,10 +2,10 @@
|
|||
|
||||
int by_name_and_same_type(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if ((S_ISDIR(a->stat_buf_struc->st_mode) &&
|
||||
S_ISDIR(b->stat_buf_struc->st_mode)) ||
|
||||
(S_ISREG(a->stat_buf_struc->st_mode) &&
|
||||
S_ISREG(b->stat_buf_struc->st_mode)))
|
||||
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);
|
||||
|
@ -27,7 +27,7 @@ int by_name(t_dir_elm *a, t_dir_elm *b)
|
|||
|
||||
int by_size(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (a->stat_buf_struc->st_size <= b->stat_buf_struc->st_size)
|
||||
if (a->stat_copy->st_size <= b->stat_copy->st_size)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
@ -36,10 +36,10 @@ int by_m_time(t_dir_elm *a, t_dir_elm *b)
|
|||
{
|
||||
long dif;
|
||||
|
||||
dif = a->stat_buf_struc->st_mtim.tv_sec - b->stat_buf_struc->st_mtim.tv_sec;
|
||||
dif = a->stat_copy->st_mtim.tv_sec - b->stat_copy->st_mtim.tv_sec;
|
||||
if (!dif)
|
||||
dif = a->stat_buf_struc->st_mtim.tv_nsec -
|
||||
b->stat_buf_struc->st_mtim.tv_nsec;
|
||||
dif = a->stat_copy->st_mtim.tv_nsec -
|
||||
b->stat_copy->st_mtim.tv_nsec;
|
||||
if (dif < 0)
|
||||
return (0);
|
||||
else if (dif > 0)
|
||||
|
@ -50,7 +50,7 @@ int by_m_time(t_dir_elm *a, t_dir_elm *b)
|
|||
|
||||
int by_type(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (S_ISREG(b->stat_buf_struc->st_mode) && a)
|
||||
if (S_ISREG(b->stat_copy->st_mode) && a)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ void sort_files(t_list **del, t_flags *flgs)
|
|||
ft_lst_merge_sort(del, by_name);
|
||||
}
|
||||
|
||||
void sort_dirs(t_list **dirs, t_flags *flgs)
|
||||
void sort_paths(t_list **dirs, t_flags *flgs)
|
||||
{
|
||||
if (flgs->abn)
|
||||
ft_lst_merge_sort(dirs, path_by_name_lex);
|
||||
|
|
Loading…
Add table
Reference in a new issue