commit c3b3e400a5f16e4c281950edbf73aeb7472f4dc2 Author: Jack Halford Date: Fri Sep 23 02:33:00 2016 +0200 first commit diff --git a/ls/.gitmodules b/ls/.gitmodules new file mode 100644 index 00000000..bb7d18d4 --- /dev/null +++ b/ls/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libft"] + path = libft + url = http://github.com/jzck/libft diff --git a/ls/.tags b/ls/.tags new file mode 100644 index 00000000..0815f4f2 --- /dev/null +++ b/ls/.tags @@ -0,0 +1,6 @@ +Mmain src/main.c /^int main(int ac, char **av)$/ +ft_error_dir src/lib_error.c /^void ft_error_dir(char *s)$/ +ft_error_option src/lib_error.c /^void ft_error_option(char c)$/ +ft_ls src/lib_print.c /^void ft_ls(t_list **dirs_p)$/ +ft_parse_ls src/lib_parse.c /^void ft_parse_ls(int ac, char **av, t_list **dirs,/ +ft_parse_ls_options src/lib_parse.c /^int ft_parse_ls_options(int ac, char **av, char */ diff --git a/ls/Makefile b/ls/Makefile new file mode 100644 index 00000000..fe9fbfd0 --- /dev/null +++ b/ls/Makefile @@ -0,0 +1,50 @@ +NAME = ft_ls +CC = gcc +TAGFILE = .tags + +D_SRC = src +F_SRC := $(shell ls -1 $(D_SRC) | grep "\.c$$") +DF_SRC := $(addprefix $(D_SRC)/, $(F_SRC)) + +D_OBJ = obj +F_OBJ = $(F_SRC:.c=.o) +DF_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ)) + +D_INC = includes libft/includes +O_INC = $(addprefix -I, $(D_INC)) + +W_FLAGS = -Wall -Wextra -Werror +D_FLAGS = + +MKDIR = mkdir -p +RM = /bin/rm -rf + +.PHONY: all clean fclean re tags test libft + +all: libft $(NAME) $(TAGFILE) + +test: + gcc -Iincludes main.c libftprintf.a + +$(TAGFILE): $(D_SRC)/*.c + @ctags -f $(TAGFILE) $(addprefix $(D_SRC)/, $(F_SRC)) + @echo "Making tags..." + +$(D_OBJ)/%.o: $(D_SRC)/%.c $(D_INC) + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." + +libft: + @$(MAKE) -C libft/ 2>/dev/null + +$(NAME): $(DF_OBJ) libft/libft.a + $(CC) $(O_INC) -Llibft -lft $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) + +clean: + $(RM) $(D_OBJ) + +fclean: clean + $(RM) $(NAME) + +re: fclean all diff --git a/ls/ft_ls.fr.pdf b/ls/ft_ls.fr.pdf new file mode 100644 index 00000000..1a5094c2 Binary files /dev/null and b/ls/ft_ls.fr.pdf differ diff --git a/ls/includes/ftls.h b/ls/includes/ftls.h new file mode 100644 index 00000000..bb816d1d --- /dev/null +++ b/ls/includes/ftls.h @@ -0,0 +1,15 @@ +#ifndef FTLS_H +# define FTLS_H +# define ALL_OPTS "lRart" +# include "libft.h" +# include + +int ft_parse_ls_options(int ac, char **av, char *opts); +void ft_parse_ls(int ac, char **av, t_list **dirs, char * opts); + +void ft_ls(t_list **dirs); + +void ft_error_option(char c); +void ft_error_dir(char *s); + +#endif diff --git a/ls/libft b/ls/libft new file mode 160000 index 00000000..55497a8a --- /dev/null +++ b/ls/libft @@ -0,0 +1 @@ +Subproject commit 55497a8a27b8f6ef48d629f0ed0e381a8130db3f diff --git a/ls/src/lib_error.c b/ls/src/lib_error.c new file mode 100644 index 00000000..21dc7943 --- /dev/null +++ b/ls/src/lib_error.c @@ -0,0 +1,12 @@ +#include "ftls.h" +#include + +void ft_error_option(char c) +{ + ft_printf("warning option '%c' is unsupported\n", c); +} + +void ft_error_dir(char *s) +{ + ft_printf("ls: %s: no such file or directory\n", s); +} diff --git a/ls/src/lib_parse.c b/ls/src/lib_parse.c new file mode 100644 index 00000000..f0066a42 --- /dev/null +++ b/ls/src/lib_parse.c @@ -0,0 +1,56 @@ +#include "ftls.h" + +void ft_parse_ls(int ac, char **av, t_list **dirs, char *opts) +{ + int i; + DIR *dir; + + (void)dirs; + i = ft_parse_ls_options(ac, av, opts); + ft_printf("options: %s\n", opts); + ft_strlsort(av + i, ac - i, &ft_strcmp); + if (i == ac) + ft_lstadd(dirs, ft_lstnew(opendir("."), sizeof(*dir))); + while (i < ac) + { + dir = opendir(av[i]); + if (dir) + { + ft_lstadd(dirs, ft_lstnew(dir, sizeof(*dir))); + /* ft_printf("found dir %s\n", av[i]); */ + } + else + ft_error_dir(av[i]); + i++; + } +} + +int ft_parse_ls_options(int ac, char **av, char *opts) +{ + int i; + int j; + + i = 1; + while (i < ac) + { + if (av[i][0] == '-' && av[i][1] != '\0') + { + j = 1; + while(av[i][j]) + { + if (ft_strchr(ALL_OPTS, av[i][j])) + { + if (!ft_strchr(opts, av[i][j])) + ft_strcat(opts, av[i] + j); + } + else + ft_error_option(av[i][j]); + j++; + } + } + else + break ; + i++; + } + return (i); +} diff --git a/ls/src/lib_print.c b/ls/src/lib_print.c new file mode 100644 index 00000000..83d673a1 --- /dev/null +++ b/ls/src/lib_print.c @@ -0,0 +1,16 @@ +#include "ftls.h" + +void ft_ls(t_list **dirs_p) +{ + t_list *ents; + struct dirent *dirent; + DIR *dir; + + ents = NULL; + dir = (*dirs_p)->content;; + while ((dirent = readdir(dir))) + { + ft_lstadd(&ents, ft_lstnew(dirent, sizeof(*dirent))); + } + *dirs_p = (*dirs_p)->next; +} diff --git a/ls/src/main.c b/ls/src/main.c new file mode 100644 index 00000000..f72a6b1b --- /dev/null +++ b/ls/src/main.c @@ -0,0 +1,15 @@ +#include "ftls.h" + +int main(int ac, char **av) +{ + t_list *dirs; + char *opts; + + dirs = NULL; + opts = (char *)malloc(sizeof(char) * 7); + ft_bzero(opts, 7); + ft_parse_ls(ac, av, &dirs, opts); + while (dirs) + ft_ls(&dirs); + return (0); +}