poartialy worked recursion
This commit is contained in:
parent
1a9ab90258
commit
9b4b6692ed
13 changed files with 526 additions and 206 deletions
|
@ -3,12 +3,18 @@ project(ft_ls)
|
|||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -O0 -g")
|
||||
|
||||
include_directories(inc libft/includes) # headers
|
||||
link_directories(libft) # libraries
|
||||
include_directories(inc libft/includes) # headers
|
||||
|
||||
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 libft/ft_lst_merge_sort.c) # sources
|
||||
src/ft_ls.c
|
||||
src/parse_input.c
|
||||
src/flags.c
|
||||
src/sort.c
|
||||
src/output.c
|
||||
src/error.c libft/ft_strcmp_lex.c) # sources
|
||||
|
||||
add_executable(ft_ls ${SOURCE_FILES}) # compilation
|
||||
|
||||
|
|
42
inc/ft_ls.h
42
inc/ft_ls.h
|
@ -25,28 +25,56 @@
|
|||
# include <sys/types.h>
|
||||
# include <errno.h>
|
||||
|
||||
# define FILES 0
|
||||
# define DIRS 1
|
||||
|
||||
typedef struct s_flags
|
||||
{
|
||||
int l;
|
||||
int a;
|
||||
int R;
|
||||
int r;
|
||||
int t;
|
||||
int col;
|
||||
int abn;
|
||||
} t_flags;
|
||||
|
||||
typedef struct s_dir_entity
|
||||
typedef struct s_dir_elm
|
||||
{
|
||||
char *name;
|
||||
char *a_time;
|
||||
char *elm_name;
|
||||
char *parent_dir;
|
||||
char *a_time_str;
|
||||
int size;
|
||||
char *g_name;
|
||||
char *u_name;
|
||||
int links;
|
||||
char *attr;
|
||||
char *attr_str;
|
||||
int link_fd;
|
||||
struct dirent *dirent;
|
||||
struct stat *stat_buf;
|
||||
struct stat *stat_buf_struc;
|
||||
t_flags *flags;
|
||||
} t_dir_entity;
|
||||
} t_dir_elm;
|
||||
|
||||
void ft_ls(
|
||||
t_list *filenames,
|
||||
t_list *dir_paths,
|
||||
t_flags *flgs,
|
||||
char *par_dir);
|
||||
|
||||
int parse_input(
|
||||
int ac,
|
||||
char **av,
|
||||
t_list **str_paths,
|
||||
t_flags *flgs);
|
||||
|
||||
void init_dir_con_lst(
|
||||
t_list **dir_content_lst, t_list *filenames,
|
||||
t_flags *flgs, char *par_dir);
|
||||
|
||||
void fill_path_lst(t_list **path_lst, char *path);
|
||||
void sort_elms(t_list **del, t_flags *flgs);
|
||||
void sort_dirs(t_list **dirs, t_flags *flgs);
|
||||
void output(t_list *del, char *current_dir, t_flags *flgs);
|
||||
int chck_flgs(char *flg, t_flags *flgs);
|
||||
void put_error(char *arg);
|
||||
|
||||
#endif
|
||||
|
|
140
libft/Makefile
140
libft/Makefile
|
@ -11,75 +11,77 @@
|
|||
# **************************************************************************** #
|
||||
|
||||
NAME = libft.a
|
||||
SRC = ft_memset.c \
|
||||
ft_bzero.c \
|
||||
ft_memcpy.c \
|
||||
ft_memccpy.c \
|
||||
ft_memmove.c \
|
||||
ft_memchr.c \
|
||||
ft_memcmp.c \
|
||||
ft_strlen.c \
|
||||
ft_strdup.c \
|
||||
ft_strcpy.c \
|
||||
ft_strncpy.c \
|
||||
ft_strcat.c \
|
||||
ft_strncat.c \
|
||||
ft_strlcat.c \
|
||||
ft_strchr.c \
|
||||
ft_strrchr.c \
|
||||
ft_strstr.c \
|
||||
ft_strnstr.c \
|
||||
ft_strcmp.c \
|
||||
ft_strncmp.c \
|
||||
ft_atoi.c \
|
||||
ft_isalpha.c \
|
||||
ft_isdigit.c \
|
||||
ft_isalnum.c \
|
||||
ft_isascii.c \
|
||||
ft_isprint.c \
|
||||
ft_toupper.c \
|
||||
ft_tolower.c \
|
||||
\
|
||||
ft_memalloc.c \
|
||||
ft_memdel.c \
|
||||
ft_strnew.c \
|
||||
ft_strdel.c \
|
||||
ft_strclr.c \
|
||||
ft_striter.c \
|
||||
ft_striteri.c \
|
||||
ft_strmap.c \
|
||||
ft_strmap.c \
|
||||
ft_strmapi.c \
|
||||
ft_strequ.c \
|
||||
ft_strnequ.c \
|
||||
ft_strsub.c \
|
||||
ft_strjoin.c \
|
||||
ft_strtrim.c \
|
||||
ft_strsplit.c \
|
||||
ft_itoa.c \
|
||||
ft_putchar.c \
|
||||
ft_putstr.c \
|
||||
ft_putendl.c \
|
||||
ft_putnbr.c \
|
||||
ft_putchar_fd.c \
|
||||
ft_putstr_fd.c \
|
||||
ft_putendl_fd.c \
|
||||
ft_putnbr_fd.c \
|
||||
\
|
||||
ft_lstnew.c \
|
||||
ft_lstdelone.c \
|
||||
ft_lstdel.c \
|
||||
ft_lstadd.c \
|
||||
ft_lstiter.c \
|
||||
ft_lstmap.c \
|
||||
ft_lststrsplit.c \
|
||||
ft_lstfind.c \
|
||||
ft_lst_at.c \
|
||||
ft_lstadd_back.c \
|
||||
ft_lst_len.c \
|
||||
\
|
||||
ft_realloc.c \
|
||||
ft_read_file.c \
|
||||
SRC = ft_memset.c \
|
||||
ft_bzero.c \
|
||||
ft_memcpy.c \
|
||||
ft_memccpy.c \
|
||||
ft_memmove.c \
|
||||
ft_memchr.c \
|
||||
ft_memcmp.c \
|
||||
ft_strlen.c \
|
||||
ft_strdup.c \
|
||||
ft_strcpy.c \
|
||||
ft_strncpy.c \
|
||||
ft_strcat.c \
|
||||
ft_strncat.c \
|
||||
ft_strlcat.c \
|
||||
ft_strchr.c \
|
||||
ft_strrchr.c \
|
||||
ft_strstr.c \
|
||||
ft_strnstr.c \
|
||||
ft_strcmp.c \
|
||||
ft_strcmp_lex.c \
|
||||
ft_strncmp.c \
|
||||
ft_atoi.c \
|
||||
ft_isalpha.c \
|
||||
ft_isdigit.c \
|
||||
ft_isalnum.c \
|
||||
ft_isascii.c \
|
||||
ft_isprint.c \
|
||||
ft_toupper.c \
|
||||
ft_tolower.c \
|
||||
\
|
||||
ft_memalloc.c \
|
||||
ft_memdel.c \
|
||||
ft_strnew.c \
|
||||
ft_strdel.c \
|
||||
ft_strclr.c \
|
||||
ft_striter.c \
|
||||
ft_striteri.c \
|
||||
ft_strmap.c \
|
||||
ft_strmap.c \
|
||||
ft_strmapi.c \
|
||||
ft_strequ.c \
|
||||
ft_strnequ.c \
|
||||
ft_strsub.c \
|
||||
ft_strjoin.c \
|
||||
ft_strtrim.c \
|
||||
ft_strsplit.c \
|
||||
ft_itoa.c \
|
||||
ft_putchar.c \
|
||||
ft_putstr.c \
|
||||
ft_putendl.c \
|
||||
ft_putnbr.c \
|
||||
ft_putchar_fd.c \
|
||||
ft_putstr_fd.c \
|
||||
ft_putendl_fd.c \
|
||||
ft_putnbr_fd.c \
|
||||
\
|
||||
ft_lstnew.c \
|
||||
ft_lstdelone.c \
|
||||
ft_lstdel.c \
|
||||
ft_lstadd.c \
|
||||
ft_lstiter.c \
|
||||
ft_lstmap.c \
|
||||
ft_lststrsplit.c \
|
||||
ft_lstfind.c \
|
||||
ft_lst_at.c \
|
||||
ft_lstadd_back.c \
|
||||
ft_lst_len.c \
|
||||
ft_lst_merge_sort.c \
|
||||
\
|
||||
ft_realloc.c \
|
||||
ft_read_file.c \
|
||||
get_next_line.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "libft.h"
|
||||
|
||||
t_list *SortedMerge(t_list *a, t_list *b)
|
||||
static t_list *merge(t_list *a, t_list *b, int (*cmp)())
|
||||
{
|
||||
t_list *result;
|
||||
|
||||
|
@ -8,24 +8,23 @@ t_list *SortedMerge(t_list *a, t_list *b)
|
|||
|
||||
if (a == NULL)
|
||||
return(b);
|
||||
else if (b==NULL)
|
||||
else if (b == NULL)
|
||||
return(a);
|
||||
|
||||
/* Pick either a or b, and recur */
|
||||
if (a->data <= b->data)
|
||||
if (cmp(a->content, b->content))
|
||||
{
|
||||
result = a;
|
||||
result->next = SortedMerge(a->next, b);
|
||||
result->next = merge(a->next, b, cmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = b;
|
||||
result->next = SortedMerge(a, b->next);
|
||||
result->next = merge(a, b->next, cmp);
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
|
||||
void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
|
||||
static void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
|
||||
{
|
||||
t_list *fast;
|
||||
t_list *slow;
|
||||
|
@ -48,13 +47,12 @@ void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
|
|||
fast = fast->next;
|
||||
}
|
||||
}
|
||||
|
||||
*front_ptr = source;
|
||||
*back_ptr = slow->next;
|
||||
slow->next = NULL;
|
||||
}
|
||||
}
|
||||
void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)())
|
||||
void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)())
|
||||
{
|
||||
t_list *head;
|
||||
t_list *a;
|
||||
|
@ -63,13 +61,8 @@ void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)())
|
|||
head = *head_ptr;
|
||||
if ((head == NULL) || (head->next == NULL))
|
||||
return;
|
||||
|
||||
split(head, &a, &b);
|
||||
|
||||
/* Recursively sort the sublists */
|
||||
ft_lst_merge_sort(&a, cmp);
|
||||
ft_lst_merge_sort(&b, cmp);
|
||||
|
||||
/* answer = merge the two sorted lists together */
|
||||
*head_ptr = SortedMerge(a, b);
|
||||
*head_ptr = merge(a, b, cmp);
|
||||
}
|
||||
|
|
32
libft/ft_strcmp_lex.c
Normal file
32
libft/ft_strcmp_lex.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_tolower.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/01 15:58:52 by gtertysh #+# #+# */
|
||||
/* Updated: 2016/12/01 15:59:14 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);
|
||||
}
|
|
@ -57,6 +57,7 @@ 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);
|
||||
|
@ -104,6 +105,8 @@ 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_realloc(void *old, unsigned int new_size,
|
||||
unsigned int old_size);
|
||||
|
||||
|
|
25
src/error.c
Normal file
25
src/error.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
//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);
|
||||
//}
|
||||
|
||||
void put_error(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);
|
||||
}
|
46
src/flags.c
46
src/flags.c
|
@ -1,22 +1,38 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void set_flags_to_zero(t_flags *flgs)
|
||||
int chck_long_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
flgs->a = 0;
|
||||
flgs->l = 0;
|
||||
flgs->R = 0;
|
||||
flgs->t = 0;
|
||||
flgs->col = 0;
|
||||
if (*(flg + 1) && (flg += 1))
|
||||
{
|
||||
if (flgs->abn == 0) flgs->abn = (ft_strcmp(flg, "all-by-name") == 0)
|
||||
? 1 : 0;
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
void chck_short_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
if (flgs->l == 0) flgs->l = (ft_strcmp(flg, "l") == 0) ? 1 : 0;
|
||||
if (flgs->a == 0) flgs->a = (ft_strcmp(flg, "a") == 0) ? 1 : 0;
|
||||
if (flgs->R == 0) flgs->R = (ft_strcmp(flg, "R") == 0) ? 1 : 0;
|
||||
if (flgs->t == 0) flgs->t = (ft_strcmp(flg, "t") == 0) ? 1 : 0;
|
||||
if (flgs->col == 0) flgs->col = (ft_strcmp(flg, "1") == 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
int chck_flgs(char *flg, t_flags *flgs)
|
||||
{
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
154
src/ft_ls.c
154
src/ft_ls.c
|
@ -1,47 +1,141 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void put_error(char *arg)
|
||||
char *get_full_path(char *path, char *dir_ent_name)
|
||||
{
|
||||
char *final_str;
|
||||
char *arg_colon_errno;
|
||||
char *colon_errno;
|
||||
char *tmp;
|
||||
char *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);
|
||||
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 init_dir_ent_list(DIR *dp, t_list **del, t_flags *flgs)
|
||||
void init_dir_con_lst(t_list **dir_content_lst, t_list *filenames,
|
||||
t_flags *flgs, char *par_dir)
|
||||
{
|
||||
t_dir_entity de;
|
||||
t_dir_elm de;
|
||||
char *tmp_path;
|
||||
|
||||
|
||||
while ((de.dirent = readdir(dp)) && (de.flags = flgs))
|
||||
ft_lstadd(del, ft_lstnew(&de, sizeof(t_dir_entity)));
|
||||
while (filenames)
|
||||
{
|
||||
de.flags = flgs;
|
||||
de.stat_buf_struc = malloc(sizeof(struct stat));
|
||||
de.elm_name = (char *)filenames->content;
|
||||
tmp_path = get_full_path(par_dir, de.elm_name);
|
||||
stat(tmp_path, de.stat_buf_struc);
|
||||
ft_lstadd(dir_content_lst, ft_lstnew(&de, sizeof(t_dir_elm)));
|
||||
free(tmp_path);
|
||||
filenames = filenames->next;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_ls(char *path, t_flags *flgs)
|
||||
//void init_dir_ent_lst(t_list **fil_ent_lst, t_list *dirs, t_flags *flgs)
|
||||
//{
|
||||
//
|
||||
// t_dir_elm de;
|
||||
//
|
||||
// while (dirs)
|
||||
// {
|
||||
// de.flags = flgs;
|
||||
// de.stat_buf_struc = malloc(sizeof(struct stat));
|
||||
// de.elm_name = (char *)dirs->content;
|
||||
// de.full_path =(char *)dirs->content;
|
||||
// stat(de.full_path, de.stat_buf_struc);
|
||||
// ft_lstadd(fil_ent_lst, ft_lstnew(&de, sizeof(t_dir_elm)));
|
||||
// dirs = dirs->next;
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void init_dir_ent_list(t_list **del, t_flags *flgs, char *path)
|
||||
//{
|
||||
// DIR *dp;
|
||||
// t_dir_elm de;
|
||||
// struct dirent *dir_ent;
|
||||
//
|
||||
// dp = opendir(path);
|
||||
// while ((dir_ent = readdir(dp)))
|
||||
// {
|
||||
// de.flags = flgs;
|
||||
// de.stat_buf_struc = malloc(sizeof(struct stat));
|
||||
// de.elm_name = ft_strdup(dir_ent->d_name);
|
||||
// de.full_path = get_full_path(path, dir_ent->d_name);
|
||||
// stat(de.full_path, de.stat_buf_struc);
|
||||
// ft_lstadd(del, ft_lstnew(&de, sizeof(t_dir_elm)));
|
||||
// }
|
||||
// closedir(dp);
|
||||
//}
|
||||
|
||||
t_list *extract_files_from_dir(char *dir, char *par_dir)
|
||||
{
|
||||
DIR *dp;
|
||||
// int blocks;
|
||||
// char *tmp_path;
|
||||
t_list *dir_ent_list;
|
||||
struct dirent *dir_ent;
|
||||
t_list *dir_files;
|
||||
char *tmp_path;
|
||||
|
||||
// tmp_path = 0;
|
||||
// blocks = 0;
|
||||
dir_ent_list = NULL;
|
||||
if (!(dp = opendir(path)) || open(path, O_RDONLY) == -1)
|
||||
put_error(path);
|
||||
else
|
||||
tmp_path = get_full_path(par_dir, dir);
|
||||
dp = opendir(tmp_path);
|
||||
dir_files = NULL;
|
||||
while ((dir_ent = readdir(dp)))
|
||||
fill_path_lst(&dir_files, dir_ent->d_name);
|
||||
closedir(dp);
|
||||
free(tmp_path);
|
||||
return (dir_files);
|
||||
}
|
||||
|
||||
t_list *extract_dirs_from_filenames(t_list *filenames, char *par_d)
|
||||
{
|
||||
t_list *dirs;
|
||||
char *tmp_full_path;
|
||||
dirs = NULL;
|
||||
|
||||
while (filenames)
|
||||
{
|
||||
init_dir_ent_list(dp, &dir_ent_list, flgs);
|
||||
sort(dir_ent_list, flgs);
|
||||
output(dir_ent_list);
|
||||
tmp_full_path = get_full_path(par_d, filenames->content);
|
||||
if (opendir(tmp_full_path) && (ft_strcmp(filenames->content, ".") != 0) && (ft_strcmp(filenames->content, "..") != 0))
|
||||
fill_path_lst(&dirs, filenames->content);
|
||||
free(tmp_full_path);
|
||||
filenames = filenames->next;
|
||||
}
|
||||
return (dirs);
|
||||
}
|
||||
|
||||
void ft_ls(t_list *filenames, t_list *dir_paths, t_flags *flgs,
|
||||
char *par_dir)
|
||||
{
|
||||
t_list *dir_content_lst;
|
||||
|
||||
dir_content_lst = NULL;
|
||||
if (filenames)
|
||||
{
|
||||
init_dir_con_lst(&dir_content_lst, filenames, flgs, par_dir);
|
||||
sort_elms(&dir_content_lst, flgs);
|
||||
output(dir_content_lst, par_dir, flgs);
|
||||
if (flgs->R)
|
||||
ft_ls(NULL, extract_dirs_from_filenames(filenames, par_dir), flgs, par_dir);
|
||||
}
|
||||
if (dir_paths)
|
||||
{
|
||||
sort_dirs(&dir_paths, flgs);
|
||||
while (dir_paths)
|
||||
{
|
||||
ft_ls(extract_files_from_dir(dir_paths->content, par_dir), NULL, flgs, dir_paths->content);
|
||||
dir_paths = dir_paths->next;
|
||||
}
|
||||
}
|
||||
}
|
51
src/main.c
51
src/main.c
|
@ -14,42 +14,21 @@
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// DIR *dp;
|
||||
// struct dirent *ep;
|
||||
// struct stat *stat_buf;
|
||||
// char *str_tmp;
|
||||
// int blocks;
|
||||
t_flags flgs;
|
||||
static t_flags flgs;
|
||||
t_list *str_paths[2];
|
||||
int no_errors;
|
||||
|
||||
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));
|
||||
str_paths[FILES] = NULL;
|
||||
str_paths[DIRS] = NULL;
|
||||
no_errors = parse_input(argc, argv, str_paths, &flgs);
|
||||
if (str_paths[FILES] || str_paths[DIRS])
|
||||
{
|
||||
ft_ls(str_paths[FILES], str_paths[DIRS], &flgs, NULL);
|
||||
}
|
||||
else if (no_errors)
|
||||
{
|
||||
fill_path_lst(&str_paths[DIRS], "./");
|
||||
ft_ls(NULL, str_paths[DIRS], &flgs, NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
79
src/output.c
79
src/output.c
|
@ -1,18 +1,77 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void column_out(t_list *entity)
|
||||
void column_or_line(t_list *entity)
|
||||
{
|
||||
t_dir_entity *d_ent;
|
||||
t_dir_elm *d_elm;
|
||||
|
||||
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);
|
||||
d_elm = (t_dir_elm *)entity->content;
|
||||
ft_putstr(d_elm->elm_name);
|
||||
if (entity->next)
|
||||
{
|
||||
if (d_elm->flags->col)
|
||||
ft_putstr("\n");
|
||||
else
|
||||
ft_putstr(" ");
|
||||
}
|
||||
}
|
||||
|
||||
void output(t_list *del)
|
||||
int out(t_list *entity)
|
||||
{
|
||||
ft_lstiter(del, column_out);
|
||||
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 (*d_elm->elm_name != '.')
|
||||
{
|
||||
column_or_line(entity);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void print_full_path(char *path)
|
||||
{
|
||||
char *cur_dir_semi_n;
|
||||
|
||||
cur_dir_semi_n = ft_strjoin(path, ":\n");
|
||||
ft_putstr(cur_dir_semi_n);
|
||||
free(cur_dir_semi_n);
|
||||
}
|
||||
|
||||
void put_current_dir(char *cur_dir, t_flags *flgs)
|
||||
{
|
||||
if (flgs->a)
|
||||
print_full_path(cur_dir);
|
||||
else
|
||||
{
|
||||
if (*cur_dir != '.')
|
||||
print_full_path(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 output(t_list *del, char *current_dir, t_flags *flgs)
|
||||
{
|
||||
if (current_dir)
|
||||
put_current_dir(current_dir, flgs);
|
||||
if (ft_lstiter_ret(del, out))
|
||||
ft_putstr("\n");
|
||||
}
|
|
@ -1,26 +1,52 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
void parse_input(int ac, char **av, t_flags *flgs)
|
||||
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));
|
||||
}
|
||||
|
||||
int parse_input(int ac, char **av, t_list **str_paths, t_flags *flgs)
|
||||
{
|
||||
int i;
|
||||
int paths;
|
||||
int open_type;
|
||||
int no_errors;
|
||||
|
||||
i = 0;
|
||||
set_flags_to_zero(flgs);
|
||||
no_errors = 1;
|
||||
if (ac == 1)
|
||||
ft_ls(".", flgs);
|
||||
else if (ac > 1)
|
||||
return (no_errors);
|
||||
while (i < ac - 1)
|
||||
{
|
||||
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);
|
||||
if (chck_flgs(av[i + 1], flgs))
|
||||
{
|
||||
open_type = chck_opn(av[i + 1]);
|
||||
if (open_type == 0)
|
||||
fill_path_lst(&str_paths[FILES], av[i + 1]);
|
||||
else if (open_type == 1)
|
||||
fill_path_lst(&str_paths[DIRS], av[i + 1]);
|
||||
else if (open_type == -1)
|
||||
no_errors = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (no_errors);
|
||||
}
|
69
src/sort.c
69
src/sort.c
|
@ -1,14 +1,71 @@
|
|||
#include "ft_ls.h"
|
||||
|
||||
|
||||
|
||||
void sort_by(t_list **del, int (*f)())
|
||||
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 (ft_strcmp(a->elm_name, b->elm_name) <= 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int all_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_size(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (a->stat_buf_struc->st_size <= b->stat_buf_struc->st_size)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_m_time(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (a->stat_buf_struc->st_mtim.tv_sec >=
|
||||
b->stat_buf_struc->st_mtim.tv_sec)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int by_type(t_dir_elm *a, t_dir_elm *b)
|
||||
{
|
||||
if (S_ISREG(b->stat_buf_struc->st_mode) && a)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int path_by_name(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(t_list *del, t_flags *flgs)
|
||||
void sort_elms(t_list **del, t_flags *flgs)
|
||||
{
|
||||
if (!flgs->t)
|
||||
sort_by(&del, lex);
|
||||
if (flgs->t)
|
||||
ft_lst_merge_sort(del, by_m_time);
|
||||
if (flgs->abn)
|
||||
ft_lst_merge_sort(del, all_lex);
|
||||
else
|
||||
by_type_and_name(del);
|
||||
}
|
||||
|
||||
void sort_dirs(t_list **dirs, t_flags *flgs)
|
||||
{
|
||||
flgs = flgs;
|
||||
ft_lst_merge_sort(dirs, path_by_name);
|
||||
}
|
Loading…
Add table
Reference in a new issue