Compare commits
No commits in common. "2b3c0fd164b70e9fa0085aee9b5686e5b0c1f271" and "d07491045d1ae86f946a61a64f276f158b196c39" have entirely different histories.
2b3c0fd164
...
d07491045d
24 changed files with 235 additions and 1260 deletions
|
@ -1,26 +1,14 @@
|
|||
cmake_minimum_required(VERSION 3.6)
|
||||
project(ft_ls)
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -g")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -O0 -g")
|
||||
|
||||
link_directories(libft) # libraries
|
||||
include_directories(inc libft/includes) # headers
|
||||
link_directories(libft) # libraries
|
||||
|
||||
set(SOURCE_FILES
|
||||
inc/ft_ls.h
|
||||
src/main.c
|
||||
src/ft_ls.c
|
||||
src/parse_input.c
|
||||
src/flags.c
|
||||
src/sort.c
|
||||
src/output.c
|
||||
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
|
||||
) # sources
|
||||
|
||||
add_executable(ft_ls ${SOURCE_FILES}) # compilation
|
||||
|
||||
|
|
8
Makefile
8
Makefile
|
@ -6,7 +6,7 @@
|
|||
# By: gtertysh <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2016/12/14 16:54:48 by gtertysh #+# #+# #
|
||||
# Updated: 2017/04/01 22:10:55 by gtertysh ### ########.fr #
|
||||
# Updated: 2017/03/27 15:23:35 by gtertysh ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
@ -43,7 +43,7 @@ OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
|
|||
|
||||
# header files and directories
|
||||
|
||||
HEADER = ft_ls_dir.h
|
||||
HEADER = ft_ls.h
|
||||
FT_LS_INC = ./inc/
|
||||
LIBFT_INC = $(LIBFT_DIR)includes/
|
||||
INC = -I $(LIBFT_DIR)includes/ -I $(FT_LS_INC)
|
||||
|
@ -73,12 +73,12 @@ all: $(NAME)
|
|||
|
||||
$(NAME): $(LIBFT_DIR)$(LIBFT) $(OBJ)
|
||||
@echo "$(CYAN)Linking binary...$(NORMAL)"
|
||||
@$(CC) $(OBJ) $(DEBUG) $(FLAGS) -o $(NAME)
|
||||
@$(CC) $(OBJ) $(FLAGS) -o $(NAME)
|
||||
@echo "$(GREEN)Done!$(NORMAL)"
|
||||
|
||||
$(OBJ_DIR)%.o: $(SRC_DIR)%.c $(FT_LS_INC)$(FT_LS_HEADER)
|
||||
@echo "$(CYAN)Compiling object files: $(BLUE)$@$(NORMAL)"
|
||||
@$(CC) $(CC_FLAGS) $(INC) -c $< -o $@
|
||||
@$(CC) $(CC_FLAGS) $(OPT) $(INC) -c $< -o $@
|
||||
|
||||
$(LIBFT_DIR)$(LIBFT):
|
||||
@echo "$(CYAN)Compiling libft library...$(NORMAL)"
|
||||
|
|
92
inc/ft_ls.h
92
inc/ft_ls.h
|
@ -1,12 +1,12 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ls_dir.h :+: :+: :+: */
|
||||
/* ft_ls.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/25 18:27:54 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:52:30 by gtertysh ### ########.fr */
|
||||
/* Updated: 2017/03/25 21:18:30 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -24,93 +24,29 @@
|
|||
# include <grp.h>
|
||||
# include <sys/types.h>
|
||||
# include <errno.h>
|
||||
# include <limits.h>
|
||||
|
||||
# define FILS 0
|
||||
# define DIRS 1
|
||||
|
||||
typedef struct s_flags
|
||||
{
|
||||
int l;
|
||||
int a;
|
||||
int recur;
|
||||
int size;
|
||||
int r;
|
||||
int R;
|
||||
int t;
|
||||
int u;
|
||||
int col;
|
||||
int abn;
|
||||
} t_flags;
|
||||
|
||||
typedef struct s_col_len
|
||||
typedef struct s_dir_entity
|
||||
{
|
||||
int link_col;
|
||||
int name_col;
|
||||
int size_mn_col;
|
||||
int group_col;
|
||||
} t_col_len;
|
||||
|
||||
typedef struct s_dir_elm
|
||||
{
|
||||
char *elm_name;
|
||||
char *link_name;
|
||||
char *m_time;
|
||||
char *name;
|
||||
char *a_time;
|
||||
int size;
|
||||
char *g_name;
|
||||
char *u_name;
|
||||
char *attr_str;
|
||||
struct stat *stat_copy;
|
||||
t_col_len cols_padd;
|
||||
} t_dir_elm;
|
||||
|
||||
typedef struct s_ft_ls
|
||||
{
|
||||
t_flags *flgs;
|
||||
t_col_len *max_cols_padd;
|
||||
int dir_content_total;
|
||||
int one_dir;
|
||||
int no_ops;
|
||||
int first_print;
|
||||
int files_print;
|
||||
} 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 **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);
|
||||
|
||||
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 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);
|
||||
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 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);
|
||||
int links;
|
||||
char *attr;
|
||||
int link_fd;
|
||||
struct dirent *dirent;
|
||||
struct stat *stat_buf;
|
||||
t_flags *flags;
|
||||
} t_dir_entity;
|
||||
|
||||
#endif
|
||||
|
|
|
@ -30,7 +30,6 @@ SRC = ft_memset.c \
|
|||
ft_strstr.c \
|
||||
ft_strnstr.c \
|
||||
ft_strcmp.c \
|
||||
ft_strcmp_lex.c \
|
||||
ft_strncmp.c \
|
||||
ft_atoi.c \
|
||||
ft_isalpha.c \
|
||||
|
@ -78,13 +77,10 @@ SRC = ft_memset.c \
|
|||
ft_lst_at.c \
|
||||
ft_lstadd_back.c \
|
||||
ft_lst_len.c \
|
||||
ft_lst_merge_sort.c \
|
||||
ft_lst_rev.c \
|
||||
\
|
||||
ft_realloc.c \
|
||||
ft_read_file.c \
|
||||
get_next_line.c \
|
||||
ft_num_len.c
|
||||
get_next_line.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
INC = -I ./includes/
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)())
|
||||
{
|
||||
t_list *result;
|
||||
|
||||
result = NULL;
|
||||
if (a == NULL)
|
||||
return (b);
|
||||
else if (b == NULL)
|
||||
return (a);
|
||||
if (cmp(a->content, b->content))
|
||||
{
|
||||
result = a;
|
||||
result->next = merge(a->next, b, cmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = b;
|
||||
result->next = merge(a, b->next, cmp);
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
*front_ptr = source;
|
||||
*back_ptr = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
slow = source;
|
||||
fast = source->next;
|
||||
while (fast != NULL)
|
||||
{
|
||||
fast = fast->next;
|
||||
if (fast != NULL)
|
||||
{
|
||||
slow = slow->next;
|
||||
fast = fast->next;
|
||||
}
|
||||
}
|
||||
*front_ptr = source;
|
||||
*back_ptr = slow->next;
|
||||
slow->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)())
|
||||
{
|
||||
t_list *head;
|
||||
t_list *a;
|
||||
t_list *b;
|
||||
|
||||
head = *head_ptr;
|
||||
if ((head == NULL) || (head->next == NULL))
|
||||
return ;
|
||||
split(head, &a, &b);
|
||||
ft_lst_merge_sort(&a, cmp);
|
||||
ft_lst_merge_sort(&b, cmp);
|
||||
*head_ptr = merge(a, b, cmp);
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lst_rev.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/09 16:28:30 by gtertysh #+# #+# */
|
||||
/* Updated: 2016/11/09 17:06:02 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lst_rev(t_list **begin_list)
|
||||
{
|
||||
t_list *prev;
|
||||
t_list *next;
|
||||
t_list *curr;
|
||||
|
||||
prev = NULL;
|
||||
curr = *begin_list;
|
||||
while (curr)
|
||||
{
|
||||
next = curr->next;
|
||||
curr->next = prev;
|
||||
prev = curr;
|
||||
curr = next;
|
||||
}
|
||||
*begin_list = prev;
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
void ft_lstadd(t_list **alst, t_list *new)
|
||||
{
|
||||
if (alst && new)
|
||||
if (alst)
|
||||
{
|
||||
new->next = *alst;
|
||||
*alst = new;
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = 1;
|
||||
if (num > 9)
|
||||
while (num)
|
||||
{
|
||||
num /= 10;
|
||||
if (num)
|
||||
len++;
|
||||
}
|
||||
return (len);
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_tolower.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/01 15:58:52 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:47:00 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_strcmp_lex(const char *s1, const char *s2)
|
||||
{
|
||||
int s1_low;
|
||||
int s2_low;
|
||||
|
||||
s1_low = ft_tolower(*(unsigned char *)s1);
|
||||
s2_low = ft_tolower(*(unsigned char *)s2);
|
||||
while (*s1 && *s2 && s1_low == s2_low)
|
||||
{
|
||||
s1++;
|
||||
s2++;
|
||||
s1_low = ft_tolower(*(unsigned char *)s1);
|
||||
s2_low = ft_tolower(*(unsigned char *)s2);
|
||||
}
|
||||
if (s1_low != s2_low)
|
||||
return (s1_low - s2_low);
|
||||
return (0);
|
||||
}
|
|
@ -17,16 +17,10 @@ char *ft_strdup(const char *s1)
|
|||
char *new;
|
||||
size_t size;
|
||||
|
||||
new = 0;
|
||||
if (s1)
|
||||
{
|
||||
if ((size = ft_strlen(s1)) != 0)
|
||||
{
|
||||
size = ft_strlen(s1);
|
||||
if (!(new = (char *)malloc(sizeof(char) * (size + 1))))
|
||||
return (0);
|
||||
ft_memcpy(new, s1, size);
|
||||
*(new + size) = '\0';
|
||||
}
|
||||
}
|
||||
return (new);
|
||||
}
|
||||
|
|
|
@ -57,7 +57,6 @@ char *ft_strnstr(const char *big, const char *little,
|
|||
size_t len);
|
||||
int ft_strcmp(const char *s1, const char *s2);
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
||||
int ft_strcmp_lex(const char *s1, const char *s2);
|
||||
int ft_atoi(const char *str);
|
||||
int ft_isalpha(int c);
|
||||
int ft_isdigit(int c);
|
||||
|
@ -105,15 +104,10 @@ t_list *ft_lststrsplit(const char *s, char c);
|
|||
t_list *ft_lstfind(t_list *lst, void *data, size_t size);
|
||||
t_list *ft_lst_at(t_list *l, unsigned int at);
|
||||
void ft_lstadd_back(t_list **l, void *data, size_t size);
|
||||
void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)());
|
||||
void ft_lst_rev(t_list **begin_list);
|
||||
|
||||
void *ft_realloc(void *old, unsigned int new_size,
|
||||
unsigned int old_size);
|
||||
|
||||
char *ft_read_file(const int fd);
|
||||
int get_next_line(const int fd, char **line);
|
||||
|
||||
int ft_num_len(long num);
|
||||
|
||||
#endif
|
||||
|
|
22
src/error.c
22
src/error.c
|
@ -1,22 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void put_error(char *arg)
|
||||
{
|
||||
char *ls_arg;
|
||||
|
||||
ls_arg = ft_strjoin("ls: ", arg);
|
||||
perror(ls_arg);
|
||||
free(ls_arg);
|
||||
}
|
62
src/flags.c
62
src/flags.c
|
@ -1,62 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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;
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void chck_short_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (*flg == '-' && *(flg + 1) && (flg += 1))
|
||||
{
|
||||
if (*(flg) == '-')
|
||||
{
|
||||
if (!chck_long_flgs(flg, flgs))
|
||||
return (0);
|
||||
else
|
||||
return (1);
|
||||
}
|
||||
chck_short_flgs(flg, flgs);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
61
src/ft_ls.c
61
src/ft_ls.c
|
@ -1,61 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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"
|
||||
|
||||
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_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->recur)
|
||||
print_full_path(s_ls, cur_path);
|
||||
output(s_ls, curr_dir_content);
|
||||
}
|
||||
|
||||
void ft_ls_fil(t_ft_ls *s_ls, t_list **files, char *path)
|
||||
{
|
||||
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 *dirs;
|
||||
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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;
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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");
|
||||
}
|
188
src/main.c
188
src/main.c
|
@ -6,84 +6,158 @@
|
|||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/25 18:26:51 by gtertysh #+# #+# */
|
||||
/* Updated: 2017/04/02 06:33:40 by gtertysh ### ########.fr */
|
||||
/* Updated: 2017/03/25 18:27:42 by gtertysh ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ls.h"
|
||||
|
||||
void free_str_lst(t_list *curr_cont)
|
||||
void put_error(char *arg)
|
||||
{
|
||||
char *tmp;
|
||||
t_list *tmp1;
|
||||
char *final_str;
|
||||
char *arg_colon_errno;
|
||||
char *colon_errno;
|
||||
|
||||
while (curr_cont)
|
||||
{
|
||||
tmp1 = curr_cont->next;
|
||||
if (curr_cont->content)
|
||||
{
|
||||
tmp = (char *)curr_cont->content;
|
||||
free(tmp);
|
||||
}
|
||||
free(curr_cont);
|
||||
curr_cont = tmp1;
|
||||
}
|
||||
colon_errno = ft_strjoin(": ", strerror(errno));
|
||||
arg_colon_errno = ft_strjoin(arg, colon_errno);
|
||||
final_str = ft_strjoin("ls: ", arg_colon_errno);
|
||||
ft_putstr(final_str);
|
||||
ft_putstr("\n");
|
||||
free(final_str);
|
||||
free(arg_colon_errno);
|
||||
free(colon_errno);
|
||||
}
|
||||
|
||||
void init_base_structs(t_ft_ls *s_ls, t_flags *flgs, t_col_len *padd,
|
||||
t_list **fil_dir)
|
||||
int chck_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
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;
|
||||
s_ls->dir_content_total = 0;
|
||||
if ((!*(flg + 1)) && (flg += 1))
|
||||
return (1);
|
||||
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->col == 0) flgs->col = (ft_strchr(flg, '1') != NULL) ? 1 : 0;
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_ls_start(t_ft_ls *s_ls, t_list **file_and_dirs, int no_error_ops)
|
||||
void init_dir_ent_list(DIR *dp, t_list **del, t_flags *flgs)
|
||||
{
|
||||
t_list *file_lst;
|
||||
t_dir_entity de;
|
||||
|
||||
if (file_and_dirs[FILS])
|
||||
|
||||
while ((de.dirent = readdir(dp)) && (de.flags = flgs))
|
||||
ft_lstadd(del, ft_lstnew(&de, sizeof(t_dir_entity)));
|
||||
}
|
||||
|
||||
void column_out(t_list *entity)
|
||||
{
|
||||
t_dir_entity *d_ent;
|
||||
|
||||
d_ent = (t_dir_entity *)entity->content;
|
||||
if (d_ent->flags->a)
|
||||
printf("%s\t", d_ent->dirent->d_name);
|
||||
else
|
||||
if (*d_ent->dirent->d_name != '.')
|
||||
printf("%s\t\n", d_ent->dirent->d_name);
|
||||
}
|
||||
|
||||
void output(t_list *del)
|
||||
{
|
||||
ft_lstiter(del, column_out);
|
||||
}
|
||||
|
||||
void ft_ls(char *path, t_flags *flgs)
|
||||
{
|
||||
DIR *dp;
|
||||
// int blocks;
|
||||
// char *tmp_path;
|
||||
t_list *dir_ent_list;
|
||||
|
||||
// tmp_path = 0;
|
||||
// blocks = 0;
|
||||
dir_ent_list = NULL;
|
||||
if (!(dp = opendir(path)) || open(path, O_RDONLY) == -1)
|
||||
put_error(path);
|
||||
else
|
||||
{
|
||||
file_lst = operands_parse(file_and_dirs[FILS], s_ls);
|
||||
s_ls->files_print = 1;
|
||||
ft_ls_fil(s_ls, &file_lst, NULL);
|
||||
s_ls->files_print = 0;
|
||||
s_ls->first_print = 0;
|
||||
init_dir_ent_list(dp, &dir_ent_list, flgs);
|
||||
output(dir_ent_list);
|
||||
}
|
||||
if (file_and_dirs[DIRS])
|
||||
|
||||
}
|
||||
|
||||
void set_flags_to_zero(t_flags *flgs)
|
||||
{
|
||||
flgs->a = 0;
|
||||
flgs->l = 0;
|
||||
flgs->R = 0;
|
||||
flgs->t = 0;
|
||||
flgs->col = 0;
|
||||
}
|
||||
|
||||
void parse_input(int ac, char **av, t_flags *flgs)
|
||||
{
|
||||
int i;
|
||||
int paths;
|
||||
|
||||
i = 0;
|
||||
set_flags_to_zero(flgs);
|
||||
if (ac == 1)
|
||||
ft_ls(".", flgs);
|
||||
else if (ac > 1)
|
||||
{
|
||||
if (ft_lst_len(file_and_dirs[FILS]) == 0 &&
|
||||
ft_lst_len(file_and_dirs[DIRS]) == 1)
|
||||
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_ops && (!file_and_dirs[DIRS] && !file_and_dirs[FILS]))
|
||||
{
|
||||
s_ls->one_dir = 1;
|
||||
fill_path_lst(&file_and_dirs[DIRS], "./");
|
||||
ft_ls_dir(s_ls, operands_parse(file_and_dirs[DIRS], s_ls), NULL);
|
||||
while (i < ac - 1 && *av[i + 1] == '-')
|
||||
if (chck_flgs(av[i + 1], flgs))
|
||||
break ;
|
||||
else
|
||||
i++;
|
||||
paths = i;
|
||||
if (paths == ac - 1)
|
||||
ft_ls("./", flgs);
|
||||
else
|
||||
while (paths < ac - 1)
|
||||
ft_ls(av[paths++ + 1], flgs);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
static t_flags flgs;
|
||||
static t_col_len padding;
|
||||
static t_ft_ls s_ls;
|
||||
t_list **file_and_dirs;
|
||||
int no_error_operands;
|
||||
// DIR *dp;
|
||||
// struct dirent *ep;
|
||||
// struct stat *stat_buf;
|
||||
// char *str_tmp;
|
||||
// int blocks;
|
||||
t_flags flgs;
|
||||
|
||||
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_str_lst(file_and_dirs[FILS]);
|
||||
free_str_lst(file_and_dirs[DIRS]);
|
||||
free(file_and_dirs);
|
||||
return (0);
|
||||
parse_input(argc, argv, &flgs);
|
||||
|
||||
// dp = opendir (argv[1]);
|
||||
// blocks = 0;
|
||||
// if (dp != NULL)
|
||||
// {
|
||||
// while ((ep = readdir (dp)))
|
||||
// {
|
||||
// str_tmp = ft_strjoin("../", ep->d_name);
|
||||
// stat_buf = malloc(sizeof(struct stat));
|
||||
// stat(str_tmp, stat_buf);
|
||||
// printf("%s:\n", str_tmp);
|
||||
// printf(" nlinks: %d\n", (int)stat_buf->st_nlink);
|
||||
// printf(" st_mode: %d\n", S_ISCHR(stat_buf->st_mode));
|
||||
// printf(" MAJOR: %d\n", (int)major(stat_buf->st_rdev));
|
||||
// printf(" MINOR: %d\n", (int)minor(stat_buf->st_rdev));
|
||||
// printf(" st_uid: %s\n", getpwuid(stat_buf->st_uid)->pw_name);
|
||||
// printf(" st_gid: %s\n", getgrgid(stat_buf->st_gid)->gr_name);
|
||||
// printf(" st_size: %d\n", (int)stat_buf->st_size);
|
||||
// printf(" st_atime: %s", ctime(&stat_buf->st_atimespec.tv_sec));
|
||||
// printf(" st_blocks: %d\n\n", (int)stat_buf->st_blocks);
|
||||
// blocks += stat_buf->st_blocks;
|
||||
// free(stat_buf);
|
||||
// free(str_tmp);
|
||||
// }
|
||||
// (void) closedir (dp);
|
||||
// printf(" total blocks: %d\n", blocks);
|
||||
// }
|
||||
// else
|
||||
// printf("%s\n", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
|
87
src/output.c
87
src/output.c
|
@ -1,87 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)
|
||||
{
|
||||
char *cur_dir_semi_n;
|
||||
char *n_cur_dir;
|
||||
|
||||
n_cur_dir = NULL;
|
||||
if (!s_ls->files_print)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void one_line(t_list *lst_d_elm, t_ft_ls *s_ls)
|
||||
{
|
||||
s_ls = 0;
|
||||
ft_putstr(((t_dir_elm *)lst_d_elm->content)->elm_name);
|
||||
if (lst_d_elm->next)
|
||||
ft_putstr(" ");
|
||||
else
|
||||
ft_putstr("\n");
|
||||
}
|
||||
|
||||
void one_column(t_list *lst_d_elm, t_ft_ls *s_ls)
|
||||
{
|
||||
s_ls = 0;
|
||||
ft_putstr(((t_dir_elm *)lst_d_elm->content)->elm_name);
|
||||
ft_putstr("\n");
|
||||
}
|
||||
|
||||
void out(t_ft_ls *s_ls, t_list *cdc, void (*format_fun)(t_list *, t_ft_ls *))
|
||||
{
|
||||
t_list *lst_rnr;
|
||||
|
||||
lst_rnr = cdc;
|
||||
while (lst_rnr)
|
||||
{
|
||||
if (s_ls->flgs->a)
|
||||
format_fun(lst_rnr, s_ls);
|
||||
else if (!is_hidden(((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, t_list *curr_dir_content)
|
||||
{
|
||||
if (s_ls->flgs->l)
|
||||
{
|
||||
if (!s_ls->files_print)
|
||||
put_total(s_ls->dir_content_total / 2);
|
||||
out(s_ls, curr_dir_content, long_format);
|
||||
}
|
||||
else if (s_ls->flgs->col)
|
||||
out(s_ls, curr_dir_content, one_column);
|
||||
else
|
||||
{
|
||||
out(s_ls, curr_dir_content, one_line);
|
||||
if (s_ls->flgs->r)
|
||||
ft_putstr("\n");
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)
|
||||
{
|
||||
DIR *d_stream;
|
||||
|
||||
if (access(path, F_OK) == 0)
|
||||
if ((d_stream = opendir(path)))
|
||||
{
|
||||
closedir(d_stream);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
else
|
||||
{
|
||||
put_error(path);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
int open_type;
|
||||
int no_errors;
|
||||
|
||||
i = 0;
|
||||
no_errors = 1;
|
||||
if (ac == 1)
|
||||
return (no_errors);
|
||||
while (i < ac - 1)
|
||||
{
|
||||
if (chck_flgs(av[i + 1], flgs))
|
||||
{
|
||||
open_type = chck_opn(av[i + 1]);
|
||||
if (open_type == 0)
|
||||
fill_path_lst(&file_and_dirs[FILS], av[i + 1]);
|
||||
else if (open_type == 1)
|
||||
fill_path_lst(&file_and_dirs[DIRS], av[i + 1]);
|
||||
else if (open_type == -1)
|
||||
no_errors = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (no_errors);
|
||||
}
|
31
src/sort.c
31
src/sort.c
|
@ -1,31 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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"
|
||||
|
||||
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);
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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
101
src/stat_parse.c
|
@ -1,101 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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