├── .gitignore ├── Makefile ├── README.md ├── author ├── ft_ls.en.pdf ├── inc └── ft_ls.h ├── libft ├── .gitignore ├── Makefile ├── ft_atoi.c ├── ft_bzero.c ├── ft_capitalize.c ├── ft_copyuntil.c ├── ft_countwords.c ├── ft_intlen.c ├── ft_isalnum.c ├── ft_isalpha.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_islower.c ├── ft_isprint.c ├── ft_isupper.c ├── ft_itoa.c ├── ft_lst_reverse.c ├── ft_lstadd.c ├── ft_lstaddback.c ├── ft_lstdel.c ├── ft_lstdelone.c ├── ft_lstiter.c ├── ft_lstmap.c ├── ft_lstnew.c ├── ft_memalloc.c ├── ft_memccpy.c ├── ft_memchr.c ├── ft_memcmp.c ├── ft_memcpy.c ├── ft_memdel.c ├── ft_memmove.c ├── ft_memset.c ├── ft_pathjoin.c ├── ft_putchar.c ├── ft_putchar_fd.c ├── ft_putendl.c ├── ft_putendl_fd.c ├── ft_putnbr.c ├── ft_putnbr_fd.c ├── ft_putstr.c ├── ft_putstr_fd.c ├── ft_realloc.c ├── ft_strcat.c ├── ft_strchr.c ├── ft_strclr.c ├── ft_strcmp.c ├── ft_strcpy.c ├── ft_strdel.c ├── ft_strdup.c ├── ft_strendswith.c ├── ft_strequ.c ├── ft_striter.c ├── ft_striteri.c ├── ft_strjoin.c ├── ft_strjoinch.c ├── ft_strlcat.c ├── ft_strlen.c ├── ft_strmap.c ├── ft_strmapi.c ├── ft_strncat.c ├── ft_strnchr.c ├── ft_strncmp.c ├── ft_strncpy.c ├── ft_strndup.c ├── ft_strnequ.c ├── ft_strnew.c ├── ft_strnstr.c ├── ft_strrchr.c ├── ft_strsplit.c ├── ft_strstartswith.c ├── ft_strstr.c ├── ft_strsub.c ├── ft_strtrim.c ├── ft_tolower.c ├── ft_toupper.c └── includes │ └── libft.h └── src ├── dir_handlers.c ├── dir_handlers_2.c ├── display_handlers.c ├── error_handlers.c ├── file_handlers.c ├── file_handlers_2.c ├── file_handlers_3.c ├── flag_handlers.c ├── format_handlers.c ├── help_handlers.c ├── main.c ├── memory_handlers.c ├── print_handlers.c ├── subdir_handlers.c └── utils.c /.gitignore: -------------------------------------------------------------------------------- 1 | ft_ls 2 | a.out 3 | *.o 4 | ft_ls.dSYM/ 5 | */*.a 6 | test* 7 | .nfs.* 8 | .*DS_Store 9 | build 10 | *.json -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jrameau +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2016/12/13 11:43:23 by jrameau #+# #+# # 9 | # Updated: 2017/03/27 12:44:56 by jrameau ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Project file 14 | NAME = ft_ls 15 | 16 | # Project builds and dirs 17 | SRCDIR = ./src/ 18 | SRCNAMES = $(shell ls $(SRCDIR) | grep -E ".+\.c") 19 | SRC = $(addprefix $(SRCDIR), $(SRCNAMES)) 20 | INC = ./inc/ 21 | BUILDDIR = ./build/ 22 | BUILDOBJS = $(addprefix $(BUILDDIR), $(SRCNAMES:.c=.o)) 23 | 24 | # Libft builds and dirs 25 | LIBDIR = ./libft/ 26 | LIBFT = ./libft/libft.a 27 | LIBINC = ./libft/includes/ 28 | 29 | # Optimization and Compiler flags and commands 30 | CC = gcc 31 | OPFLAGS = -O3 -funroll-loops 32 | CFLAGS = -Wall -Werror -Wextra 33 | 34 | # Debugging flags 35 | DEBUG = -g 36 | 37 | # Main rule 38 | all: $(BUILDDIR) $(LIBFT) $(NAME) 39 | 40 | # Object dir rule 41 | $(BUILDDIR): 42 | mkdir -p $(BUILDDIR) 43 | 44 | # Objects rule 45 | $(BUILDDIR)%.o:$(SRCDIR)%.c 46 | $(CC) $(DEBUG) $(CFLAGS) -I$(LIBINC) -I$(INC) -o $@ -c $< 47 | 48 | # Project file rule 49 | $(NAME): $(BUILDOBJS) 50 | $(CC) $(OPFLAGS) $(CFLAGS) -o $(NAME) $(BUILDOBJS) $(LIBFT) 51 | 52 | # Libft rule 53 | $(LIBFT): 54 | make -C $(LIBDIR) 55 | 56 | # Cleaning up the build files 57 | clean: 58 | rm -rf $(BUILDDIR) 59 | make -C $(LIBDIR) clean 60 | 61 | # Getting rid of the project file 62 | fclean: clean 63 | rm -rf $(NAME) 64 | make -C $(LIBDIR) fclean 65 | 66 | # Do both of the above 67 | re: fclean all 68 | 69 | # Just in case those files exist in the root dir 70 | .PHONY: all fclean clean re 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ft_ls - @42Born2Code 2 | 3 | My own implementation of the famous Unix command: ls, using the C programming language. 4 | 5 | ### Requirements 6 | To be able to build and run this program you'll need a macbook, because I didn't make it portable for other systems. Sorry :(. As for software Requirements, you'll need: 7 | - GNU make 8 | - GCC 9 | 10 | No specific versions, just update them to the latest version if you still can't build the program. 11 | 12 | ### Building the program 13 | 14 | 1. Download/Clone the source code 15 | 2. `cd` into the root directory 16 | 3. Run `make` 17 | 18 | ### Running the program 19 | 20 | It's pretty simple, instead of calling `ls`, call `./ft_ls` from the root directory of the source code after building it. 21 | 22 | #### Supported flags 23 | 24 | - Long listing display: -l 25 | - Display recursively: -R 26 | - Display all files: -a 27 | - Reverse sort: -r 28 | - Sort by modification date: -t 29 | - Display user ID and group ID: -n 30 | - Suppress owner: -g 31 | - Display one entry per line: -1 32 | - Column display: -C (Set by default) 33 | - Sort by creation date: -U 34 | - Sort by last access date: -a 35 | - Sort by last status change date: -c 36 | - Show every entry except for current and previous directories: -A 37 | - Sort by file size: -S 38 | - Colorized output: -G (Only works in long listing display) 39 | 40 | ### Notes 41 | 42 | - You can find the project instructions by [clicking here][1] 43 | - The `master` branch has the original code, the `norme` branch has the normed version of it. 44 | - No need to mention the odd spacing in files, I'm using a tab size of 4. 45 | - It's not fast and there are a lot of memory leaks, I'll come back and fix those later. 46 | - I know comments would have been super useful, this is kind of a big code base, but I'm trying to go fast in my school program, I'll make sure to add them later. 47 | - There are probably better ways to implement it, but we are limited by a set of functions at my school (just to make it harder and give us a deeper understanding of what's happening in the back), please, review the [project instructions][1] before you explain how `X` or `Y` would have been a better way to do it ;) 48 | 49 | ## Sponsors 50 | 51 | Sponsor 52 | 53 | Enjoy! 54 | 55 | [1]: https://github.com/R4meau/ft_ls/blob/master/ft_ls.en.pdf 56 | -------------------------------------------------------------------------------- /author: -------------------------------------------------------------------------------- 1 | jrameau 2 | -------------------------------------------------------------------------------- /ft_ls.en.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_ls/d2415198a870ea15711d2ab37d27ca24e6a8651e/ft_ls.en.pdf -------------------------------------------------------------------------------- /inc/ft_ls.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_ls.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/24 15:32:06 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/16 01:33:27 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_LS_H 14 | #define FT_LS_H 15 | #include "libft.h" 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #define MEMCHECK(x) if (!x) exit(2) 33 | 34 | #define DIRS_MEM 1 35 | #define ERROR_MEM 2 36 | 37 | #define FLAG_ERR 1 38 | #define FILE_ACCESS_ERR 2 39 | #define NONEXISTENT_ERR 4 40 | 41 | #define IS_NONEXISTENT 1 42 | #define IS_NOTDIR 2 43 | #define IS_DIR 4 44 | #define IS_LINK 8 45 | #define IS_UNREADABLE 16 46 | 47 | #define ISUSR 1 48 | #define ISGRP 2 49 | #define ISOTH 4 50 | 51 | #define ANSI_COLOR_RED "\x1b[31m" 52 | #define ANSI_COLOR_GREEN "\x1b[32m" 53 | #define ANSI_COLOR_BOLD_GREEN "\x1b[32;1m" 54 | #define ANSI_COLOR_YELLOW "\x1b[33m" 55 | #define ANSI_COLOR_BLUE "\x1b[34m" 56 | #define ANSI_COLOR_MAGENTA "\x1b[35m" 57 | #define ANSI_COLOR_BOLD_CYAN "\x1b[96;1m" 58 | #define ANSI_COLOR_RESET "\x1b[0m" 59 | 60 | #define IS_NONPRINTABLE(c) (c <= ' ' && c >= '~') 61 | 62 | #define INIT_FORMAT 1 63 | #define UPDATE_FORMAT 2 64 | #define IDLE_FORMAT 4 65 | 66 | typedef struct s_format { 67 | int link; 68 | int owner; 69 | int group; 70 | int fileSize; 71 | int date_month; 72 | int date_day; 73 | int date_hour; 74 | int date_minute; 75 | int date_year; 76 | int user_id; 77 | int group_id; 78 | int major; 79 | int minor; 80 | } t_format; 81 | 82 | typedef enum e_flags { 83 | LONG_LISTING_FLAG = 1, // -l 84 | RECURSIVE_FLAG = 2, // -R 85 | ALL_FLAG = 4, // -a 86 | REVERSE_FLAG = 8, // -r 87 | MODIFICATION_DATE_SORT = 16, // -t 88 | DISPLAY_UID_AND_GID = 32, // -n 89 | SUPRESS_OWNER = 64, // -g 90 | ONE_ENTRY_PER_LINE = 128, // -1 91 | COLUMN_DISPLAY = 256, // -C 92 | CREATION_DATE_SORT = 512, // -U 93 | LAST_ACCESS_DATE_SORT = 1024, // -a 94 | LAST_STATUS_CHANGE_SORT = 2048, // -c 95 | HIDE_CURR_AND_PREV_DIRS = 4096, // -A 96 | FILE_SIZE_SORT = 8192, // -S 97 | COLORED_OUTPUT = 16384 // -G 98 | } t_flags; 99 | 100 | typedef struct s_date { 101 | char *month; 102 | char *day; 103 | char *hour; 104 | char *minute; 105 | char *year; 106 | unsigned long long mtv_sec; 107 | unsigned long long mtv_nsec; 108 | unsigned long long atv_sec; 109 | unsigned long long atv_nsec; 110 | unsigned long long ctv_sec; 111 | unsigned long long ctv_nsec; 112 | unsigned long long birthtv_sec; 113 | unsigned long long birthtv_nsec; 114 | } t_date; 115 | 116 | typedef struct s_files { 117 | char *modes; 118 | long link; 119 | char *owner; 120 | unsigned int user_id; 121 | char *group; 122 | unsigned int group_id; 123 | long size; 124 | t_date date; 125 | char *name; 126 | int is_dir_info; 127 | long major; 128 | long minor; 129 | int is_chr_or_blk; 130 | int is_link; 131 | char *linked_to; 132 | int has_nonprintable_chars; 133 | char *display_name; 134 | int status; 135 | struct s_files *next; 136 | struct stat f; // Since I'm passing this I don't need to do stuffs like is_link or is_chr_or_blk 137 | } t_files; 138 | 139 | typedef struct s_dirs { 140 | char *name; 141 | int status; 142 | t_files *files; 143 | t_files *self; 144 | t_format format; 145 | int is_default; 146 | int is_unreadable; 147 | int total_blocks; 148 | struct s_dirs *sub_dirs; 149 | struct s_dirs *next; 150 | int file_count; 151 | int max_file_len; 152 | t_date date; 153 | int is_subdir; 154 | char *display_name; 155 | int has_chr_or_blk; 156 | int has_valid_files; 157 | } t_dirs; 158 | 159 | typedef union u_etarget { 160 | char flag; 161 | char *file; 162 | } t_etarget; 163 | 164 | 165 | typedef union u_entries { 166 | t_files *files; 167 | char **file_names; 168 | } t_entries; 169 | 170 | void help_handler(void); 171 | t_dirs *dir_handler(char **args, t_flags flags); 172 | int flag_handler(char **args, t_flags *flags); 173 | void error_handler(int err, t_etarget target); 174 | void display_handler(t_dirs *head, t_dirs *dirs, t_flags flags, int target); 175 | t_files *file_handler(t_dirs *dirs, t_flags flags); 176 | void set_dir(char *path, t_dirs **dirs, char *subdir_name, t_flags flags); 177 | void add_file(t_files **curr_file, t_dirs **dirs, t_flags flags, int format_option); 178 | void add_dir(t_dirs **dirs, t_dirs *new); 179 | void format_handler(t_format *format, t_files *file, int format_option); 180 | int is_last_dir(t_dirs *dirs); 181 | t_dirs *subdir_handler(t_dirs *next, t_dirs **sub_dirs, t_flags flags); 182 | void memory_handler(void *mem_target, int target); 183 | int is_last_nondir(t_dirs *dirs); 184 | int is_only_dir(t_dirs *head); 185 | void file_sort(t_files **files, t_flags flags); 186 | int has_dirs(t_dirs *dirs); 187 | void dir_sort(t_dirs **dirs, t_flags flags); 188 | void reverse_files(t_files **files); 189 | void reverse_dirs(t_dirs **dirs); 190 | void ft_display(t_dirs *dirs, t_flags flags); 191 | char get_file_entry_type(int mode); 192 | char third_permission_mode_handler(int mode, int userType); 193 | char **argument_handler(int ac, char **av); 194 | t_format get_nondir_format(t_dirs **dirs, t_flags flags); 195 | void print_handler(int fd, char *str, int format, char *target); 196 | void lprint_handler(int fd, char *str, int format, char *target); 197 | #endif 198 | -------------------------------------------------------------------------------- /libft/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_ls/d2415198a870ea15711d2ab37d27ca24e6a8651e/libft/.gitignore -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jrameau +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2016/09/21 14:58:27 by jrameau #+# #+# # 9 | # Updated: 2016/09/25 19:32:38 by jrameau ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | CFLAGS = -Wall -Werror -Wextra -Iincludes -c 15 | FILES = $(shell ls | grep -E "ft_.+\.c") 16 | OBJ = $(FILES:%.c=%.o) 17 | 18 | all: $(NAME) 19 | 20 | # This won't run if the .o files don't exist or are not modified 21 | $(NAME): $(OBJ) 22 | @ar rcs $(NAME) $(OBJ) 23 | 24 | # This won't run if the source files don't exist or are not modified 25 | $(OBJ): $(FILES) 26 | @gcc $(CFLAGS) $(FILES) 27 | 28 | clean: 29 | @rm -f $(OBJ) 30 | 31 | fclean: clean 32 | @rm -f $(NAME) 33 | 34 | re: fclean all 35 | 36 | # I use .PHONY to make sure that gnu make will still run even if files called 37 | # clean / fclean / all and re already exist in the directory 38 | .PHONY: clean fclean all re 39 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 04:34:26 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 04:34:27 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int i; 18 | int num; 19 | int sign; 20 | 21 | i = 0; 22 | num = 0; 23 | sign = 1; 24 | while (*(str + i) == '\n' || 25 | *(str + i) == '\t' || 26 | *(str + i) == '\r' || 27 | *(str + i) == '\v' || 28 | *(str + i) == '\f' || 29 | *(str + i) == ' ') 30 | i++; 31 | if (*(str + i) == '-') 32 | sign = -1; 33 | if (*(str + i) == '-' || *(str + i) == '+') 34 | i++; 35 | while (*(str + i) && *(str + i) >= '0' && *(str + i) <= '9') 36 | num = num * 10 + (*(str + i++) - '0'); 37 | return (num * sign); 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 15:52:51 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 15:52:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | if (!n) 21 | return ; 22 | ptr = s; 23 | i = 0; 24 | while (i < n) 25 | *(ptr + i++) = 0; 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_capitalize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_capitalize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:51:24 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:51:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_capitalize(char *s) 16 | { 17 | char *new; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new = ft_strnew(ft_strlen(s)); 23 | new[0] = ft_toupper(s[0]); 24 | i = 0; 25 | while (*(s + ++i)) 26 | if (!ft_isalnum(s[i - 1]) && ft_isalnum(s[i])) 27 | new[i] = ft_toupper(s[i]); 28 | else 29 | new[i] = s[i]; 30 | return (new); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_copyuntil.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* copyuntil.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/11/26 05:49:22 by jrameau #+# #+# */ 9 | /* Updated: 2016/11/26 05:52:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_copyuntil(char **dst, char *src, char c) 16 | { 17 | int i; 18 | int count; 19 | int pos; 20 | 21 | i = -1; 22 | count = 0; 23 | while (src[++i]) 24 | if (src[i] == c) 25 | break ; 26 | pos = i; 27 | if (!(*dst = ft_strnew(i))) 28 | return (0); 29 | while (src[count] && count < i) 30 | { 31 | if (!(*dst = ft_strjoinch(*dst, src[count]))) 32 | return (0); 33 | count++; 34 | } 35 | return (pos); 36 | } 37 | -------------------------------------------------------------------------------- /libft/ft_countwords.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_countwords.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 15:17:40 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 15:17:41 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_countwords(char const *str, char c) 16 | { 17 | int count; 18 | int i; 19 | 20 | i = 0; 21 | count = 0; 22 | while (str[i]) 23 | { 24 | while (str[i] == c) 25 | i++; 26 | if (str[i] != c && str[i] != '\0') 27 | count++; 28 | while (str[i] != c && str[i] != '\0') 29 | i++; 30 | } 31 | return (count); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_intlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_intlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/22 13:09:38 by jrameau #+# #+# */ 9 | /* Updated: 2017/03/14 21:07:32 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_intlen(int num) 14 | { 15 | if(num >= 100000) { 16 | if(num >= 10000000) { 17 | if(num >= 1000000000) return 10; 18 | if(num >= 100000000) return 9; 19 | return 8; 20 | } 21 | if(num >= 1000000) return 7; 22 | return 6; 23 | } 24 | else { 25 | if(num >= 1000) { 26 | if(num >= 10000) return 5; 27 | return 4; 28 | } 29 | else { 30 | if(num >= 100) return 3; 31 | if(num >= 10) return 2; 32 | } 33 | return 1; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 20:54:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 20:54:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | return (ft_isalpha(c) || ft_isdigit(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:39:45 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:39:46 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | return (ft_islower(c) || ft_isupper(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 21:10:58 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:02 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | return (c >= 0 && c <= 127); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 17:00:15 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 23:43:14 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | return (c <= '9' && c >= '0'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_islower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_islower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:45:32 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:45:33 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_islower(int c) 16 | { 17 | return (c <= 'z' && c >= 'a'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 21:21:53 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 23:40:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | return (c >= 32 && c < 127); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:42:09 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:42:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isupper(int c) 16 | { 17 | return (c <= 'Z' && c >= 'A'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 02:04:57 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 02:04:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t get_str_len(int n) 16 | { 17 | size_t i; 18 | 19 | i = 1; 20 | while (n /= 10) 21 | i++; 22 | return (i); 23 | } 24 | 25 | char *ft_itoa(int n) 26 | { 27 | char *str; 28 | size_t str_len; 29 | unsigned int n_cpy; 30 | 31 | str_len = get_str_len(n); 32 | n_cpy = n; 33 | if (n < 0) 34 | { 35 | n_cpy = -n; 36 | str_len++; 37 | } 38 | if (!(str = ft_strnew(str_len))) 39 | return (NULL); 40 | str[--str_len] = n_cpy % 10 + '0'; 41 | while (n_cpy /= 10) 42 | str[--str_len] = n_cpy % 10 + '0'; 43 | if (n < 0) 44 | *(str + 0) = '-'; 45 | return (str); 46 | } 47 | -------------------------------------------------------------------------------- /libft/ft_lst_reverse.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_reverse.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/10/15 16:09:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/10/15 16:09:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lst_reverse(t_list *alst) 16 | { 17 | t_list *prev; 18 | t_list *cur; 19 | t_list *next; 20 | 21 | prev = NULL; 22 | cur = alst; 23 | while (cur != NULL) 24 | { 25 | next = cur->next; 26 | cur->next = prev; 27 | prev = cur; 28 | cur = next; 29 | } 30 | alst = prev; 31 | return (alst); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_lstadd.c: -------------------------------------------------------------------------------- 1 | 2 | /* ************************************************************************** */ 3 | /* */ 4 | /* ::: :::::::: */ 5 | /* ft_lstadd.c :+: :+: :+: */ 6 | /* +:+ +:+ +:+ */ 7 | /* By: jrameau +#+ +:+ +#+ */ 8 | /* +#+#+#+#+#+ +#+ */ 9 | /* Created: 2016/09/29 00:01:01 by jrameau #+# #+# */ 10 | /* Updated: 2016/09/29 00:01:03 by jrameau ### ########.fr */ 11 | /* */ 12 | /* ************************************************************************** */ 13 | 14 | #include "libft.h" 15 | 16 | void ft_lstadd(t_list **alst, t_list *new) 17 | { 18 | new->next = *alst; 19 | *alst = new; 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstaddback.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:01:01 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 00:01:03 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstaddback(t_list **alst, t_list *new) 16 | { 17 | t_list *tmp; 18 | t_list *head; 19 | 20 | tmp = *alst; 21 | head = tmp; 22 | while (tmp->next) 23 | tmp = tmp->next; 24 | tmp->next = new; 25 | *alst = head; 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_lstdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 23:58:22 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 23:58:27 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) 16 | { 17 | if ((*alst)->next) 18 | ft_lstdel(&(*alst)->next, del); 19 | ft_lstdelone(&(*alst), del); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 23:17:34 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 23:17:35 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) 16 | { 17 | del((*alst)->content, (*alst)->content_size); 18 | free(*alst); 19 | *alst = NULL; 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:16:06 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 00:16:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) 16 | { 17 | if (!lst) 18 | return ; 19 | if (lst->next) 20 | ft_lstiter(lst->next, f); 21 | f(lst); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:20:40 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 00:20:41 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) 16 | { 17 | t_list *new; 18 | t_list *list; 19 | 20 | if (!lst) 21 | return (NULL); 22 | list = f(lst); 23 | new = list; 24 | while (lst->next) 25 | { 26 | lst = lst->next; 27 | if (!(list->next = f(lst))) 28 | { 29 | free(list->next); 30 | return (NULL); 31 | } 32 | list = list->next; 33 | } 34 | return (new); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:49:15 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:49:16 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void const *content, size_t content_size) 16 | { 17 | t_list *list; 18 | 19 | if (!(list = (t_list *)malloc(sizeof(*list)))) 20 | return (NULL); 21 | if (!content) 22 | { 23 | list->content = NULL; 24 | list->content_size = 0; 25 | } 26 | else 27 | { 28 | if (!(list->content = malloc(content_size))) 29 | return (NULL); 30 | ft_memcpy(list->content, content, content_size); 31 | list->content_size = content_size; 32 | } 33 | list->next = NULL; 34 | return (list); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_memalloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memalloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/24 03:37:39 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:55:50 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memalloc(size_t size) 16 | { 17 | void *mem; 18 | 19 | mem = malloc(size); 20 | if (!mem) 21 | return (NULL); 22 | ft_bzero(mem, size); 23 | return (mem); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 21:34:36 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 21:34:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | i = -1; 21 | ptr = dst; 22 | while (++i < n) 23 | { 24 | *(ptr + i) = *((unsigned char *)src + i); 25 | if (*((unsigned char *)src + i) == (unsigned char)c) 26 | return (dst + i + 1); 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 13:54:41 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 17:55:42 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | const char *sc; 18 | size_t i; 19 | 20 | sc = (const char *)s; 21 | i = -1; 22 | while (++i < n) 23 | if (*(sc + i) == (char)c) 24 | return ((void *)sc + i); 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 14:18:12 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 14:18:14 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | unsigned char *s1c; 18 | unsigned char *s2c; 19 | size_t i; 20 | 21 | i = -1; 22 | s1c = (unsigned char *)s1; 23 | s2c = (unsigned char *)s2; 24 | while (++i < n && *(s1c + i) == *(s2c + i)) 25 | ; 26 | if (i == n) 27 | return (0); 28 | return (*(s1c + i) - *(s2c + i)); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 16:54:04 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 16:54:05 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | size_t i; 18 | char *ptr; 19 | char *ptr2; 20 | 21 | ptr = dst; 22 | ptr2 = (char *)src; 23 | i = -1; 24 | while (++i < n) 25 | *(ptr + i) = *(ptr2 + i); 26 | return (dst); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_memdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 20:09:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 20:09:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_memdel(void **ap) 16 | { 17 | if (!ap || !*ap) 18 | return ; 19 | free(*ap); 20 | *ap = 0; 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 12:41:41 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 17:15:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | char *srcc; 18 | char *dstc; 19 | size_t i; 20 | 21 | i = -1; 22 | srcc = (char *)src; 23 | dstc = (char *)dst; 24 | if (srcc < dstc) 25 | while ((int)(--len) >= 0) 26 | *(dstc + len) = *(srcc + len); 27 | else 28 | while (++i < len) 29 | *(dstc + i) = *(srcc + i); 30 | return (dst); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 13:13:13 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 13:13:16 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | ptr = b; 21 | i = 0; 22 | while (i < len) 23 | *(ptr + i++) = c; 24 | return (b); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_pathjoin.c: -------------------------------------------------------------------------------- 1 | #include "libft.h" 2 | 3 | char *ft_pathjoin(char *p1, char *p2) 4 | { 5 | if (!ft_strendswith(p1, "/")) 6 | return (ft_strjoin(ft_strjoinch(p1, '/'), p2)); 7 | return (ft_strjoin(p1, p2)); 8 | } 9 | -------------------------------------------------------------------------------- /libft/ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/08/17 17:45:18 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 21:44:04 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar(char c) 16 | { 17 | ft_putchar_fd(c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 17:49:11 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 17:49:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putendl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:04:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:04:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl(char const *s) 16 | { 17 | ft_putendl_fd(s, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:26:34 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:26:34 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char const *s, int fd) 16 | { 17 | ft_putstr_fd(ft_strjoin(s, "\n"), fd); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:10:39 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:10:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr(int n) 16 | { 17 | ft_putnbr_fd(n, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:18:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (n < 0) 18 | { 19 | ft_putchar_fd('-', fd); 20 | n = -n; 21 | } 22 | if (n == -2147483648) 23 | { 24 | ft_putchar_fd('2', fd); 25 | n %= 1000000000; 26 | n = -n; 27 | } 28 | if (n >= 10) 29 | { 30 | ft_putnbr_fd(n / 10, fd); 31 | ft_putnbr_fd(n % 10, fd); 32 | } 33 | else 34 | ft_putchar_fd(n + '0', fd); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/08/17 17:47:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/08/17 17:47:34 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr(char const *s) 16 | { 17 | ft_putstr_fd(s, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:23:42 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/15 18:36:20 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char const *s, int fd) 16 | { 17 | write(fd, s, ft_strlen(s)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_realloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_realloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/10/21 01:11:53 by jrameau #+# #+# */ 9 | /* Updated: 2016/10/21 01:11:56 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_realloc(void *ptr, size_t size) 16 | { 17 | void *new_ptr; 18 | 19 | if (!ptr || !size) 20 | return (NULL); 21 | if (!(new_ptr = ft_strnew(size))) 22 | return (NULL); 23 | ft_strcpy(new_ptr, ptr); 24 | return (new_ptr); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 18:07:43 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 18:07:44 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcat(char *s1, const char *s2) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = -1; 21 | j = (int)ft_strlen(s1); 22 | while (*(s2 + ++i)) 23 | *(s1 + j++) = *(s2 + i); 24 | *(s1 + j) = '\0'; 25 | return (s1); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 00:41:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 00:41:21 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (++i < (int)ft_strlen(s) + 1) 21 | if (*(s + i) == (char)c) 22 | return ((char *)s + i); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strclr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strclr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:07:51 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:07:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strclr(char *s) 16 | { 17 | if (s) 18 | ft_bzero(s, ft_strlen(s)); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 03:51:36 by jrameau #+# #+# */ 9 | /* Updated: 2017/02/21 15:53:57 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strcmp(const char *s1, const char *s2) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*(s1 + i) && *(s1 + i) == *(s2 + i)) 21 | i++; 22 | return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 15:25:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 02:49:18 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcpy(char *dst, const char *src) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (*(src + ++i)) 21 | *(dst + i) = *(src + i); 22 | *(dst + i) = '\0'; 23 | return (dst); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:03:47 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:03:48 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strdel(char **as) 16 | { 17 | if (!as || !*as) 18 | return ; 19 | free(*as); 20 | *as = 0; 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 14:52:14 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 14:52:15 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | return (ft_strndup(s1, ft_strlen(s1))); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_strendswith.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strendswith.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/01/17 20:56:49 by jrameau #+# #+# */ 9 | /* Updated: 2017/01/17 23:58:22 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strendswith(char *s1, char *s2) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (s1[++i]) 21 | if (s1[i] == s2[0]) 22 | if (ft_strcmp(s1 + i, s2) == 0) 23 | return (1); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:42:03 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:42:04 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strequ(char const *s1, char const *s2) 16 | { 17 | if (!s1 || !s2) 18 | return (0); 19 | return (ft_strcmp(s1, s2) ? 0 : 1); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_striter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:12:58 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:12:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striter(char *s, void (*f)(char *)) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (s && f) 21 | while (*(s + i)) 22 | f(s + i++); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:22:13 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:22:15 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | if (s && f) 21 | while (*(s + ++i)) 22 | f(i, s + i); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:08:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 00:08:21 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | char *new_str; 18 | size_t i; 19 | size_t j; 20 | size_t s1_len; 21 | size_t s2_len; 22 | 23 | if (!s1 || !s2) 24 | return (NULL); 25 | s1_len = ft_strlen(s1); 26 | s2_len = ft_strlen(s2); 27 | new_str = ft_strnew(s1_len + s2_len); 28 | if (!new_str) 29 | return (NULL); 30 | i = -1; 31 | j = -1; 32 | while (++i < s1_len) 33 | *(new_str + i) = *(s1 + i); 34 | while (++j < s2_len) 35 | *(new_str + i++) = *(s2 + j); 36 | return (new_str); 37 | } 38 | -------------------------------------------------------------------------------- /libft/ft_strjoinch.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:08:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 00:08:21 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoinch(char const *s1, char c) 16 | { 17 | char *new_str; 18 | size_t i; 19 | size_t s1_len; 20 | 21 | if (!s1 || !c) 22 | return (NULL); 23 | s1_len = ft_strlen(s1); 24 | new_str = ft_strnew(s1_len + 1); 25 | if (!new_str) 26 | return (NULL); 27 | i = -1; 28 | while (++i < s1_len) 29 | *(new_str + i) = *(s1 + i); 30 | *(new_str + i) = c; 31 | return (new_str); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 19:27:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 22:41:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t size) 16 | { 17 | size_t i; 18 | int j; 19 | size_t dst_len; 20 | size_t src_len; 21 | 22 | i = ft_strlen(dst); 23 | j = 0; 24 | dst_len = ft_strlen(dst); 25 | src_len = ft_strlen(src); 26 | if (size < dst_len + 1) 27 | return (src_len + size); 28 | if (size > dst_len + 1) 29 | { 30 | while (i < size - 1) 31 | *(dst + i++) = *(src + j++); 32 | *(dst + i) = '\0'; 33 | } 34 | return (dst_len + src_len); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 12:43:37 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 12:43:39 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (*(s + ++i)) 21 | ; 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:27:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:27:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmap(char const *s, char (*f)(char)) 16 | { 17 | char *new_str; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(ft_strlen(s)); 23 | if (!new_str) 24 | return (NULL); 25 | i = -1; 26 | while (*(s + ++i)) 27 | *(new_str + i) = f(*(s + i)); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:39:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:39:43 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *new_str; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(ft_strlen(s)); 23 | if (!new_str) 24 | return (NULL); 25 | i = -1; 26 | while (*(s + ++i)) 27 | *(new_str + i) = f(i, *(s + i)); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strncat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 18:58:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:16:42 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncat(char *s1, const char *s2, size_t n) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = -1; 21 | j = (int)ft_strlen(s1); 22 | while (*(s2 + ++i) && i < (int)n) 23 | *(s1 + j++) = *(s2 + i); 24 | *(s1 + j) = '\0'; 25 | return (s1); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strnchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/11/26 05:38:48 by jrameau #+# #+# */ 9 | /* Updated: 2016/11/26 05:39:55 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnchr(char *s, char c, int offset) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (s[++i]) 21 | if (s[i] == c) 22 | return (s + i + offset); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 04:22:10 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 04:22:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*(s1 + i) && *(s1 + i) == *(s2 + i) && i < (int)n - 1) 21 | i++; 22 | if (n) 23 | return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strncpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/24 02:48:43 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncpy(char *dst, const char *src, size_t len) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (++i < len) 21 | if (*(src + i)) 22 | *(dst + i) = *(src + i); 23 | else 24 | while (i < len) 25 | *(dst + i++) = '\0'; 26 | return (dst); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_strndup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strndup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:20:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:20:23 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strndup(const char *s1, size_t n) 16 | { 17 | char *tmp; 18 | 19 | if (!(tmp = ft_strnew(n))) 20 | return (NULL); 21 | ft_strncpy(tmp, s1, n); 22 | return (tmp); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strnequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:47:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:47:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strnequ(char const *s1, char const *s2, size_t n) 16 | { 17 | if (!s1 || !s2) 18 | return (0); 19 | return (ft_strncmp(s1, s2, n) ? 0 : 1); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_strnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 22:53:09 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 22:53:10 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnew(size_t size) 16 | { 17 | char *str; 18 | 19 | str = (char *)malloc(sizeof(char) * size + 1); 20 | if (!str) 21 | return (NULL); 22 | ft_bzero(str, size + 1); 23 | return (str); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 03:19:21 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/25 02:15:51 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *big, const char *little, size_t len) 16 | { 17 | size_t i; 18 | size_t j; 19 | size_t k; 20 | int found; 21 | 22 | i = -1; 23 | found = 1; 24 | if (!ft_strlen(little)) 25 | return ((char *)big); 26 | while (*(big + ++i) && i < len) 27 | { 28 | j = 0; 29 | if (*(big + i) == *(little + 0)) 30 | { 31 | k = i; 32 | found = 1; 33 | while (*(big + k) && *(little + j) && j < len && k < len) 34 | if (*(big + k++) != *(little + j++)) 35 | found = 0; 36 | if (found && !*(little + j)) 37 | return ((char *)big + i); 38 | } 39 | } 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:25:57 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:25:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = (int)ft_strlen(s) + 1; 20 | while (i--) 21 | if (*(s + i) == (char)c) 22 | return ((char *)s + i); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strsplit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsplit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 13:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 13:18:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int get_word_len(char const *str, char c) 16 | { 17 | int i; 18 | int len; 19 | 20 | i = 0; 21 | len = 0; 22 | while (str[i] == c) 23 | i++; 24 | while (str[i] != c && str[i] != '\0') 25 | { 26 | i++; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | char **ft_strsplit(char const *s, char c) 33 | { 34 | int i; 35 | int j; 36 | int k; 37 | char **str2; 38 | 39 | if (!s || !(str2 = (char **)malloc(sizeof(*str2) * 40 | (ft_countwords(s, c) + 1)))) 41 | return (NULL); 42 | i = -1; 43 | j = 0; 44 | while (++i < ft_countwords(s, c)) 45 | { 46 | k = 0; 47 | if (!(str2[i] = ft_strnew(get_word_len(&s[j], c) + 1))) 48 | str2[i] = NULL; 49 | while (s[j] == c) 50 | j++; 51 | while (s[j] != c && s[j]) 52 | str2[i][k++] = s[j++]; 53 | str2[i][k] = '\0'; 54 | } 55 | str2[i] = 0; 56 | return (str2); 57 | } 58 | -------------------------------------------------------------------------------- /libft/ft_strstartswith.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/14 17:33:27 by jrameau #+# #+# */ 9 | /* Updated: 2017/01/15 10:18:18 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_strstartswith(char *s1, char *s2) 14 | { 15 | int i; 16 | 17 | i = -1; 18 | while (s2[++i]) 19 | if (s1[i] != s2[i]) 20 | return (0); 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_strstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 01:39:09 by jrameau #+# #+# */ 9 | /* Updated: 2017/01/17 22:39:47 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strstr(const char *big, const char *little) 16 | { 17 | int i; 18 | int j; 19 | int k; 20 | int good; 21 | 22 | if (!ft_strlen(little)) 23 | return ((char *)big); 24 | i = -1; 25 | good = 0; 26 | while (*(big + ++i) && !good) 27 | { 28 | if (*(big + i) == *(little + 0)) 29 | { 30 | j = 0; 31 | k = i; 32 | good = 1; 33 | while (*(little + j)) 34 | if (*(little + j++) != *(big + k++)) 35 | good = 0; 36 | if (good) 37 | return ((char *)big + i); 38 | } 39 | } 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_strsub.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsub.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:50:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:50:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strsub(char const *s, unsigned int start, size_t len) 16 | { 17 | char *new_str; 18 | size_t i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(len); 23 | if (!new_str) 24 | return (NULL); 25 | i = 0; 26 | while (i < len) 27 | *(new_str + i++) = *(s + start++); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:50:46 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 00:50:47 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int has_whitespaces(char *str, int *i, size_t *j) 16 | { 17 | while (IS_SPACE(*(str + *i))) 18 | (*i)++; 19 | while (IS_SPACE(*(str + *j))) 20 | (*j)--; 21 | if (*i || *j < ft_strlen(str)) 22 | return (1); 23 | return (0); 24 | } 25 | 26 | char *ft_strtrim(char const *s) 27 | { 28 | int i; 29 | size_t j; 30 | int k; 31 | char *new_str; 32 | size_t new_size; 33 | 34 | if (!s) 35 | return (NULL); 36 | i = 0; 37 | k = 0; 38 | j = ft_strlen(s) - 1; 39 | if (!has_whitespaces((char *)s, &i, &j) || !ft_strlen(s)) 40 | return ((char *)s); 41 | new_size = (i == (int)ft_strlen(s)) ? 0 : ft_strlen(s) - (size_t)i - \ 42 | (ft_strlen(s) - j); 43 | new_str = ft_strnew(new_size + 1); 44 | if (!new_str) 45 | return (NULL); 46 | while (i <= (int)j) 47 | *(new_str + k++) = *(s + i++); 48 | return (new_str); 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 23:44:33 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:24:45 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (ft_isupper(c)) 18 | return (c + 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 22:57:01 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:25 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (ft_islower(c)) 18 | return (c - 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/includes/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 21:30:24 by jrameau #+# #+# */ 9 | /* Updated: 2017/03/14 21:08:41 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | # include 16 | # include 17 | # include 18 | 19 | void ft_putchar(char c); 20 | void ft_putstr(char const *str); 21 | void ft_putendl(char const *str); 22 | void ft_putnbr(int nbr); 23 | void ft_putchar_fd(char c, int fd); 24 | void ft_putstr_fd(char const *s, int fd); 25 | void ft_putendl_fd(char const *s, int fd); 26 | void ft_putnbr_fd(int n, int fd); 27 | 28 | char *ft_itoa(int n); 29 | int ft_atoi(const char *str); 30 | 31 | void *ft_memalloc(size_t size); 32 | void ft_memdel(void **ap); 33 | char *ft_strnew(size_t size); 34 | void ft_strdel(char **as); 35 | void ft_strclr(char *s); 36 | void ft_striter(char *s, void (*f)(char *)); 37 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 38 | char *ft_strmap(char const *s, char (*f)(char)); 39 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 40 | 41 | int ft_strequ(char const *s1, char const *s2); 42 | int ft_strnequ(char const *s1, char const *s2, size_t n); 43 | char *ft_strsub(char const *s, unsigned int start, size_t len); 44 | char *ft_strjoin(char const *s1, char const *s2); 45 | char *ft_strtrim(char const *s); 46 | char **ft_strsplit(char const *s, char c); 47 | 48 | void *ft_memset(void *b, int c, size_t len); 49 | void ft_bzero(void *s, size_t n); 50 | void *ft_memcpy(void *dst, const void *src, size_t n); 51 | void *ft_memccpy(void *dst, const void *restrict src, 52 | int c, size_t n); 53 | void *ft_memmove(void *dst, const void *src, size_t len); 54 | void *ft_memchr(const void *s, int c, size_t n); 55 | int ft_memcmp(const void *s1, const void *s2, size_t n); 56 | 57 | size_t ft_strlen(const char *str); 58 | char *ft_strdup(const char *s1); 59 | char *ft_strndup(const char *s1, size_t n); 60 | char *ft_strcpy(char *dst, const char *src); 61 | char *ft_strncpy(char *dst, const char *src, size_t len); 62 | char *ft_strcat(char *s1, const char *s2); 63 | char *ft_strncat(char *s1, const char *s2, size_t n); 64 | size_t ft_strlcat(char *dst, const char *src, size_t size); 65 | char *ft_strchr(const char *s, int c); 66 | char *ft_strrchr(const char *s, int c); 67 | char *ft_strstr(const char *big, const char *little); 68 | char *ft_strnstr(const char *big, 69 | const char *little, size_t len); 70 | int ft_strcmp(const char *s1, const char *s2); 71 | int ft_strncmp(const char *s1, const char *s2, size_t n); 72 | 73 | int ft_isalpha(int c); 74 | int ft_isdigit(int c); 75 | int ft_isalnum(int c); 76 | int ft_isascii(int c); 77 | int ft_isprint(int c); 78 | int ft_toupper(int c); 79 | int ft_tolower(int c); 80 | 81 | # ifndef IS_SPACE 82 | # define IS_SPACE(x) (x==' '||x=='\n'||x=='\t') 83 | # endif 84 | 85 | typedef struct s_list 86 | { 87 | void *content; 88 | size_t content_size; 89 | struct s_list *next; 90 | } t_list; 91 | 92 | t_list *ft_lstnew(const void *content, size_t content_size); 93 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); 94 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); 95 | void ft_lstadd(t_list **alst, t_list *n); 96 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); 97 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); 98 | 99 | /* 100 | ** Extra functions 101 | */ 102 | 103 | int ft_isupper(int c); 104 | int ft_islower(int c); 105 | int ft_countwords(char const *str, char c); 106 | char *ft_strndup(const char *s1, size_t n); 107 | char *ft_capitalize(char *s); 108 | t_list *ft_lst_reverse(t_list *alst); 109 | void *ft_realloc(void *ptr, size_t size); 110 | char *ft_strjoinch(char const *s1, char c); 111 | char *ft_strnchr(char *s, char c, int offset); 112 | int ft_copyuntil(char **dst, char *src, char c); 113 | int ft_strstartswith(char *s1, char *s2); 114 | int ft_intlen(int num); 115 | int ft_strendswith(char *s1, char *s2); 116 | char *ft_pathjoin(char *p1, char *p2); 117 | void ft_lstaddback(t_list **alst, t_list *new); 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /src/dir_handlers.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* dir_handlers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/03/27 13:32:37 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/18 13:37:50 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_ls.h" 14 | 15 | 16 | t_dirs *new_dir(char *path, int status, int is_default, char *subdir_name, t_flags flags) 17 | { 18 | t_dirs *dir; 19 | DIR *dr; 20 | struct stat f; 21 | 22 | MEMCHECK((dir = (t_dirs *)ft_memalloc(sizeof(t_dirs)))); 23 | MEMCHECK((dir->name = ft_strdup(path))); 24 | MEMCHECK((dir->self = (t_files *)ft_memalloc(sizeof(t_files)))); 25 | MEMCHECK((dir->self->name = ft_strdup(path))); 26 | if (status == IS_LINK) 27 | { 28 | if (!(dr = opendir(dir->name)) || flags & LONG_LISTING_FLAG) 29 | status = IS_NOTDIR; 30 | else 31 | status = IS_DIR; 32 | if (dr) 33 | closedir(dr); 34 | } 35 | if (status != IS_NONEXISTENT) 36 | { 37 | // Check if this passed 38 | lstat(dir->name, &f); 39 | dir->date.mtv_sec = (unsigned long long)f.st_mtimespec.tv_sec; 40 | dir->date.mtv_nsec = (unsigned long long)f.st_mtimespec.tv_nsec; 41 | dir->date.atv_sec = (unsigned long long)f.st_atimespec.tv_sec; 42 | dir->date.atv_nsec = (unsigned long long)f.st_atimespec.tv_nsec; 43 | dir->date.ctv_sec = (unsigned long long)f.st_ctimespec.tv_sec; 44 | dir->date.ctv_nsec = (unsigned long long)f.st_ctimespec.tv_nsec; 45 | dir->date.birthtv_sec = (unsigned long long)f.st_birthtimespec.tv_sec; 46 | dir->date.birthtv_nsec = (unsigned long long)f.st_birthtimespec.tv_nsec; 47 | MEMCHECK((dir->self->display_name = ft_strdup(path))); 48 | dir->self->is_dir_info = 1; 49 | if (subdir_name) 50 | MEMCHECK((dir->display_name = ft_strdup(subdir_name))); 51 | } 52 | dir->status = status; 53 | dir->next = NULL; 54 | dir->is_default = is_default; 55 | dir->is_unreadable = 0; 56 | dir->total_blocks = 0; 57 | dir->file_count = 0; 58 | dir->has_valid_files = 0; 59 | dir->max_file_len = 0; 60 | return (dir); 61 | } 62 | 63 | void add_dir(t_dirs **dirs, t_dirs *new) { 64 | t_dirs *tmp; 65 | t_dirs *head; 66 | 67 | tmp = *dirs; 68 | head = tmp; 69 | while (tmp->next) 70 | tmp = tmp->next; 71 | tmp->next = new; 72 | *dirs = head; 73 | } 74 | 75 | void reverse_dirs(t_dirs **dirs) 76 | { 77 | t_dirs *curr; 78 | t_dirs *next; 79 | t_dirs *prev; 80 | 81 | prev = NULL; 82 | curr = *dirs; 83 | while (curr) 84 | { 85 | next = curr->next; 86 | curr->next = prev; 87 | prev = curr; 88 | curr = next; 89 | } 90 | *dirs = prev; 91 | } 92 | 93 | void set_dir(char *path, t_dirs **dirs, char *subdir_name, t_flags flags) { 94 | t_dirs *new; 95 | int status; 96 | struct stat f; 97 | 98 | status = IS_DIR; 99 | if (lstat(path, &f) == -1) 100 | { 101 | if (ENOENT == errno) 102 | status = IS_NONEXISTENT; 103 | } 104 | else 105 | { 106 | if (!S_ISDIR(f.st_mode)) 107 | status = IS_NOTDIR; 108 | } 109 | if (S_ISLNK(f.st_mode)) 110 | status = IS_LINK; 111 | new = new_dir(path, status, 0, subdir_name, flags); 112 | if (!*dirs || (*dirs)->is_default) 113 | *dirs = new; 114 | else 115 | add_dir(dirs, new); 116 | } 117 | 118 | t_dirs *dir_handler(char **args, t_flags flags) { 119 | int i; 120 | t_dirs *dirs; 121 | t_etarget target; 122 | t_dirs *tmp; 123 | 124 | dirs = new_dir(".", IS_DIR, 1, 0, flags); 125 | i = -1; 126 | while (args[++i]) 127 | { 128 | if (args[i][0] == '\0') 129 | { 130 | MEMCHECK((target.file = ft_strdup("fts_open"))); 131 | error_handler(NONEXISTENT_ERR, target); 132 | free(target.file); 133 | exit(1); 134 | } 135 | set_dir(args[i], &dirs, NULL, flags); 136 | } 137 | 138 | if (flags & FILE_SIZE_SORT) 139 | { 140 | tmp = dirs; 141 | while (tmp) 142 | { 143 | add_file(&tmp->self, &tmp, flags, IDLE_FORMAT); 144 | tmp = tmp->next; 145 | } 146 | } 147 | dir_sort(&dirs, flags); 148 | return (dirs); 149 | } 150 | -------------------------------------------------------------------------------- /src/dir_handlers_2.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | #include 3 | 4 | void move_dir(t_dirs **destRef, t_dirs **sourceRef) 5 | { 6 | t_dirs *new; 7 | 8 | new = *sourceRef; 9 | *sourceRef = (*sourceRef)->next; 10 | new->next = *destRef; 11 | *destRef = new; 12 | } 13 | 14 | void handle_dir_merge_comparison(t_dirs **a, t_dirs **b, t_dirs **tmp, t_flags flags) 15 | { 16 | int comparison; 17 | 18 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 19 | if (flags & MODIFICATION_DATE_SORT) 20 | { 21 | comparison = (*a)->date.mtv_sec >= (*b)->date.mtv_sec; 22 | if ((*a)->date.mtv_sec == (*b)->date.mtv_sec) 23 | { 24 | comparison = (*a)->date.mtv_nsec >= (*b)->date.mtv_nsec; 25 | if ((*a)->date.mtv_nsec == (*b)->date.mtv_nsec) 26 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 27 | } 28 | if (flags & CREATION_DATE_SORT) 29 | { 30 | comparison = (*a)->date.birthtv_sec >= (*b)->date.birthtv_sec; 31 | if ((*a)->date.birthtv_sec == (*b)->date.birthtv_sec) 32 | { 33 | comparison = (*a)->date.birthtv_nsec >= (*b)->date.birthtv_nsec; 34 | if ((*a)->date.birthtv_nsec == (*b)->date.birthtv_nsec) 35 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 36 | } 37 | } 38 | else if (flags & LAST_ACCESS_DATE_SORT) 39 | { 40 | comparison = (*a)->date.atv_sec >= (*b)->date.atv_sec; 41 | if ((*a)->date.atv_sec == (*b)->date.atv_sec) 42 | { 43 | comparison = (*a)->date.atv_nsec >= (*b)->date.atv_nsec; 44 | if ((*a)->date.atv_nsec == (*b)->date.atv_nsec) 45 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 46 | } 47 | } 48 | else if (flags & LAST_STATUS_CHANGE_SORT) 49 | { 50 | comparison = (*a)->date.ctv_sec >= (*b)->date.ctv_sec; 51 | if ((*a)->date.ctv_sec == (*b)->date.ctv_sec) 52 | { 53 | comparison = (*a)->date.ctv_nsec >= (*b)->date.ctv_nsec; 54 | if ((*a)->date.ctv_nsec == (*b)->date.ctv_nsec) 55 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 56 | } 57 | } 58 | } 59 | else if (flags & FILE_SIZE_SORT) 60 | { 61 | comparison = (*a)->self->size >= (*b)->self->size; 62 | if ((*a)->self->size == (*b)->self->size) 63 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 64 | } 65 | move_dir(tmp, comparison ? a : b); 66 | } 67 | 68 | t_dirs *merge_splitted_dirs(t_dirs *a, t_dirs *b, t_flags flags) 69 | { 70 | t_dirs *res; 71 | t_dirs **tmp; 72 | 73 | res = NULL; 74 | tmp = &res; 75 | while (1) 76 | { 77 | if (!a) 78 | { 79 | *tmp = b; 80 | break; 81 | } 82 | else if (!b) 83 | { 84 | *tmp = a; 85 | break; 86 | } 87 | handle_dir_merge_comparison(&a, &b, tmp, flags); 88 | tmp = &((*tmp)->next); 89 | } 90 | return (res); 91 | } 92 | 93 | void split_dirs(t_dirs *sourceRef, t_dirs **frontRef, t_dirs **backRef) 94 | { 95 | t_dirs *slow; 96 | t_dirs *fast; 97 | 98 | slow = sourceRef; 99 | fast = sourceRef->next; 100 | while (fast) 101 | { 102 | fast = fast->next; 103 | if (fast) 104 | { 105 | slow = slow->next; 106 | fast = fast->next; 107 | } 108 | } 109 | *frontRef = sourceRef; 110 | *backRef = slow->next; 111 | slow->next = NULL; 112 | } 113 | 114 | void dir_sort(t_dirs **dirs, t_flags flags) 115 | { 116 | t_dirs *head; 117 | t_dirs *a; 118 | t_dirs *b; 119 | 120 | head = *dirs; 121 | if (!head || !head->next) 122 | return ; 123 | split_dirs(head, &a, &b); 124 | dir_sort(&a, flags); 125 | dir_sort(&b, flags); 126 | *dirs = merge_splitted_dirs(a, b, flags); 127 | } 128 | -------------------------------------------------------------------------------- /src/display_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void date_display_handler(t_format format, t_date date, t_flags flags) 4 | { 5 | struct timeval tp; 6 | unsigned long long curr_date; 7 | unsigned long long six_months; 8 | unsigned long long t; 9 | 10 | gettimeofday(&tp, NULL); 11 | curr_date = (unsigned long long)tp.tv_sec; 12 | six_months = 15778476; 13 | t = date.mtv_sec; 14 | if (flags & CREATION_DATE_SORT) 15 | t = date.birthtv_sec; 16 | if (flags & LAST_ACCESS_DATE_SORT) 17 | t = date.atv_sec; 18 | if (flags & LAST_STATUS_CHANGE_SORT) 19 | t = date.ctv_sec; 20 | if (t <= (curr_date - six_months) || t >= (curr_date + six_months)) 21 | print_handler(1, "%s ", format.date_year, date.year); 22 | else 23 | { 24 | print_handler(1, "%s:", format.date_hour, date.hour); 25 | print_handler(1, "%s ", format.date_minute, date.minute); 26 | } 27 | } 28 | 29 | void display_file_name(struct stat f, char *name, t_flags flags) 30 | { 31 | if (!(flags & COLORED_OUTPUT)) 32 | return (print_handler(1, "%s", 0, name)); 33 | if (S_ISDIR(f.st_mode)) 34 | print_handler(1, ANSI_COLOR_BOLD_CYAN "%s" ANSI_COLOR_RESET, 0, name); 35 | else if (S_ISLNK(f.st_mode)) 36 | print_handler(1, ANSI_COLOR_MAGENTA "%s" ANSI_COLOR_RESET, 0, name); 37 | else if (S_ISSOCK(f.st_mode)) 38 | print_handler(1, ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET, 0, name); 39 | else if (S_ISFIFO(f.st_mode)) 40 | print_handler(1, ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET, 0, name); 41 | else if (S_ISBLK(f.st_mode)) 42 | print_handler(1, ANSI_COLOR_BOLD_GREEN "%s" ANSI_COLOR_RESET, 0, name); 43 | else if (S_ISCHR(f.st_mode)) 44 | print_handler(1, ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET, 0, name); 45 | else if (S_ISREG(f.st_mode) && f.st_mode & S_IXUSR) 46 | print_handler(1, ANSI_COLOR_RED "%s" ANSI_COLOR_RESET, 0, name); 47 | else 48 | print_handler(1, "%s", 0, name); 49 | } 50 | 51 | void long_listing_display(t_format format, t_files *file, int has_chr_or_blk, t_flags flags) { 52 | print_handler(1, "%s ", 0, file->modes); 53 | print_handler(1, "%ld ", format.link, ft_itoa(file->link)); 54 | if (!(flags & SUPRESS_OWNER)) 55 | { 56 | if (file->owner && !(flags & DISPLAY_UID_AND_GID)) 57 | lprint_handler(1, "%s ", format.owner, file->owner); 58 | else 59 | lprint_handler(1, "%d ", format.user_id, ft_itoa(file->user_id)); 60 | } 61 | if (file->group && !(flags & DISPLAY_UID_AND_GID)) 62 | lprint_handler(1, "%s ", format.group, file->group); 63 | else 64 | lprint_handler(1, "%d ", format.group_id, ft_itoa(file->group_id)); 65 | if (file->is_chr_or_blk) 66 | { 67 | print_handler(1, " %ld, ", format.major, ft_itoa(file->major)); 68 | print_handler(1, "%ld ", format.minor, ft_itoa(file->minor)); 69 | } 70 | else 71 | print_handler(1, "%ld ", has_chr_or_blk ? format.major + format.minor + format.fileSize + 2 : format.fileSize, ft_itoa(file->size)); 72 | print_handler(1, "%s ", format.date_month, file->date.month); 73 | print_handler(1, "%s ", format.date_day, file->date.day); 74 | date_display_handler(format, file->date, flags); 75 | if (file->has_nonprintable_chars) 76 | display_file_name(file->f, file->display_name, flags); 77 | else 78 | display_file_name(file->f, file->name, flags); 79 | if (file->is_link) 80 | print_handler(1, " -> %s", 0, file->linked_to); 81 | print_handler(1, "\n", 0, NULL); 82 | } 83 | 84 | void column_display(t_entries entries, int file_count, int max_file_len, int target) 85 | { 86 | struct winsize w; 87 | int cols; 88 | int rows; 89 | char **arr; 90 | int term_width; 91 | int i; 92 | int pos; 93 | 94 | ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); 95 | term_width = w.ws_col; 96 | cols = term_width / (max_file_len + 1); 97 | if (!cols) 98 | cols = 1; 99 | if ((max_file_len + 1) * file_count < term_width) 100 | cols = file_count; 101 | rows = file_count / cols; 102 | if (file_count % cols) 103 | ++rows; 104 | arr = NULL; 105 | if (target == IS_DIR) 106 | { 107 | MEMCHECK((arr = (char **)ft_memalloc(sizeof(char *) * (file_count + 1)))); 108 | i = 0; 109 | while (entries.files) 110 | { 111 | if (entries.files->has_nonprintable_chars) 112 | arr[i++] = ft_strdup(entries.files->display_name); 113 | else 114 | arr[i++] = ft_strdup(entries.files->name); 115 | entries.files = entries.files->next; 116 | } 117 | } 118 | pos = 0; 119 | i = -1; 120 | while (++i < rows) 121 | { 122 | int j = -1; 123 | pos = i; 124 | while (++j < cols) 125 | { 126 | lprint_handler(1, "%s ", max_file_len, target == IS_DIR ? arr[pos] : entries.file_names[pos]); 127 | pos += rows; 128 | if (pos >= file_count) 129 | break; 130 | } 131 | print_handler(1, "\n", 0, NULL); 132 | } 133 | if (target == IS_DIR) 134 | free(arr); 135 | } 136 | 137 | void nondir_column_display(t_dirs *dirs, int should_separate) 138 | { 139 | t_dirs *tmp; 140 | int file_count; 141 | t_entries entries; 142 | int max_file_len; 143 | int i; 144 | 145 | file_count = 0; 146 | max_file_len = 0; 147 | tmp = dirs; 148 | while (tmp) 149 | { 150 | if (tmp->status == IS_NOTDIR) 151 | { 152 | ++file_count; 153 | if ((int)ft_strlen(tmp->self->name) > max_file_len) 154 | max_file_len = ft_strlen(tmp->self->name); 155 | } 156 | tmp = tmp->next; 157 | } 158 | MEMCHECK((entries.file_names = (char **)ft_memalloc(sizeof(char *) * file_count))); 159 | tmp = dirs; 160 | i = -1; 161 | while (tmp) 162 | { 163 | if (tmp->status == IS_NOTDIR) 164 | { 165 | if (tmp->self->has_nonprintable_chars) 166 | entries.file_names[++i] = ft_strdup(tmp->self->display_name); 167 | else 168 | entries.file_names[++i] = ft_strdup(tmp->self->name); 169 | } 170 | tmp = tmp->next; 171 | } 172 | if (file_count) 173 | column_display(entries, file_count, max_file_len, IS_NOTDIR); 174 | free(entries.file_names); 175 | if (file_count && should_separate) 176 | print_handler(1, "\n", 0, NULL); 177 | } 178 | 179 | void nondir_display(t_dirs *dirs, t_flags flags) { 180 | t_dirs *tmp; 181 | int should_separate; 182 | t_format nondir_format; 183 | 184 | should_separate = has_dirs(dirs); 185 | if (flags & COLUMN_DISPLAY) 186 | return (nondir_column_display(dirs, should_separate)); 187 | nondir_format = get_nondir_format(&dirs, flags); 188 | tmp = dirs; 189 | while (tmp) 190 | { 191 | if (tmp->status == IS_NOTDIR) 192 | { 193 | if (flags & LONG_LISTING_FLAG) 194 | long_listing_display(nondir_format, tmp->self, tmp->has_chr_or_blk, flags); 195 | else 196 | { 197 | if (tmp->self->has_nonprintable_chars) 198 | print_handler(1, "%s\n", 0, tmp->self->display_name); 199 | else 200 | print_handler(1, "%s\n", 0, tmp->self->name); 201 | } 202 | if (is_last_nondir(tmp) && should_separate) 203 | print_handler(1, "\n", 0, NULL); 204 | } 205 | tmp = tmp->next; 206 | } 207 | } 208 | 209 | void dir_display(t_dirs *head, t_dirs *dirs, t_flags flags) { 210 | t_entries entries; 211 | t_etarget target; 212 | 213 | if (head->next) 214 | print_handler(1, "%s:\n", 0, dirs->name); 215 | if (dirs->is_unreadable) 216 | { 217 | MEMCHECK((target.file = ft_strdup(dirs->display_name))); 218 | return (error_handler(FILE_ACCESS_ERR, target)); 219 | free(target.file); 220 | } 221 | if ((flags & LONG_LISTING_FLAG) && dirs->files && dirs->has_valid_files) 222 | print_handler(1, "total %d\n", 0, ft_itoa(dirs->total_blocks)); 223 | if (flags & COLUMN_DISPLAY) 224 | { 225 | entries.files = dirs->files; 226 | if (dirs->file_count) 227 | return (column_display(entries, dirs->file_count, dirs->max_file_len, IS_DIR)); 228 | } 229 | while (dirs->files) 230 | { 231 | if (dirs->files->status == IS_NONEXISTENT) { 232 | if (dirs->files->has_nonprintable_chars) { 233 | MEMCHECK((target.file = ft_strdup(dirs->files->display_name))); 234 | } 235 | else { 236 | MEMCHECK((target.file = ft_strdup(dirs->files->name))); 237 | } 238 | error_handler(NONEXISTENT_ERR, target); 239 | free(target.file); 240 | } 241 | else if (dirs->files->status == IS_UNREADABLE) { 242 | if (dirs->files->has_nonprintable_chars) { 243 | MEMCHECK((target.file = ft_strdup(dirs->files->display_name))); 244 | } 245 | else { 246 | MEMCHECK((target.file = ft_strdup(dirs->files->name))); 247 | } 248 | error_handler(FILE_ACCESS_ERR, target); 249 | free(target.file); 250 | } 251 | else { 252 | if (flags & LONG_LISTING_FLAG) 253 | long_listing_display(dirs->format, dirs->files, dirs->has_chr_or_blk, flags); 254 | else 255 | { 256 | if (dirs->files->has_nonprintable_chars) 257 | print_handler(1, "%s\n", 0, dirs->files->display_name); 258 | else 259 | print_handler(1, "%s\n", 0, dirs->files->name); 260 | } 261 | } 262 | dirs->files = dirs->files->next; 263 | } 264 | } 265 | 266 | void display_handler(t_dirs *head, t_dirs *dirs, t_flags flags, int target) { 267 | t_etarget etarget; 268 | t_dirs *tmp; 269 | 270 | if (target == IS_NONEXISTENT) 271 | { 272 | tmp = dirs; 273 | while (tmp) { 274 | if (tmp->status == IS_NONEXISTENT) 275 | { 276 | MEMCHECK((etarget.file = ft_strdup(tmp->name))); 277 | error_handler(NONEXISTENT_ERR, etarget); 278 | memory_handler(etarget.file, ERROR_MEM); 279 | } 280 | tmp = tmp->next; 281 | } 282 | } 283 | else if (target == IS_NOTDIR) 284 | nondir_display(dirs, flags); 285 | else 286 | dir_display(head, dirs, flags); 287 | } 288 | 289 | void ft_display(t_dirs *dirs, t_flags flags) 290 | { 291 | t_dirs *tmp; 292 | 293 | display_handler(NULL, dirs, flags, IS_NONEXISTENT); 294 | if (flags & REVERSE_FLAG) 295 | reverse_dirs(&dirs); 296 | display_handler(NULL, dirs, flags, IS_NOTDIR); 297 | tmp = dirs; 298 | while (tmp) 299 | { 300 | if (tmp->status == IS_DIR) 301 | { 302 | tmp->files = file_handler(tmp, flags); 303 | if (flags & REVERSE_FLAG) 304 | reverse_files(&tmp->files); 305 | display_handler(dirs, tmp, flags, IS_DIR); 306 | tmp->next = subdir_handler(tmp->next, &(tmp->sub_dirs), flags); 307 | if (!is_last_dir(tmp)) 308 | print_handler(1, "\n", 0, NULL); 309 | } 310 | tmp = tmp->next; 311 | } 312 | } -------------------------------------------------------------------------------- /src/error_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void error_handler(int err, t_etarget target) 4 | { 5 | if (err == FLAG_ERR) { 6 | print_handler(2, "ls: illegal option -- %c\n", 0, &target.flag); 7 | print_handler(2, "usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]\n", 0, NULL); 8 | print_handler(2, "Try 'ls --help' for more information.\n", 0, NULL); 9 | exit(1); 10 | } 11 | else if (err == NONEXISTENT_ERR) 12 | print_handler(2, "ls: %s: No such file or directory\n", 0, target.file); 13 | else 14 | print_handler(2, "ls: %s: Permission denied\n", 0, target.file); 15 | } 16 | -------------------------------------------------------------------------------- /src/file_handlers.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* file_handlers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/03/27 13:40:08 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/18 04:29:44 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_ls.h" 14 | #include 15 | 16 | void file_date_handler(t_date *date, struct stat f, t_flags flags) { 17 | char buff[200]; 18 | unsigned long long t; 19 | 20 | date->mtv_sec = (unsigned long long)f.st_mtimespec.tv_sec; 21 | date->mtv_nsec = (unsigned long long)f.st_mtimespec.tv_nsec; 22 | date->atv_sec = (unsigned long long)f.st_atimespec.tv_sec; 23 | date->atv_nsec = (unsigned long long)f.st_atimespec.tv_nsec; 24 | date->ctv_sec = (unsigned long long)f.st_ctimespec.tv_sec; 25 | date->ctv_nsec = (unsigned long long)f.st_ctimespec.tv_nsec; 26 | date->birthtv_sec = (unsigned long long)f.st_birthtimespec.tv_sec; 27 | date->birthtv_nsec = (unsigned long long)f.st_birthtimespec.tv_nsec; 28 | t = date->mtv_sec; 29 | if (flags & CREATION_DATE_SORT) 30 | t = date->birthtv_sec; 31 | if (flags & LAST_ACCESS_DATE_SORT) 32 | t = date->atv_sec; 33 | if (flags & LAST_STATUS_CHANGE_SORT) 34 | t = date->ctv_sec; 35 | strftime(buff, 200, "%b", localtime((const long *)&t)); 36 | MEMCHECK((date->month = ft_strdup(buff))); 37 | strftime(buff, 200, "%-d", localtime((const long *)&t)); 38 | MEMCHECK((date->day = ft_strdup(buff))); 39 | strftime(buff, 200, "%H", localtime((const long *)&t)); 40 | MEMCHECK((date->hour = ft_strdup(buff))); 41 | strftime(buff, 200, "%M", localtime((const long *)&t)); 42 | MEMCHECK((date->minute = ft_strdup(buff))); 43 | strftime(buff, 200, "%Y", localtime((const long *)&t)); 44 | MEMCHECK((date->year = ft_strdup(buff))); 45 | } 46 | 47 | char extended_attributes_handler(char *file_path) 48 | { 49 | char res; 50 | acl_t acl; 51 | acl_entry_t entry; 52 | 53 | res = ' '; 54 | if (listxattr(file_path, NULL, 0, XATTR_NOFOLLOW) == -1) 55 | { 56 | if (errno == EPERM || errno == EACCES || errno == EFAULT) 57 | return (' '); 58 | } 59 | else if (listxattr(file_path, NULL, 0, XATTR_NOFOLLOW) > 0) 60 | return ('@'); 61 | acl = acl_get_link_np(file_path, ACL_TYPE_EXTENDED); 62 | if (!acl && acl_get_entry(acl, ACL_FIRST_ENTRY, &entry) == -1) 63 | { 64 | acl_free(acl); 65 | acl = NULL; 66 | } 67 | if (acl) 68 | res = '+'; 69 | return (res); 70 | } 71 | 72 | char *serialize_file_name(char *name, int len) 73 | { 74 | char *new; 75 | int i; 76 | char c; 77 | 78 | MEMCHECK((new = ft_strnew(ft_strlen(name)))); 79 | i = -1; 80 | while (i < len && name[i]) { 81 | c = name[i]; 82 | if (IS_NONPRINTABLE(name[i])) 83 | { 84 | if (name[i] == '\r') 85 | c = '?'; 86 | else 87 | c = ' '; 88 | } 89 | new[i] = c; 90 | } 91 | return (new); 92 | } 93 | 94 | int has_nonprintable_chars(char *s, int len) 95 | { 96 | int i; 97 | 98 | i = -1; 99 | while (++i < len && s[i]) 100 | if (IS_NONPRINTABLE(s[i])) 101 | return (1); 102 | return (0); 103 | } 104 | 105 | void file_permission_handler(t_files **curr_file, char *file_path, struct stat f) 106 | { 107 | (*curr_file)->modes[0] = get_file_entry_type(f.st_mode); 108 | (*curr_file)->modes[1] = (f.st_mode & S_IRUSR) ? 'r' : '-'; 109 | (*curr_file)->modes[2] = (f.st_mode & S_IWUSR) ? 'w' : '-'; 110 | (*curr_file)->modes[3] = third_permission_mode_handler(f.st_mode, ISUSR); 111 | (*curr_file)->modes[4] = (f.st_mode & S_IRGRP) ? 'r' : '-'; 112 | (*curr_file)->modes[5] = (f.st_mode & S_IWGRP) ? 'w' : '-'; 113 | (*curr_file)->modes[6] = third_permission_mode_handler(f.st_mode, ISGRP); 114 | (*curr_file)->modes[7] = (f.st_mode & S_IROTH) ? 'r' : '-'; 115 | (*curr_file)->modes[8] = (f.st_mode & S_IWOTH) ? 'w' : '-'; 116 | (*curr_file)->modes[9] = third_permission_mode_handler(f.st_mode, ISOTH); 117 | if ((*curr_file)->modes[1] != '-') 118 | (*curr_file)->modes[10] = extended_attributes_handler(file_path); 119 | else 120 | (*curr_file)->modes[10] = ' '; 121 | } 122 | 123 | void get_file_info(t_files **curr_file, t_dirs **dirs, char *file_path, int format_option, t_flags flags) 124 | { 125 | char buff[256]; 126 | struct stat f; 127 | 128 | f = (*curr_file)->f; 129 | file_permission_handler(curr_file, file_path, f); 130 | (*curr_file)->link = f.st_nlink; 131 | (*curr_file)->owner = getpwuid(f.st_uid) ? ft_strdup(getpwuid(f.st_uid)->pw_name) : NULL; 132 | (*curr_file)->group = getgrgid(f.st_gid) ? ft_strdup(getgrgid(f.st_gid)->gr_name) : NULL; 133 | (*curr_file)->user_id = (int)f.st_uid; 134 | (*curr_file)->group_id = (int)f.st_gid; 135 | (*curr_file)->size = f.st_size; 136 | if (S_ISCHR(f.st_mode) || S_ISBLK(f.st_mode)) 137 | { 138 | (*curr_file)->major = (long)major(f.st_rdev); 139 | (*curr_file)->minor = (long)minor(f.st_rdev); 140 | (*curr_file)->is_chr_or_blk = 1; 141 | if (!(*dirs)->has_chr_or_blk) 142 | (*dirs)->has_chr_or_blk = 1; 143 | } 144 | if (S_ISLNK(f.st_mode)) 145 | { 146 | (*curr_file)->is_link = 1; 147 | int link_len = 0; 148 | if ((link_len = readlink(file_path, buff, 256)) == -1) 149 | exit(2); 150 | if (has_nonprintable_chars(buff, link_len)) { 151 | MEMCHECK(((*curr_file)->linked_to = serialize_file_name(buff, link_len))); 152 | } 153 | else { 154 | MEMCHECK(((*curr_file)->linked_to = ft_strndup(buff, link_len))); 155 | } 156 | } 157 | file_date_handler(&((*curr_file)->date), f, flags); 158 | format_handler(&(*dirs)->format, *curr_file, format_option); 159 | } 160 | 161 | void add_file(t_files **curr_file, t_dirs **dirs, t_flags flags, int format_option) 162 | { 163 | char *dir_name; 164 | char *file_path; 165 | int file_name_len; 166 | 167 | dir_name = (*dirs)->name; 168 | file_path = (*curr_file)->is_dir_info ? (*curr_file)->name : ft_pathjoin(dir_name, (*curr_file)->name); 169 | if (lstat(file_path, &(*curr_file)->f) < 0) 170 | { 171 | if (errno == ENOENT) 172 | (*curr_file)->status = IS_NONEXISTENT; 173 | else if (errno == EACCES) 174 | (*curr_file)->status = IS_UNREADABLE; 175 | return ; 176 | } 177 | (*dirs)->has_valid_files = 1; 178 | MEMCHECK(((*curr_file)->modes = ft_strnew(11))); 179 | get_file_info(curr_file, dirs, file_path, format_option, flags); 180 | if ((flags & LONG_LISTING_FLAG) && !(*curr_file)->is_dir_info) 181 | { 182 | if ((*dirs)->status == IS_DIR) 183 | (*dirs)->total_blocks += (*curr_file)->f.st_blocks; 184 | } 185 | else 186 | { 187 | (*dirs)->file_count++; 188 | file_name_len = ft_strlen((*curr_file)->name); 189 | if (file_name_len > (*dirs)->max_file_len) 190 | (*dirs)->max_file_len = file_name_len; 191 | } 192 | if (S_ISDIR((*curr_file)->f.st_mode) && (flags & RECURSIVE_FLAG) && !ft_strequ((*curr_file)->name, "..") && !ft_strequ((*curr_file)->name, ".")) { 193 | set_dir(ft_pathjoin(dir_name, (*curr_file)->name), &((*dirs)->sub_dirs), (*curr_file)->name, flags); 194 | } 195 | } 196 | 197 | // Make this a libft function 198 | char *get_entry_name(char *path) 199 | { 200 | char **parts; 201 | 202 | MEMCHECK((parts = ft_strsplit(path, '/'))); 203 | int i = -1; 204 | while (parts[++i]) 205 | { 206 | if (!parts[i + 1]) 207 | return (parts[i]); 208 | } 209 | return (path); 210 | } 211 | 212 | t_files *file_handler(t_dirs *dirs, t_flags flags) { 213 | DIR *dir; 214 | struct dirent *sd; 215 | t_files *files; 216 | t_files **tmp; 217 | int format_option; 218 | char *file_name; 219 | 220 | if (!(dir = opendir(dirs->name))) 221 | { 222 | dirs->is_unreadable = 1; 223 | MEMCHECK((dirs->display_name = get_entry_name(dirs->name))); 224 | return (NULL); 225 | } 226 | files = NULL; 227 | tmp = &files; 228 | format_option = INIT_FORMAT; 229 | while ((sd = readdir(dir))) 230 | { 231 | if (flags & HIDE_CURR_AND_PREV_DIRS && !(flags & ALL_FLAG) && (ft_strequ(sd->d_name, ".") || ft_strequ(sd->d_name, ".."))) 232 | continue ; 233 | if (!(flags & ALL_FLAG) && !(flags & HIDE_CURR_AND_PREV_DIRS) && sd->d_name[0] == '.') 234 | continue ; 235 | MEMCHECK(((*tmp = (t_files *)ft_memalloc(sizeof(t_files))))); 236 | file_name = sd->d_name; 237 | if (has_nonprintable_chars(sd->d_name, ft_strlen(sd->d_name))) { 238 | (*tmp)->display_name = serialize_file_name(sd->d_name, ft_strlen(sd->d_name)); 239 | (*tmp)->has_nonprintable_chars = 1; 240 | } 241 | (*tmp)->name = ft_strdup(sd->d_name); 242 | add_file(tmp, &dirs, flags, format_option); 243 | format_option = UPDATE_FORMAT; 244 | tmp = &((*tmp)->next); 245 | } 246 | closedir(dir); 247 | file_sort(&files, flags); 248 | return (files); 249 | } 250 | -------------------------------------------------------------------------------- /src/file_handlers_2.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void move_file(t_files **destRef, t_files **sourceRef) 4 | { 5 | t_files *new; 6 | 7 | new = *sourceRef; 8 | *sourceRef = (*sourceRef)->next; 9 | new->next = *destRef; 10 | *destRef = new; 11 | } 12 | 13 | void handle_file_merge_comparison(t_files **a, t_files **b, t_files **tmp, t_flags flags) 14 | { 15 | int comparison; 16 | 17 | // Try to do '<'' instead of '<=' to see if it would improve speed 18 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 19 | if (flags & MODIFICATION_DATE_SORT) 20 | { 21 | comparison = (*a)->date.mtv_sec >= (*b)->date.mtv_sec; 22 | if ((*a)->date.mtv_sec == (*b)->date.mtv_sec) 23 | { 24 | comparison = (*a)->date.mtv_nsec >= (*b)->date.mtv_nsec; 25 | if ((*a)->date.mtv_nsec == (*b)->date.mtv_nsec) 26 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 27 | } 28 | if (flags & CREATION_DATE_SORT) 29 | { 30 | comparison = (*a)->date.birthtv_sec >= (*b)->date.birthtv_sec; 31 | if ((*a)->date.birthtv_sec == (*b)->date.birthtv_sec) 32 | { 33 | comparison = (*a)->date.birthtv_nsec >= (*b)->date.birthtv_nsec; 34 | if ((*a)->date.birthtv_nsec == (*b)->date.birthtv_nsec) 35 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 36 | } 37 | } 38 | else if (flags & LAST_ACCESS_DATE_SORT) 39 | { 40 | comparison = (*a)->date.atv_sec >= (*b)->date.atv_sec; 41 | if ((*a)->date.atv_sec == (*b)->date.atv_sec) 42 | { 43 | comparison = (*a)->date.atv_nsec >= (*b)->date.atv_nsec; 44 | if ((*a)->date.atv_nsec == (*b)->date.atv_nsec) 45 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 46 | } 47 | } 48 | else if (flags & LAST_STATUS_CHANGE_SORT) 49 | { 50 | comparison = (*a)->date.ctv_sec >= (*b)->date.ctv_sec; 51 | if ((*a)->date.ctv_sec == (*b)->date.ctv_sec) 52 | { 53 | comparison = (*a)->date.ctv_nsec >= (*b)->date.ctv_nsec; 54 | if ((*a)->date.ctv_nsec == (*b)->date.ctv_nsec) 55 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 56 | } 57 | } 58 | } 59 | else if (flags & FILE_SIZE_SORT) 60 | { 61 | comparison = (*a)->size >= (*b)->size; 62 | if ((*a)->size == (*b)->size) 63 | comparison = ft_strcmp((*a)->name, (*b)->name) <= 0; 64 | } 65 | move_file(tmp, comparison ? a : b); 66 | } 67 | 68 | t_files *merge_splitted_files(t_files *a, t_files *b, t_flags flags) 69 | { 70 | t_files *res; 71 | t_files **tmp; 72 | 73 | res = NULL; 74 | tmp = &res; 75 | while (1) 76 | { 77 | if (!a) 78 | { 79 | *tmp = b; 80 | break; 81 | } 82 | else if (!b) 83 | { 84 | *tmp = a; 85 | break; 86 | } 87 | handle_file_merge_comparison(&a, &b, tmp, flags); 88 | tmp = &((*tmp)->next); 89 | } 90 | return (res); 91 | } 92 | 93 | void split_file(t_files *sourceRef, t_files **frontRef, t_files **backRef) 94 | { 95 | t_files *slow; 96 | t_files *fast; 97 | 98 | slow = sourceRef; 99 | fast = sourceRef->next; 100 | while (fast) 101 | { 102 | fast = fast->next; 103 | if (fast) 104 | { 105 | slow = slow->next; 106 | fast = fast->next; 107 | } 108 | } 109 | *frontRef = sourceRef; 110 | *backRef = slow->next; 111 | slow->next = NULL; 112 | } 113 | 114 | void file_sort(t_files **files, t_flags flags) 115 | { 116 | t_files *head; 117 | t_files *a; 118 | t_files *b; 119 | 120 | head = *files; 121 | if (!head || !head->next) 122 | return ; 123 | split_file(head, &a, &b); 124 | file_sort(&a, flags); 125 | file_sort(&b, flags); 126 | *files = merge_splitted_files(a, b, flags); 127 | } 128 | -------------------------------------------------------------------------------- /src/file_handlers_3.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | char get_file_entry_type(int mode) 4 | { 5 | if (S_ISBLK(mode)) 6 | return ('b'); 7 | else if (S_ISCHR(mode)) 8 | return ('c'); 9 | else if (S_ISDIR(mode)) 10 | return ('d'); 11 | else if (S_ISLNK(mode)) 12 | return ('l'); 13 | else if (S_ISSOCK(mode)) 14 | return ('s'); 15 | else if (S_ISFIFO(mode)) 16 | return ('p'); 17 | else 18 | return ('-'); 19 | } 20 | 21 | void reverse_files(t_files **files) 22 | { 23 | t_files *curr; 24 | t_files *next; 25 | t_files *prev; 26 | 27 | prev = NULL; 28 | curr = *files; 29 | while (curr) 30 | { 31 | next = curr->next; 32 | curr->next = prev; 33 | prev = curr; 34 | curr = next; 35 | } 36 | *files = prev; 37 | } 38 | 39 | char get_correct_character(int mode, int isExec, int isSticky) 40 | { 41 | if (mode & isExec) 42 | { 43 | if (mode & isSticky) 44 | return ('s'); 45 | else 46 | return ('x'); 47 | } 48 | else 49 | { 50 | if (mode & isSticky) 51 | return ('S'); 52 | else 53 | return ('-'); 54 | } 55 | } 56 | 57 | char third_permission_mode_handler(int mode, int userType) 58 | { 59 | if (userType == ISUSR) 60 | return get_correct_character(mode, S_IXUSR, S_ISUID); 61 | else if (userType == ISGRP) 62 | return get_correct_character(mode, S_IXGRP, S_ISGID); 63 | else 64 | { 65 | if (mode & S_IXOTH) 66 | { 67 | if (mode & S_ISTXT) 68 | return ('t'); 69 | else 70 | return ('x'); 71 | } 72 | else 73 | { 74 | if (mode & S_ISTXT) 75 | return ('T'); 76 | else 77 | return ('-'); 78 | } 79 | } 80 | } -------------------------------------------------------------------------------- /src/flag_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void long_option_flag(char *option, t_flags *flags) { 4 | t_etarget target; 5 | 6 | if (ft_strequ(option, "help")) 7 | help_handler(); 8 | else if (ft_strequ(option, "recursive")) 9 | *flags |= RECURSIVE_FLAG; 10 | else if (ft_strequ(option, "all")) 11 | *flags |= ALL_FLAG; 12 | else if (ft_strequ(option, "reverse")) 13 | *flags |= REVERSE_FLAG; 14 | else { 15 | target.flag = '-'; 16 | error_handler(FLAG_ERR, target); 17 | } 18 | } 19 | 20 | void display_flag_handler(t_flags *flags, char f) 21 | { 22 | if (f == '1') 23 | { 24 | if (*flags & COLUMN_DISPLAY) 25 | *flags &= ~COLUMN_DISPLAY; 26 | if (*flags & LONG_LISTING_FLAG) 27 | *flags &= ~LONG_LISTING_FLAG; 28 | *flags |= ONE_ENTRY_PER_LINE; 29 | } 30 | else if (f == 'l' || f == 'g') 31 | { 32 | if (f == 'g') 33 | *flags |= SUPRESS_OWNER; 34 | if (*flags & COLUMN_DISPLAY) 35 | *flags &= ~COLUMN_DISPLAY; 36 | if (*flags & ONE_ENTRY_PER_LINE) 37 | *flags &= ~ONE_ENTRY_PER_LINE; 38 | *flags |= LONG_LISTING_FLAG; 39 | } 40 | else 41 | { 42 | if (*flags & ONE_ENTRY_PER_LINE) 43 | *flags &= ~ONE_ENTRY_PER_LINE; 44 | if (*flags & LONG_LISTING_FLAG) 45 | *flags &= ~LONG_LISTING_FLAG; 46 | *flags |= COLUMN_DISPLAY; 47 | } 48 | } 49 | 50 | void sort_flag_handler(t_flags *flags, char f) 51 | { 52 | if (f == 't' && !(*flags & FILE_SIZE_SORT)) 53 | *flags |= MODIFICATION_DATE_SORT; 54 | else if (f == 'U') 55 | { 56 | if (*flags & LAST_ACCESS_DATE_SORT) 57 | *flags &= ~LAST_ACCESS_DATE_SORT; 58 | if (*flags & LAST_STATUS_CHANGE_SORT) 59 | *flags &= ~LAST_STATUS_CHANGE_SORT; 60 | *flags |= CREATION_DATE_SORT; 61 | } 62 | else if (f == 'u') 63 | { 64 | if (*flags & CREATION_DATE_SORT) 65 | *flags &= ~CREATION_DATE_SORT; 66 | if (*flags & LAST_STATUS_CHANGE_SORT) 67 | *flags &= ~LAST_STATUS_CHANGE_SORT; 68 | *flags |= LAST_ACCESS_DATE_SORT; 69 | } 70 | else if (f == 'c') 71 | { 72 | if (*flags & CREATION_DATE_SORT) 73 | *flags &= ~CREATION_DATE_SORT; 74 | if (*flags & LAST_ACCESS_DATE_SORT) 75 | *flags &= ~LAST_ACCESS_DATE_SORT; 76 | *flags |= LAST_STATUS_CHANGE_SORT; 77 | } 78 | else 79 | { 80 | if (*flags & MODIFICATION_DATE_SORT) 81 | *flags &= ~MODIFICATION_DATE_SORT; 82 | *flags |= FILE_SIZE_SORT; 83 | } 84 | } 85 | 86 | void set_flag(char *arg, t_flags *flags) { 87 | int i; 88 | t_etarget target; 89 | 90 | if (ft_strstartswith(arg, "--") && arg[2]) 91 | return long_option_flag(arg + 2, flags); 92 | i = 0; 93 | while (arg[++i]) { 94 | if (arg[i] == 'R') 95 | *flags |= RECURSIVE_FLAG; 96 | else if (arg[i] == 'a') 97 | *flags |= ALL_FLAG; 98 | else if (arg[i] == 'r') 99 | *flags |= REVERSE_FLAG; 100 | else if (arg[i] == 'n') 101 | *flags |= DISPLAY_UID_AND_GID; 102 | else if (arg[i] == 'A') 103 | *flags |= HIDE_CURR_AND_PREV_DIRS; 104 | else if (arg[i] == 'G') 105 | *flags |= COLORED_OUTPUT; 106 | else if (arg[i] == 'U' || arg[i] == 't' || arg[i] == 'u' || arg[i] == 'c' || arg[i] == 'S') 107 | sort_flag_handler(flags, arg[i]); 108 | else if (arg[i] == '1' || arg[i] == 'l' || arg[i] == 'C' || arg[i] == 'g') 109 | display_flag_handler(flags, arg[i]); 110 | else { 111 | target.flag = arg[i]; 112 | error_handler(FLAG_ERR, target); 113 | } 114 | } 115 | } 116 | 117 | int flag_handler(char **args, t_flags *flags) { 118 | int i; 119 | 120 | i = -1; 121 | *flags |= COLUMN_DISPLAY; 122 | while (args[++i]) { 123 | if (args[i][0] != '-') 124 | break; 125 | if (ft_strequ(args[i], "--")) { 126 | i++; 127 | break; 128 | } 129 | if (args[i][0] == '-' && args[i][1]) 130 | set_flag(args[i], flags); 131 | else 132 | break; 133 | } 134 | return (i > 0 ? i + 1 : 1); 135 | } 136 | -------------------------------------------------------------------------------- /src/format_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void initialize_format(t_format *format) 4 | { 5 | format->date_month = 3; 6 | format->date_day = 2; 7 | format->date_hour = 2; 8 | format->date_minute = 2; 9 | format->date_year = 5; 10 | format->link = 0; 11 | format->owner = 0; 12 | format->group = 0; 13 | format->fileSize = 0; 14 | format->user_id = 0; 15 | format->group_id = 0; 16 | format->major = 0; 17 | format->minor = 0; 18 | } 19 | 20 | void format_handler(t_format *format, t_files *file, int format_option) { 21 | if (format_option == IDLE_FORMAT) 22 | return ; 23 | if (format_option == INIT_FORMAT) 24 | initialize_format(format); 25 | if (format->link < ft_intlen(file->link)) 26 | format->link = ft_intlen(file->link); 27 | if (format->owner < (int)ft_strlen(file->owner)) 28 | format->owner = (int)ft_strlen(file->owner); 29 | if (format->group < (int)ft_strlen(file->group)) 30 | format->group = (int)ft_strlen(file->group); 31 | if (format->fileSize < ft_intlen(file->size)) 32 | format->fileSize = ft_intlen(file->size); 33 | if (format->user_id < ft_intlen(file->user_id)) 34 | format->user_id = ft_intlen(file->user_id); 35 | if (format->group_id < ft_intlen(file->group_id)) 36 | format->group_id = ft_intlen(file->group_id); 37 | if (format->major < ft_intlen(file->major)) 38 | format->major = ft_intlen(file->major); 39 | if (format->minor < ft_intlen(file->minor)) 40 | format->minor = ft_intlen(file->minor); 41 | } 42 | -------------------------------------------------------------------------------- /src/help_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void usage_help() 4 | { 5 | print_handler(1, "Usage: ./ft_ls [OPTION]... [FILE]...\n", 0, NULL); 6 | print_handler(1, "List information about the FILEs (the current directory by default).\n", 0, NULL); 7 | print_handler(1, "Sort entries alphabetically if -t is not specified.\n", 0, NULL); 8 | } 9 | 10 | void arguments_help() 11 | { 12 | print_handler(1, "Mandatory arguments to long options are mandatory for short options too.\n", 0, NULL); 13 | print_handler(1, " -l\t\t\t\t\t\tuse a long listing format\n", 0, NULL); 14 | print_handler(1, " -R, --recursive\tlist subdirectories recursively\n", 0, NULL); 15 | print_handler(1, " -a, --all\t\t\t\tdo not ignore entries starting with .\n", 0, NULL); 16 | print_handler(1, " -r, --reverse\t\treverse order while sorting\n", 0, NULL); 17 | print_handler(1, " -t\t\t\t\t\t\tsort by modification time, newest first\n", 0, NULL); 18 | } 19 | 20 | void exits_help() 21 | { 22 | print_handler(1, "Exit status:\n", 0, NULL); 23 | print_handler(1, " 0 if OK,\n", 0, NULL); 24 | print_handler(1, " 1 if minor problems (e.g., cannot access subdirectory),\n", 0, NULL); 25 | print_handler(1, " 2 if serious trouble (e.g., cannot access command-line argument).\n", 0, NULL); 26 | } 27 | 28 | void help_handler(void) 29 | { 30 | usage_help(); 31 | print_handler(1, "\n", 0, NULL); 32 | arguments_help(); 33 | print_handler(1, "\n", 0, NULL); 34 | exits_help(); 35 | exit(0); 36 | } 37 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/14 17:33:27 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/15 05:32:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int main(int ac, char **av) 16 | { 17 | t_flags flags; 18 | int i; 19 | t_dirs *dirs; 20 | 21 | flags = 0; 22 | (void)ac; 23 | i = flag_handler(av + 1, &flags); 24 | dirs = dir_handler(av + i, flags); 25 | ft_display(dirs, flags); 26 | memory_handler(&dirs, DIRS_MEM); 27 | exit(0); 28 | } 29 | -------------------------------------------------------------------------------- /src/memory_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void free_files(t_files **files) 4 | { 5 | t_files *next; 6 | 7 | while (*files) 8 | { 9 | next = (*files)->next; 10 | free((*files)->name); 11 | free((*files)->owner); 12 | free((*files)->group); 13 | free((*files)->modes); 14 | free((*files)->date.month); 15 | free((*files)->date.day); 16 | free((*files)->date.hour); 17 | free((*files)->date.minute); 18 | free((*files)->date.year); 19 | if ((*files)->has_nonprintable_chars) 20 | free((*files)->display_name); 21 | ft_memdel((void *)files); 22 | *files = next; 23 | } 24 | } 25 | 26 | void free_dirs(t_dirs **dirs) 27 | { 28 | t_dirs *next; 29 | 30 | while (*dirs) 31 | { 32 | next = (*dirs)->next; 33 | free((*dirs)->name); 34 | free((*dirs)->display_name); 35 | free_files(&((*dirs)->files)); 36 | free_files(&((*dirs)->self)); 37 | ft_memdel((void *)dirs); 38 | *dirs = next; 39 | } 40 | } 41 | 42 | 43 | void memory_handler(void *mem_target, int target) 44 | { 45 | if (target == DIRS_MEM) 46 | free_dirs((t_dirs **)mem_target); 47 | else if (target == ERROR_MEM) 48 | free(mem_target); 49 | } 50 | -------------------------------------------------------------------------------- /src/print_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | void print_handler(int fd, char *str, int format, char *target) 4 | { 5 | char *new; 6 | char *tmp; 7 | 8 | if (!format && !target) 9 | return (ft_putstr_fd(str, fd)); 10 | MEMCHECK((new = ft_strnew(format + ft_strlen(str) + ft_strlen(target) - 2))); 11 | int len = (int)ft_strlen(target); 12 | int i = -1; 13 | while (str[++i]) 14 | { 15 | if (str[i] == '%') 16 | { 17 | while (len++ < format) { 18 | tmp = new; 19 | new = ft_strjoinch(new, ' '); 20 | free(tmp); 21 | } 22 | tmp = new; 23 | MEMCHECK((new = ft_strjoin(new, target))); 24 | free(tmp); 25 | i += (str[i + 1] == 'd' || str[i + 1] == 's') ? 1 : 2; 26 | } 27 | else { 28 | tmp = new; 29 | MEMCHECK((new = ft_strjoinch(new, str[i]))); 30 | free(tmp); 31 | } 32 | } 33 | ft_putstr_fd(new, fd); 34 | free(new); 35 | } 36 | 37 | void lprint_handler(int fd, char *str, int format, char *target) 38 | { 39 | char *new; 40 | char *tmp; 41 | 42 | if (!format && !target) 43 | return (ft_putstr_fd(str, fd)); 44 | MEMCHECK((new = ft_strnew(format + ft_strlen(str) + ft_strlen(target) - 2))); 45 | int len = (int)ft_strlen(target); 46 | int i = -1; 47 | while (str[++i]) 48 | { 49 | if (str[i] == '%') 50 | { 51 | tmp = new; 52 | MEMCHECK((new = ft_strjoin(new, target))); 53 | free(tmp); 54 | while (len++ < format) { 55 | tmp = new; 56 | new = ft_strjoinch(new, ' '); 57 | free(tmp); 58 | } 59 | i += (str[i + 1] == 'd' || str[i + 1] == 's') ? 1 : 2; 60 | } 61 | else { 62 | tmp = new; 63 | MEMCHECK((new = ft_strjoinch(new, str[i]))); 64 | free(tmp); 65 | } 66 | } 67 | ft_putstr_fd(new, fd); 68 | free(new); 69 | } -------------------------------------------------------------------------------- /src/subdir_handlers.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | t_dirs *subdir_handler(t_dirs *next, t_dirs **sub_dirs, t_flags flags) 4 | { 5 | t_dirs *tmp; 6 | 7 | if (!*sub_dirs) 8 | return (next); 9 | dir_sort(sub_dirs, flags); 10 | if (flags & REVERSE_FLAG) 11 | reverse_dirs(sub_dirs); 12 | tmp = *sub_dirs; 13 | while (tmp->next) { 14 | tmp = tmp->next; 15 | } 16 | tmp->next = next; 17 | return (*sub_dirs); 18 | } 19 | -------------------------------------------------------------------------------- /src/utils.c: -------------------------------------------------------------------------------- 1 | #include "ft_ls.h" 2 | 3 | int is_last_dir(t_dirs *dirs) 4 | { 5 | t_dirs *tmp; 6 | 7 | tmp = dirs->next; 8 | while (tmp) 9 | { 10 | if (tmp->status == IS_DIR) 11 | return (0); 12 | tmp = tmp->next; 13 | } 14 | return (1); 15 | } 16 | 17 | int is_last_nondir(t_dirs *dirs) 18 | { 19 | t_dirs *tmp; 20 | 21 | tmp = dirs->next; 22 | while (tmp) 23 | { 24 | if (tmp->status == IS_NOTDIR) 25 | return (0); 26 | tmp = tmp->next; 27 | } 28 | return (1); 29 | } 30 | 31 | int has_dirs(t_dirs *dirs) 32 | { 33 | t_dirs *tmp; 34 | 35 | tmp = dirs; 36 | while (tmp) 37 | { 38 | if (tmp->status == IS_DIR) 39 | return (1); 40 | tmp = tmp->next; 41 | } 42 | return (0); 43 | } 44 | 45 | t_format get_nondir_format(t_dirs **dirs, t_flags flags) 46 | { 47 | t_format format; 48 | t_dirs *tmp; 49 | int format_option; 50 | 51 | tmp = *dirs; 52 | format_option = INIT_FORMAT; 53 | while (tmp) 54 | { 55 | if (tmp->status == IS_NOTDIR) 56 | { 57 | add_file(&tmp->self, &tmp, flags, INIT_FORMAT); 58 | if (flags & LONG_LISTING_FLAG) 59 | format_handler(&format, tmp->self, format_option); 60 | format_option = UPDATE_FORMAT; 61 | } 62 | tmp = tmp->next; 63 | } 64 | return (format); 65 | } --------------------------------------------------------------------------------