diff --git a/ls/.gitignore b/ls/.gitignore new file mode 100644 index 00000000..1e22e7bb --- /dev/null +++ b/ls/.gitignore @@ -0,0 +1,2 @@ +ft_ls +tests diff --git a/ls/Makefile b/ls/Makefile new file mode 100644 index 00000000..9b476997 --- /dev/null +++ b/ls/Makefile @@ -0,0 +1,45 @@ +NAME = ft_ls +CC = gcc + +D_SRC = src +F_SRC := $(shell ls -1 $(D_SRC) | grep "\.c$$") +F_SRC := ft_dir_get_ents.c ft_ent_free.c ft_ent_get_dirs.c ft_ent_has_dir.c ft_ls.c ft_ls_color.c ft_ls_long.c ft_ls_postname.c ft_ls_short.c lib_ent.c lib_error.c lib_ls_long.c lib_ls_long2.c lib_lsdata.c lib_parse.c lib_time.c main.c +DF_SRC := $(addprefix $(D_SRC)/, $(F_SRC)) + +D_OBJ = obj +F_OBJ = $(F_SRC:.c=.o) +DF_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ)) + +D_INC = includes libft/includes +O_INC = $(addprefix -I, $(D_INC)) + +W_FLAGS = -Wall -Wextra -Werror +D_FLAGS = + +MKDIR = mkdir -p +RM = /bin/rm -rf + +.PHONY: all clean fclean re + +all: $(NAME) + +$(D_OBJ)/%.o: $(D_SRC)/%.c $(D_INC) + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." + +libft/libft.a: + @$(MAKE) -C libft/ 2>/dev/null + +$(NAME): libft/libft.a $(DF_OBJ) + $(CC) $(O_INC) -Llibft -lft $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) + +clean: + $(RM) $(D_OBJ) + @$(MAKE) -C libft clean 2>/dev/null + +fclean: clean + $(RM) $(NAME) + @$(MAKE) -C libft fclean 2>/dev/null + +re: fclean all diff --git a/ls/README.md b/ls/README.md new file mode 100644 index 00000000..179fdbd4 --- /dev/null +++ b/ls/README.md @@ -0,0 +1,110 @@ +# ft_ls -- list directory contents + +## SYNOPSIS + ft_ls [-AFGRUSTacdfgloprtu1] [file ...] + +## DESCRIPTION + For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information. For each operand that names a file of type + directory, ls displays the names of files contained within that directory, as well as any requested, associated information. + + If no operands are given, the contents of the current directory are displayed. If more than one operand is given, non-directory operands are displayed first; directory and non- + directory operands are sorted separately and in lexicographical order. + + The following options are available: + + -1 (The numeric digit ``one''.) Force output to be one entry per line. This is the default when output is not to a terminal. + + -A List all entries except for . and ... Always set for the super-user. + + -a Include directory entries whose names begin with a dot (.). + + -d Directories are listed as plain files (not searched recursively). + + -F Display a slash (`/') immediately after each pathname that is a directory, an asterisk (`*') after each that is executable, an at sign (`@') after each symbolic link, an + equals sign (`=') after each socket, a percent sign (`%') after each whiteout, and a vertical bar (`|') after each that is a FIFO. + + -f Output is not sorted. This option turns on the -a option. + + -G Enable colorized output. This option is equivalent to defining CLICOLOR in the environment. (See below.) + + -g This option is only available for compatibility with POSIX; it is used to display the group name in the long (-l) format output (the owner name is suppressed). + + -l (The lowercase letter ``ell''.) List in long format. (See below.) If the output is to a terminal, a total sum for all the file sizes is output on a line before the long + listing. + + -o List in long format, but omit the group id. + + -p Write a slash (`/') after each filename if that file is a directory. + + -R Recursively list subdirectories encountered. + + -r Reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size + + -S Sort files by size + + -s Display the number of file system blocks actually used by each file, in units of 512 bytes, where partial units are rounded up to the next integer value. If the output is to + a terminal, a total sum for all the file sizes is output on a line before the listing. The environment variable BLOCKSIZE overrides the unit size of 512 bytes. + + -T When used with the -l (lowercase letter ``ell'') option, display complete time information for the file, including month, day, hour, minute, second, and year. + + -t Sort by time modified (most recently modified first) before sorting the operands by lexicographical order. + + -u Use time of last access, instead of last modification of the file for sorting (-t) or long printing (-l). + + -U Use time of file creation, instead of last modification for sorting (-t) or long output (-l). + +### The Long Format + If the -l option is given, the following information is displayed for each file: file mode, number of links, owner name, group name, number of bytes in the file, abbreviated month, + day-of-month file was last modified, hour file last modified, minute file last modified, and the pathname. In addition, for each directory whose contents are displayed, the total + number of 512-byte blocks used by the files in the directory is displayed on a line by itself, immediately before the information for the files in the directory. If the file or + directory has extended attributes, the permissions field printed by the -l option is followed by a '@' character. Otherwise, if the file or directory has extended security informa- + tion (such as an access control list), the permissions field printed by the -l option is followed by a '+' character. + + If the modification time of the file is more than 6 months in the past or future, then the year of the last modification is displayed in place of the hour and minute fields. + + If the owner or group names are not a known user or group name, or the -n option is given, the numeric ID's are displayed. + + If the file is a character special or block special file, the major and minor device numbers for the file are displayed in the size field. If the file is a symbolic link, the path- + name of the linked-to file is preceded by ``->''. + + The file mode printed under the -l option consists of the entry type, owner permissions, and group permissions. The entry type character describes the type of file, as follows: + + b Block special file. + c Character special file. + d Directory. + l Symbolic link. + s Socket link. + p FIFO. + - Regular file. + + The next three fields are three characters each: owner permissions, group permissions, and other permissions. Each field has three character positions: + + 1. If r, the file is readable; if -, it is not readable. + + 2. If w, the file is writable; if -, it is not writable. + + 3. The first of the following that applies: + + S If in the owner permissions, the file is not executable and set-user-ID mode is set. If in the group permissions, the file is not executable and set-group-ID + mode is set. + + s If in the owner permissions, the file is executable and set-user-ID mode is set. If in the group permissions, the file is executable and setgroup-ID mode is + set. + + x The file is executable or the directory is searchable. + + - The file is neither readable, writable, executable, nor set-user-ID nor set-group-ID mode, nor sticky. (See below.) + + These next two apply only to the third character in the last group (other permissions). + + T The sticky bit is set (mode 1000), but not execute or search permission. (See chmod(1) or sticky(8).) + + t The sticky bit is set (mode 1000), and is searchable or executable. (See chmod(1) or sticky(8).) + +## EXAMPLES + The following is how to do an ls listing sorted by increasing size + + ./ft_ls -lrS + +## DIAGNOSTICS + The ft_ls utility exits 0 on success, and >0 if an error occurs. diff --git a/ls/auteur b/ls/auteur new file mode 100644 index 00000000..b0e9923f --- /dev/null +++ b/ls/auteur @@ -0,0 +1 @@ +jhalford diff --git a/ls/includes/ft_ls.h b/ls/includes/ft_ls.h new file mode 100644 index 00000000..ef4475af --- /dev/null +++ b/ls/includes/ft_ls.h @@ -0,0 +1,137 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftls.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 15:10:03 by jhalford #+# #+# */ +/* Updated: 2016/11/27 13:09:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LS_H +# define FT_LS_H +# define LS_LEGAL_OPTS "AFGRUSTacdfgloprtu1" +# define LS_MSG_ILLEGAL_OPT "ft_ls: illegal option -- %c\n" +# define LS_MSG_USAGE "usage: ls [-%s] [file ...]\n" +# define LS_MSG_FILE_ERR "ls: %s: No such file or directory\n" +# define LS_MSG_ACC_ERR "ls: %s: Permission denied\n" + +# ifndef S_IXUGO +# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH) +# endif + +# define OPTS_LA 0x00000001 +# define OPTS_LC 0x00000002 +# define OPTS_LD 0x00000004 +# define OPTS_LF 0x00000008 +# define OPTS_LG 0x00000010 +# define OPTS_LL 0x00000020 +# define OPTS_LO 0x00000040 +# define OPTS_LP 0x00000080 +# define OPTS_LR 0x00000100 +# define OPTS_LT 0x00000200 +# define OPTS_LU 0x00000400 + +# define OPTS_UA 0x00000800 +# define OPTS_UF 0x00001000 +# define OPTS_UG 0x00002000 +# define OPTS_UR 0x00004000 +# define OPTS_UU 0x00008000 +# define OPTS_US 0x00010000 +# define OPTS_UT 0x00020000 + +# define OPTS_ONE 0x00040000 +# define OPTS_ERR_A 0x00100000 +# define OPTS_HEAD 0x01000000 + +# define TIME_MASK (OPTS_LC | OPTS_LU | OPTS_UU) + +# include "libft.h" +# include +# include +# include +# include +# include +# include +# include +# include + +struct s_lsdata +{ + struct stat stat; + char *path; + struct dirent *dirent; +}; + +struct s_pads +{ + int nlink; + int name; + int gr_name; + int size; + int major; + int minor; +}; + +struct s_opts +{ + char id; + int on; + int off; +}; + +typedef struct s_lsdata t_lsdata; +typedef struct s_pads t_pads; +typedef struct s_opts t_opts; + +extern t_opts g_opts[]; + +int ft_ls_parse(int ac, char **av, t_list **dir, t_list **ent); + +void ft_ls_dirs(t_list *dir, int opts); +void ft_ls_files(t_list **ent, t_list **dir, t_lsdata *topdir, int *opts); + +int ft_cmp_mtime(t_lsdata *dat1, t_lsdata *dat2); +int ft_cmp_atime(t_lsdata *dat1, t_lsdata *dat2); +int ft_cmp_ctime(t_lsdata *dat1, t_lsdata *dat2); +int ft_cmp_btime(t_lsdata *dat1, t_lsdata *dat2); + +void ft_ls_postname(mode_t m, int opts); +void ft_lsdata_filename(t_lsdata *data, t_lsdata *topdir, int opts); +int ft_lsdata_cmp_name(t_lsdata *dat1, t_lsdata *dat2); +int ft_lsdata_cmp_size(t_lsdata *dat1, t_lsdata *dat2); +int ft_lsdata_cmp0(t_lsdata *dat1, char *dataref); +int ft_lsdata_cmpa(t_lsdata *data, char *dataref); + +void ft_ent_filter(t_list **ent, int opts); +void ft_ent_sort(t_list **ent, int opts); +void ft_ent_print( + t_list *ent, int *opts, t_lsdata *topdir, t_list *nextdir); +int ft_ent_has_dir(t_list *ent); +t_list *ft_ent_get_dirs(t_list **ent); +void ft_ent_free(void *ptr, size_t size); + +t_list *ft_dir_get_ents(t_lsdata *topdir, int *opts); + +void ft_ls_short(t_list *ent, t_lsdata *topdir, int opts); + +int ft_ls_long(t_list *ent, t_lsdata *topdir, int opts); +int ft_ls_long_print(t_list *ent, t_lsdata *topdir, t_pads pads, int opts); + +void ft_ls_long_total(t_list *ent); +int ft_ls_long_pads(t_list *ent, t_pads *pads); +void ft_ls_long_type(mode_t m); +void ft_ls_long_rights(mode_t m); +int ft_ls_long_xattr(mode_t m, char *path); +int ft_ls_long_middle(struct stat *stat, t_pads *pads, int opts); +void ft_ls_long_date(struct stat *stat, int opts); +int ft_ls_long_lnk(t_lsdata *data); + +void ft_error_option(char c); +void ft_error_dir(char *s); +void ft_error_access(char *s); + +void ft_ls_color(mode_t m); +#endif diff --git a/ls/libft/Makefile b/ls/libft/Makefile new file mode 100644 index 00000000..4e8fdb23 --- /dev/null +++ b/ls/libft/Makefile @@ -0,0 +1,39 @@ +NAME = libft.a +CC = gcc +AR = ar -rc + +D_SRC = src +D_OBJ = obj + +O_FLAGS = +W_FLAGS = -Wall -Wextra -Werror +DEBUG = +MKDIR = mkdir -p +RM = /bin/rm -rf + +D_INC = includes + +F_SRC := $(shell find $(D_SRC) -type f -regex ".*\.c$$") +F_OBJ := $(addprefix $(D_OBJ)/, $(notdir $(F_SRC:.c=.o))) + +.PHONY: all clean fclean re + +all: $(NAME) + +$(D_OBJ)/%.o: $(D_SRC)/*/%.c + @$(MKDIR) $(D_OBJ) + @$(CC) -I$(D_INC) $(W_FLAGS) -c $< -o $@ $(DEBUG) + @echo "(libft) Compiling "$<"..." + +$(NAME): $(F_OBJ) + @$(AR) $(NAME) $(F_OBJ) + @echo "(libft) Linking "$@"..." + @ranlib $(NAME) + +clean: + $(RM) $(D_OBJ) + +fclean: clean + $(RM) $(NAME) + +re: fclean all diff --git a/ls/libft/includes/btree.h b/ls/libft/includes/btree.h new file mode 100644 index 00000000..153bbf84 --- /dev/null +++ b/ls/libft/includes/btree.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/16 11:13:15 by jhalford #+# #+# */ +/* Updated: 2016/11/25 20:37:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BTREE_H +# define BTREE_H + +# include "libft.h" + +typedef struct s_btree t_btree; + +struct s_btree +{ + void *item; + size_t content_size; + struct s_btree *left; + struct s_btree *right; +}; + +struct s_printdata +{ + int is_left; + int offset; + int depth; + int left; + int right; +}; + +typedef struct s_printdata t_printdata; + +t_btree *btree_create_node(void const *item, size_t content_size); + +void btree_insert_data( + t_btree **root, + void *item, + size_t content_size, + int (*cmpf)(void *, void *)); + +void *btree_search_item(t_btree *root, + void *data_ref, int (*cmpf)(void *, void *)); + +int btree_level_count(t_btree *root); +void btree_apply_prefix(t_btree *root, void (*applyf)(void *)); +void btree_apply_infix(t_btree *root, void (*applyf)(void *)); +void btree_apply_suffix(t_btree *root, void (*applyf)(void *)); +void btree_print(t_btree *tree, char *(*printer)(void *)); + +#endif diff --git a/ls/libft/includes/color.h b/ls/libft/includes/color.h new file mode 100644 index 00000000..5dc2c4ef --- /dev/null +++ b/ls/libft/includes/color.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 13:36:48 by jhalford #+# #+# */ +/* Updated: 2016/11/27 13:21:19 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COLOR_H +# define COLOR_H +# include "libft.h" + +struct s_color +{ + char fg[7]; + char bg[7]; +}; + +typedef struct s_color t_color; + +# define FG_BLACK "\x1b[30m" +# define FG_RED "\x1b[31m" +# define FG_GREEN "\x1b[32m" +# define FG_YELLOW "\x1b[33m" +# define FG_BLUE "\x1b[34m" +# define FG_MAGENTA "\x1b[35m" +# define FG_CYAN "\x1b[36m" +# define FG_DEFAULT "\x1b[0m" + +# define BG_BLACK "\x1b[40m" +# define BG_RED "\x1b[41m" +# define BG_GREEN "\x1b[42m" +# define BG_YELLOW "\x1b[43m" +# define BG_BLUE "\x1b[44m" +# define BG_MAGENTA "\x1b[45m" +# define BG_CYAN "\x1b[46m" +# define BG_DEFAULT "\x1b[49m" + +# define FBG_DEFAULT "\x1b[49m\x1b[20m" + +void ft_color_reset(void); +void ft_color_set(t_color color); + +void ft_color_mk(t_color *color, char fg[7], char bg[7]); +void ft_color_mkif(t_color *color, int cond, char fg[7], char bg[7]); + +#endif diff --git a/ls/libft/includes/dlst.h b/ls/libft/includes/dlst.h new file mode 100644 index 00000000..42d73729 --- /dev/null +++ b/ls/libft/includes/dlst.h @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dlst.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:21:04 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:21:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DLST_H +# define DLST_H + +struct s_dlist +{ + void *content; + size_t content_size; + struct s_dlist *next; + struct s_dlist *prev; +}; + +typedef struct s_dlist t_dlist; + +void ft_dlstadd_after(t_dlist **alst, t_dlist *new); +void ft_dlstadd_before(t_dlist **alst, t_dlist *new); +void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t)); +void ft_dlstdelone(t_dlist **alst, void (*del)(void *, size_t)); +int ft_dlstsize(t_dlist *list); +t_dlist *ft_dlstnew(void const *content, size_t content_size); +t_dlist *ft_dlstlast(t_dlist *list); +char *ft_dlsttostr(t_dlist *list); + +#endif diff --git a/ls/libft/includes/ft_printf.h b/ls/libft/includes/ft_printf.h new file mode 100644 index 00000000..0cef383e --- /dev/null +++ b/ls/libft/includes/ft_printf.h @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftprintf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:22:54 by jhalford #+# #+# */ +/* Updated: 2016/11/21 18:20:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H +# include "libft.h" +# include +# define ALL_FLAGS "#0- +" +# define ALL_CONVERSIONS "sSpdDioOuUxXcCb" + +typedef struct s_fmt t_fmt; +typedef struct s_conv t_conv; +typedef char *(t_converter)(t_fmt *fmt, va_list ap); +typedef void (t_pad_func)(char *str, t_fmt *fmt); + +struct s_conv +{ + char id; + char allowed_flags[6]; + char base[20]; + t_converter *converter; + t_pad_func *sharp_func; +}; + +struct s_fmt +{ + char flags[6]; + int width; + int precision; + char modifier[3]; + char conversion; + int valid; + t_conv conv; +}; + +int ft_vdprintf(int fd, const char *format, va_list ap); +int ft_fmtcalc(char **final, char **str, va_list ap); + +extern t_conv g_convs[]; + +t_fmt *ft_fmt_init(void); +void ft_fmt_print(t_fmt *fmt); + +t_fmt *ft_printf_parse(char **format, va_list ap); +void ft_printf_parse_flags(t_fmt *fmt, char **format); +void ft_printf_parse_width(t_fmt *fmt, char **format, va_list ap); +void ft_printf_parse_precision(t_fmt *fmt, char **format, va_list ap); +void ft_printf_parse_modifiers(t_fmt *fmt, char **format); + +char *ft_transform(t_fmt *fmt, va_list ap); + +void ft_fmt_error_conv(char conv); +void ft_fmt_error_mod_conv(char *mod, char conv); +void ft_fmt_error_flag_conv(char flag, char conv); +void ft_fmt_error_flag_flag(char flag1, char flag2); + +void ft_fmt_simplify(t_fmt *fmt); +int ft_fmt_validate_conv(t_fmt *fmt); +void ft_fmt_validate_flags(t_fmt *fmt); +void ft_fmt_validate_mod(t_fmt *fmt); + +char *ft_signed_conversion(t_fmt *fmt, va_list ap); +char *ft_unsigned_conversion(t_fmt *fmt, va_list ap); +char *ft_str_conversion(t_fmt *fmt, va_list ap); +char *ft_char_conversion(t_fmt *fmt, va_list ap); + +void ft_pad_sharp_o(char *str, t_fmt *fmt); +void ft_pad_sharp_xb(char *str, t_fmt *fmt); +void ft_pad_left(char *str, t_fmt *fmt); +void ft_pad_right(char *str, t_fmt *fmt); +#endif diff --git a/ls/libft/includes/ft_xattr.h b/ls/libft/includes/ft_xattr.h new file mode 100644 index 00000000..9afadc18 --- /dev/null +++ b/ls/libft/includes/ft_xattr.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftxattr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:24:05 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:24:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_XATTR_H +# define FT_XATTR_H +# define FT_XATTR_SIZE 10000 +# include +# include + +int ft_xattr_print(char *path); +int ft_xattr_count(char *path); +#endif diff --git a/ls/libft/includes/get_next_line.h b/ls/libft/includes/get_next_line.h new file mode 100644 index 00000000..b2e6ba42 --- /dev/null +++ b/ls/libft/includes/get_next_line.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/05 12:21:36 by jhalford #+# #+# */ +/* Updated: 2016/11/17 13:18:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H +# define BUFF_SIZE 32 + +# include "libft.h" +# include +# include + +typedef struct s_save t_save; + +struct s_save +{ + int fd; + char *str; +}; + +int get_next_line(int const fd, char **line); + +#endif diff --git a/ls/libft/includes/libft.h b/ls/libft/includes/libft.h new file mode 100644 index 00000000..c9f88621 --- /dev/null +++ b/ls/libft/includes/libft.h @@ -0,0 +1,140 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ +/* Updated: 2016/11/27 13:25:46 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include "ft_xattr.h" +# include "mytime.h" +# include "lst.h" +# include "dlst.h" +# include "btree.h" +# include "color.h" + +# include +# include +# include +# include +# include +# include +# include + +# define FT_WS(x) (x == ' ' || x == '\t' || x == '\n') +# define FT_ABS(x) (((x) < 0) ? -(x) : (x)) +# define FT_NEG(x) (((x) < 0) ? 1 : 0) +# define FT_POS(x) (((x) > 0) ? 1 : 0) +# define FT_MIN(a, b) ((a) < (b) ? (a) : (b)) +# define FT_MAX(a, b) ((a) > (b) ? (a) : (b)) +# define FT_DIST(a, b) (FT_ABS((a) - (b))) + +typedef struct s_stof t_stof; + +struct s_stof +{ + char *name; + int (*f)(); +}; + +void ft_debug(void); + +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memccpy(void *dst, const void *src, int c, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +size_t ft_strlen(const char *s); +char *ft_strdup(const char *s1); +char *ft_strcpy(char *dst, const char *src); +char *ft_strncpy(char *dst, const char *src, size_t len); +char *ft_strcat(char *s1, const char *s2); +char *ft_strncat(char *s1, const char *s2, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t size); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strstr(const char *big, const char *little); +char *ft_strnstr(const char *big, const char *little, size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *str); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_toupper(int c); +int ft_tolower(int c); + +void *ft_memalloc(size_t size); +void ft_memdel(void **ap); +char *ft_strnew(size_t size); +void ft_strdel(char **as); +void ft_strclr(char *s); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strequ(char const *s1, char const *s2); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s); +char **ft_strsplit(char const *s, char c); +char *ft_itoa(int n); +int ft_putchar(int c); +void ft_putstr(char const *s); +void ft_putendl(char const *s); +void ft_putnbr(int n); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char const *s, int fd); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putaddr(void *a); + +char *ft_strrev(char *str); +char **ft_strsplit(char const *s, char c); +char *ft_str3join(char const *s1, char const *s2, char const *s3); +char *ft_strcut(char *str, char *cut); +char **ft_split_whitespaces(char *str); +char *ft_convert_base( + char *str, char *base_from, char *base_to, char *flags); +char *ft_strcatf(char *s1, const char *s2); +char *ft_strinsert(char *str, char c, int n); + +char *ft_itoa_base(int nbr, char *base, char *flags); +char *ft_lltoa_base(long long nbr, char *base, char *flags); +char *ft_ulltoa_base(unsigned long long nbr, char *base); +char *ft_uitoa_base(unsigned int nbr, char *base); +size_t ft_ilen(int n); +size_t ft_ilen_base(int n, int base); +size_t ft_uilen(unsigned int n); +size_t ft_lllen(long long n); +size_t ft_lllen_base(long long n, int base); +int ft_addrcmp(void *a, void *b); + +char **ft_sstradd(char **list, char *new); +void ft_sstrsort(char **list, int (*cmp)()); +void ft_sstrprint(char **list, char sep); +char **ft_sstrdup(char **list); +void ft_sstrdel(char **sstr, int index); +void ft_sstrfree(char **sstr); + +char *ft_path_notdir(char *path); + +int ft_printf(const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); + +char *ft_getenv(char **env, char *key); + +void *ft_realloc(void *data, int size); +#endif diff --git a/ls/libft/includes/lst.h b/ls/libft/includes/lst.h new file mode 100644 index 00000000..bff4ef8e --- /dev/null +++ b/ls/libft/includes/lst.h @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lst.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */ +/* Updated: 2016/11/23 14:50:54 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LST_H +# define LST_H + +struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +}; + +typedef struct s_list t_list; + +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); + +t_list *ft_lstnew_range(int a, int b); +void ft_lsteadd(t_list **alst, t_list *new); +void ft_lstnadd(t_list **alst, t_list *new, int n); +void ft_lstsort(t_list **begin_list, int (*cmp)()); +void ft_lst_print(t_list *list, void (*printer)()); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lst_sorted_merge( + t_list **begin_list1, + t_list *begin_list2, + int (*cmp)()); +void ft_lst_sorted_insert( + t_list **begin_list, + t_list *insert, + int (*cmp)()); +void ft_lst_delif( + t_list **alist, + void *data_ref, + int (*cmp)(), + void (*del)(void *, size_t)); +void ft_lst_delsub( + t_list **alst, + t_list *sub, int (*cmp)(), + void (*del)(void *, size_t)); +void ft_lst_cfree(void *ptr, size_t size); +t_list *ft_lst_filter( + t_list *lst, + void const *data_ref, + t_list *(*f)(t_list *elem, void const *)); +t_list *ft_lst_removeif( + t_list **alst, + void *data_ref, + int (*cmp)()); +t_list *ft_lst_find( + t_list *begin_list, + void *data_ref, + int (*cmp)()); +t_list *ft_lstpop(t_list **lst); +void ft_lst_merge(t_list **begin_list1, t_list *begin_list2); +void ft_lst_reverse(t_list **begin_list); + +int ft_diff(void *a, void *b); +t_list *ft_id(t_list *a); + +#endif diff --git a/ls/libft/includes/mytime.h b/ls/libft/includes/mytime.h new file mode 100644 index 00000000..140e1100 --- /dev/null +++ b/ls/libft/includes/mytime.h @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mytime.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 11:43:12 by jhalford #+# #+# */ +/* Updated: 2016/11/25 20:26:20 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MYTIME_H +# define MYTIME_H +# include "libft.h" + +struct s_mytime +{ + char *year; + char *month; + char *day; + char *hour; + char *min; + char *sec; +}; + +typedef struct s_mytime t_mytime; + +int ft_time_isrecent(time_t event); + +t_mytime *ft_mytime_get(time_t epoch); +void ft_mytime_free(t_mytime **time); + +#endif diff --git a/ls/libft/pdf/ft_printf.pdf b/ls/libft/pdf/ft_printf.pdf new file mode 100644 index 00000000..b4fd9669 Binary files /dev/null and b/ls/libft/pdf/ft_printf.pdf differ diff --git a/ls/libft/pdf/get_next_line.fr.pdf b/ls/libft/pdf/get_next_line.fr.pdf new file mode 100644 index 00000000..03d33367 Binary files /dev/null and b/ls/libft/pdf/get_next_line.fr.pdf differ diff --git a/ls/libft/pdf/libft.fr.pdf b/ls/libft/pdf/libft.fr.pdf new file mode 100644 index 00000000..b3951a0a Binary files /dev/null and b/ls/libft/pdf/libft.fr.pdf differ diff --git a/ls/libft/src/btree/btree_apply_by_level.c b/ls/libft/src/btree/btree_apply_by_level.c new file mode 100644 index 00000000..09619fee --- /dev/null +++ b/ls/libft/src/btree/btree_apply_by_level.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_apply_by_level.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/19 12:06:15 by jhalford #+# #+# */ +/* Updated: 2016/11/16 11:14:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +int g_level = 0; + +static void btree_apply_to_level( + t_btree *root, + int level, + int is_first_elem, + void (*applyf)(void *item, int current_level, int is_first_elem)) +{ + if (level == g_level) + { + (*applyf)(root->item, level, is_first_elem); + return ; + } + if (root->left) + btree_apply_to_level(root->left, level + 1, is_first_elem, applyf); + if (root->right) + btree_apply_to_level(root->right, level + 1, 0, applyf); +} + +void btree_apply_by_level( + t_btree *root, + void (*applyf)(void *item, int current_level, int is_first_elem)) +{ + int height; + + height = btree_level_count(root); + while (g_level < height) + { + btree_apply_to_level(root, 0, 1, applyf); + g_level++; + } +} diff --git a/ls/libft/src/btree/btree_apply_infix.c b/ls/libft/src/btree/btree_apply_infix.c new file mode 100644 index 00000000..f4b95302 --- /dev/null +++ b/ls/libft/src/btree/btree_apply_infix.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/11/14 11:58:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_apply_infix(t_btree *root, void (*applyf)(void *)) +{ + if (root->left) + btree_apply_infix(root->left, applyf); + (*applyf)(root->item); + if (root->right) + btree_apply_infix(root->right, applyf); + return ; +} diff --git a/ls/libft/src/btree/btree_apply_prefix.c b/ls/libft/src/btree/btree_apply_prefix.c new file mode 100644 index 00000000..e7e4332c --- /dev/null +++ b/ls/libft/src/btree/btree_apply_prefix.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/18 21:06:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_apply_prefix(t_btree *root, void (*applyf)(void *)) +{ + (*applyf)(root->item); + if (root->left) + btree_apply_prefix(root->left, applyf); + if (root->right) + btree_apply_prefix(root->right, applyf); +} diff --git a/ls/libft/src/btree/btree_apply_suffix.c b/ls/libft/src/btree/btree_apply_suffix.c new file mode 100644 index 00000000..08f01881 --- /dev/null +++ b/ls/libft/src/btree/btree_apply_suffix.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/11/14 16:08:23 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_apply_suffix(t_btree *root, void (*applyf)(void *)) +{ + if (root->left) + btree_apply_suffix(root->left, applyf); + if (root->right) + btree_apply_suffix(root->right, applyf); + (*applyf)(root->item); + return ; +} diff --git a/ls/libft/src/btree/btree_create_node.c b/ls/libft/src/btree/btree_create_node.c new file mode 100644 index 00000000..18f50233 --- /dev/null +++ b/ls/libft/src/btree/btree_create_node.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/11/14 16:11:49 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +t_btree *btree_create_node(void const *item, size_t content_size) +{ + t_btree *new; + + if (!(new = (t_btree *)malloc(sizeof(t_btree)))) + return (NULL); + new->left = 0; + new->right = 0; + if (!item) + { + new->content_size = 0; + new->item = NULL; + } + else + { + new->content_size = content_size; + new->item = ft_memalloc(content_size + 1); + ft_memcpy(new->item, item, content_size); + } + return (new); +} diff --git a/ls/libft/src/btree/btree_insert_data.c b/ls/libft/src/btree/btree_insert_data.c new file mode 100644 index 00000000..a6e262fc --- /dev/null +++ b/ls/libft/src/btree/btree_insert_data.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/11/14 16:12:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_insert_data( + t_btree **root, + void *item, + size_t content_size, + int (*cmpf)(void *, void *)) +{ + t_btree *node; + + if (!*root) + { + *root = btree_create_node(item, content_size); + return ; + } + node = *root; + if ((*cmpf)(item, node->item) < 0) + { + if (node->left) + btree_insert_data(&node->left, item, content_size, cmpf); + else + node->left = btree_create_node(item, content_size); + } + else + { + if (node->right) + btree_insert_data(&node->right, item, content_size, cmpf); + else + node->right = btree_create_node(item, content_size); + } +} diff --git a/ls/libft/src/btree/btree_level_count.c b/ls/libft/src/btree/btree_level_count.c new file mode 100644 index 00000000..41248654 --- /dev/null +++ b/ls/libft/src/btree/btree_level_count.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/25 17:46:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +int btree_level_count(t_btree *root) +{ + return (root + ? 1 + FT_MAX(btree_level_count(root->left), + btree_level_count(root->right)) + : 0); +} diff --git a/ls/libft/src/btree/btree_print.c b/ls/libft/src/btree/btree_print.c new file mode 100644 index 00000000..2db652b8 --- /dev/null +++ b/ls/libft/src/btree/btree_print.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/14 18:06:24 by jhalford #+# #+# */ +/* Updated: 2016/11/25 20:44:56 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +static int print_t2(t_printdata data, char s[20][255], char b[20]) +{ + int width; + int i; + + width = 5; + i = -1; + while (++i < width) + s[2 * data.depth][data.offset + data.left + i] = b[i]; + i = -1; + if (data.depth && data.is_left) + { + while (++i < width + data.right) + s[2 * data.depth - 1] + [data.offset + data.left + width / 2 + i] = '-'; + s[2 * data.depth - 1][data.offset + data.left + width / 2] = '+'; + s[2 * data.depth - 1] + [data.offset + data.left + data.right + 3 * width / 2] = '+'; + } + else if (data.depth && !data.is_left) + { + while (++i < width + data.left) + s[2 * data.depth - 1][data.offset - width / 2 + i] = '-'; + s[2 * data.depth - 1][data.offset + data.left + width / 2] = '+'; + s[2 * data.depth - 1][data.offset - width / 2 - 1] = '+'; + } + return (data.left + width + data.right); +} + +static int print_t(t_btree *tree, + t_printdata data, char s[20][255], char *(*printer)(void *)) +{ + char b[20]; + int width; + + width = 5; + if (!tree) + return (0); + sprintf(b, "%5s", printer(tree->item)); + data.left = print_t(tree->left, (t_printdata){ + 1, data.offset, data.depth + 1, data.left, data.right}, s, printer); + data.right = print_t(tree->right, (t_printdata){ + 0, data.offset + data.left + width, data.depth + 1, + data.left, data.right}, s, printer); + return (print_t2(data, s, b)); +} + +void btree_print(t_btree *tree, char *(*printer)(void *)) +{ + char s[20][255]; + char empty[255]; + int i; + + i = -1; + while (++i < 20) + sprintf(s[i], "%80s", " "); + sprintf(empty, "%80s", " "); + print_t(tree, (t_printdata){0, 0, 0, 0, 0}, s, printer); + i = -1; + while (++i < 20) + { + if (ft_strcmp(s[i], empty) == 0) + break ; + printf("%s\n", s[i]); + } +} diff --git a/ls/libft/src/btree/btree_search_item.c b/ls/libft/src/btree/btree_search_item.c new file mode 100644 index 00000000..a7a1eb68 --- /dev/null +++ b/ls/libft/src/btree/btree_search_item.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/23 19:04:56 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void *btree_search_item(t_btree *root, + void *data_ref, int (*cmpf)(void *, void *)) +{ + void *out; + + out = NULL; + if (root) + { + out = btree_search_item(root->left, data_ref, cmpf); + if (!out && ((*cmpf)(root->item, data_ref) == 0)) + out = root->item; + if (!out) + out = btree_search_item(root->right, data_ref, cmpf); + } + return (out); +} diff --git a/ls/libft/src/char/ft_isalnum.c b/ls/libft/src/char/ft_isalnum.c new file mode 100644 index 00000000..673b6e89 --- /dev/null +++ b/ls/libft/src/char/ft_isalnum.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:56:18 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:31:33 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + if ((c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9')) + return (c); + return (0); +} diff --git a/ls/libft/src/char/ft_isalpha.c b/ls/libft/src/char/ft_isalpha.c new file mode 100644 index 00000000..29f532a9 --- /dev/null +++ b/ls/libft/src/char/ft_isalpha.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:56:24 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:32:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + return (c); + return (0); +} diff --git a/ls/libft/src/char/ft_isascii.c b/ls/libft/src/char/ft_isascii.c new file mode 100644 index 00000000..52a85067 --- /dev/null +++ b/ls/libft/src/char/ft_isascii.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:56:28 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:35:42 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/ls/libft/src/char/ft_isdigit.c b/ls/libft/src/char/ft_isdigit.c new file mode 100644 index 00000000..510371c2 --- /dev/null +++ b/ls/libft/src/char/ft_isdigit.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:56:33 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:56:34 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + unsigned char a; + + a = (unsigned char)c; + if (a >= '0' && a <= '9') + return (a); + return (0); +} diff --git a/ls/libft/src/char/ft_isprint.c b/ls/libft/src/char/ft_isprint.c new file mode 100644 index 00000000..74a46640 --- /dev/null +++ b/ls/libft/src/char/ft_isprint.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:56:38 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:32:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (c); + return (0); +} diff --git a/ls/libft/src/char/ft_tolower.c b/ls/libft/src/char/ft_tolower.c new file mode 100644 index 00000000..3992c4c9 --- /dev/null +++ b/ls/libft/src/char/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:46 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:24:09 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c + 32); + return (c); +} diff --git a/ls/libft/src/char/ft_toupper.c b/ls/libft/src/char/ft_toupper.c new file mode 100644 index 00000000..3aa9ea10 --- /dev/null +++ b/ls/libft/src/char/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:53:58 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:24:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c - 32); + return (c); +} diff --git a/ls/libft/src/color/ft_color_mk.c b/ls/libft/src/color/ft_color_mk.c new file mode 100644 index 00000000..974b06d3 --- /dev/null +++ b/ls/libft/src/color/ft_color_mk.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_color_mk.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/27 11:35:18 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:36:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_color_mk(t_color *color, char fg[7], char bg[7]) +{ + ft_strcpy(color->fg, fg); + ft_strcpy(color->bg, bg); +} diff --git a/ls/libft/src/color/ft_color_mkif.c b/ls/libft/src/color/ft_color_mkif.c new file mode 100644 index 00000000..7b05f315 --- /dev/null +++ b/ls/libft/src/color/ft_color_mkif.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_color_mk.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/27 11:35:18 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:36:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_color_mkif(t_color *color, int cond, char fg[7], char bg[7]) +{ + if (cond) + ft_color_mk(color, fg, bg); +} diff --git a/ls/libft/src/color/ft_color_reset.c b/ls/libft/src/color/ft_color_reset.c new file mode 100644 index 00000000..e9748484 --- /dev/null +++ b/ls/libft/src/color/ft_color_reset.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_color_reset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 13:48:05 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:42:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_color_reset(void) +{ + ft_putstr(FG_DEFAULT); + ft_putstr(BG_DEFAULT); +} diff --git a/ls/libft/src/color/ft_color_set.c b/ls/libft/src/color/ft_color_set.c new file mode 100644 index 00000000..911f94dc --- /dev/null +++ b/ls/libft/src/color/ft_color_set.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_color_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 13:53:46 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:29:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_color_set(t_color color) +{ + char out[20]; + + ft_strcpy(out, color.fg); + ft_strcat(out, color.bg); + ft_putstr(out); +} diff --git a/ls/libft/src/dlst/ft_dlstadd_after.c b/ls/libft/src/dlst/ft_dlstadd_after.c new file mode 100644 index 00000000..19dc852a --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstadd_after.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlst_add_after.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:04 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:27:36 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_dlstadd_after(t_dlist **alst, t_dlist *new) +{ + if (new) + { + new->prev = (*alst); + if (*alst) + new->next = (*alst)->next; + else + new->next = NULL; + if (new->next) + new->next->prev = new; + if (new->prev) + new->prev->next = new; + *alst = new; + } +} diff --git a/ls/libft/src/dlst/ft_dlstadd_before.c b/ls/libft/src/dlst/ft_dlstadd_before.c new file mode 100644 index 00000000..e2d6b2e8 --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstadd_before.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlst_add_before.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:09 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:27:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_dlstadd_before(t_dlist **alst, t_dlist *new) +{ + if (new) + { + new->next = (*alst); + if (*alst) + new->prev = (*alst)->prev; + if (new->next) + new->next->prev = new; + if (new->prev) + new->prev->next = new; + *alst = new; + } +} diff --git a/ls/libft/src/dlst/ft_dlstdel.c b/ls/libft/src/dlst/ft_dlstdel.c new file mode 100644 index 00000000..43d0eb63 --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstdel.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/14 17:55:40 by jhalford #+# #+# */ +/* Updated: 2016/11/16 11:15:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_dlstdelback(t_dlist **alst, void (*del)(void *, size_t)) +{ + if (alst && *alst && del) + { + ft_dlstdelback(&(*alst)->prev, del); + ft_dlstdelone(alst, del); + } +} + +static void ft_dlstdelfront(t_dlist **alst, void (*del)(void *, size_t)) +{ + if (alst && *alst && del) + { + ft_dlstdelfront(&(*alst)->next, del); + ft_dlstdelone(alst, del); + } +} + +void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t)) +{ + if (alst && *alst && del) + { + ft_dlstdelback(&(*alst)->prev, del); + ft_dlstdelfront(&(*alst)->next, del); + ft_dlstdelone(alst, del); + } +} diff --git a/ls/libft/src/dlst/ft_dlstdelone.c b/ls/libft/src/dlst/ft_dlstdelone.c new file mode 100644 index 00000000..55826853 --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstdelone.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlst_delone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:13 by jhalford #+# #+# */ +/* Updated: 2016/11/14 17:52:58 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_dlstdelone(t_dlist **alst, void (*del)(void *, size_t)) +{ + t_dlist *tmp; + + tmp = *alst; + if (tmp) + { + if (del) + (*del)(tmp->content, tmp->content_size); + if (tmp->next) + tmp->next->prev = tmp->prev; + if (tmp->prev) + tmp->prev->next = tmp->next; + if (tmp->prev) + *alst = tmp->prev; + else + *alst = tmp->next; + free(tmp); + } +} diff --git a/ls/libft/src/dlst/ft_dlstlast.c b/ls/libft/src/dlst/ft_dlstlast.c new file mode 100644 index 00000000..1286c32c --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstlast.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlst_last.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:15 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:27:15 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_dlist *ft_dlstlast(t_dlist *list) +{ + while (list && list->next) + list = list->next; + return (list); +} diff --git a/ls/libft/src/dlst/ft_dlstnew.c b/ls/libft/src/dlst/ft_dlstnew.c new file mode 100644 index 00000000..dfe32bdc --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstnew.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlst_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:20 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:27:20 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_dlist *ft_dlstnew(void const *content, size_t content_size) +{ + t_dlist *new; + + if (!content) + { + new = malloc(sizeof(*new)); + if (!new) + return (NULL); + new->content_size = 0; + new->content = NULL; + } + else + { + new = (t_dlist *)malloc(sizeof(*new)); + if (!new) + return (NULL); + new->content_size = content_size; + new->content = ft_memalloc(content_size + 1); + ft_memcpy(new->content, content, content_size); + } + new->next = NULL; + new->prev = NULL; + return (new); +} diff --git a/ls/libft/src/dlst/ft_dlstrtostr.c b/ls/libft/src/dlst/ft_dlstrtostr.c new file mode 100644 index 00000000..e92e4d8a --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstrtostr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstrtostr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:29 by jhalford #+# #+# */ +/* Updated: 2016/11/14 16:13:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_dlsttostr(t_dlist *list) +{ + char *str; + + if (!list) + return (NULL); + while (list->prev) + list = list->prev; + str = (char *)ft_strnew(sizeof(char) * (ft_dlstsize(list) + 2)); + while (list) + { + ft_strcat(str, (char *)list->content); + list = list->next; + } + return (str); +} diff --git a/ls/libft/src/dlst/ft_dlstsize.c b/ls/libft/src/dlst/ft_dlstsize.c new file mode 100644 index 00000000..b536efa2 --- /dev/null +++ b/ls/libft/src/dlst/ft_dlstsize.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlst_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:23 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:27:23 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_dlstsize(t_dlist *list) +{ + int size; + t_dlist *tmp; + + size = 0; + if (list) + size++; + tmp = list; + while (tmp->next) + { + size++; + tmp = tmp->next; + } + tmp = list; + while (tmp->prev) + { + size++; + tmp = tmp->prev; + } + return (size); +} diff --git a/ls/libft/src/env/ft_getenv.c b/ls/libft/src/env/ft_getenv.c new file mode 100644 index 00000000..65ff27a7 --- /dev/null +++ b/ls/libft/src/env/ft_getenv.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getenv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 14:30:00 by jhalford #+# #+# */ +/* Updated: 2016/11/16 11:24:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_getenv(char **env, char *key) +{ + if (!env) + return (NULL); + while (*env) + { + if (ft_strcmp(*env, key) == '=') + return (*env + ft_strlen(key) + 1); + env++; + } + return (NULL); +} diff --git a/ls/libft/src/ft_printf/ft_conversion.c b/ls/libft/src/ft_printf/ft_conversion.c new file mode 100644 index 00000000..f8844a94 --- /dev/null +++ b/ls/libft/src/ft_printf/ft_conversion.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_conversion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:31:48 by jhalford #+# #+# */ +/* Updated: 2016/11/16 18:30:20 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +char *ft_signed_conversion(t_fmt *fmt, va_list ap) +{ + char base10[11]; + long long arg; + + arg = va_arg(ap, int); + ft_strcpy(base10, "0123456789"); + (void)fmt; + return (ft_lltoa_base(arg, base10, fmt->flags)); +} + +char *ft_unsigned_conversion(t_fmt *fmt, va_list ap) +{ + unsigned int uiarg; + unsigned long long ullarg; + int i; + + i = 0; + while (fmt->conversion != g_convs[i].id) + i++; + if (!*fmt->modifier + || ft_strequ(fmt->modifier, "hh") + || ft_strequ(fmt->modifier, "h") + || ft_strequ(fmt->modifier, "z")) + { + uiarg = va_arg(ap, int); + return (ft_uitoa_base(uiarg, g_convs[i].base)); + } + ullarg = va_arg(ap, long long); + return (ft_ulltoa_base(ullarg, g_convs[i].base)); +} + +char *ft_char_conversion(t_fmt *fmt, va_list ap) +{ + char *ret; + + (void)fmt; + ret = (char *)malloc(sizeof(char) + 1); + ret[0] = (char)va_arg(ap, int); + ret[1] = '\0'; + return (ret); +} + +char *ft_str_conversion(t_fmt *fmt, va_list ap) +{ + char *ret; + + (void)fmt; + ret = ft_strdup(va_arg(ap, char *)); + if (fmt->precision && fmt->precision < (int)ft_strlen(ret)) + ret[fmt->precision] = '\0'; + return (ret); +} diff --git a/ls/libft/src/ft_printf/ft_fmt_simplify.c b/ls/libft/src/ft_printf/ft_fmt_simplify.c new file mode 100644 index 00000000..f35d2ffa --- /dev/null +++ b/ls/libft/src/ft_printf/ft_fmt_simplify.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_fmt_validate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:43 by jhalford #+# #+# */ +/* Updated: 2016/11/07 16:53:54 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_fmt_simplify(t_fmt *fmt) +{ + char hashtag; + + hashtag = '#'; + if (fmt->conversion == 'p') + { + fmt->conversion = 'x'; + if (!ft_strchr(fmt->flags, '#')) + ft_strncat(fmt->flags, &hashtag, 1); + } + if (ft_strchr("DOUCS", fmt->conversion)) + { + fmt->conversion += 32; + ft_strcpy(fmt->modifier, "l"); + } +} diff --git a/ls/libft/src/ft_printf/ft_fmt_validate_conv.c b/ls/libft/src/ft_printf/ft_fmt_validate_conv.c new file mode 100644 index 00000000..a10b619c --- /dev/null +++ b/ls/libft/src/ft_printf/ft_fmt_validate_conv.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fmt_validate_conv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 16:55:36 by jhalford #+# #+# */ +/* Updated: 2016/11/07 16:55:37 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_fmt_validate_conv(t_fmt *fmt) +{ + if (!ft_strchr(ALL_CONVERSIONS, fmt->conversion)) + { + if (fmt->conversion != '%') + ft_fmt_error_conv(fmt->conversion); + return (1); + } + return (0); +} diff --git a/ls/libft/src/ft_printf/ft_fmt_validate_flags.c b/ls/libft/src/ft_printf/ft_fmt_validate_flags.c new file mode 100644 index 00000000..3f5892e1 --- /dev/null +++ b/ls/libft/src/ft_printf/ft_fmt_validate_flags.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fmt_validate_flags.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 16:53:07 by jhalford #+# #+# */ +/* Updated: 2016/11/16 11:15:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_fmt_validate_flag_flag(t_fmt *fmt) +{ + char *flag_ptr; + + if (ft_strchr(fmt->flags, '+') && (flag_ptr = ft_strchr(fmt->flags, ' '))) + { + ft_fmt_error_flag_flag(' ', '+'); + *flag_ptr = '.'; + } + if (ft_strchr(fmt->flags, '-') && (flag_ptr = ft_strchr(fmt->flags, '0'))) + { + ft_fmt_error_flag_flag('0', '-'); + *flag_ptr = '.'; + } + if (fmt->precision && (flag_ptr = ft_strchr(fmt->flags, '0'))) + *flag_ptr = '.'; +} + +static void ft_fmt_validate_flag_conv(t_fmt *fmt) +{ + char *flag_ptr; + char flag; + int i; + + i = 0; + flag_ptr = fmt->flags; + while (fmt->conversion != g_convs[i].id) + i++; + while (*flag_ptr) + { + flag = *flag_ptr; + if (!ft_strchr(g_convs[i].allowed_flags, flag)) + { + ft_fmt_error_flag_conv(flag, fmt->conversion); + if (flag == '#') + *flag_ptr = '.'; + } + flag_ptr++; + } +} + +void ft_fmt_validate_flags(t_fmt *fmt) +{ + int i; + + i = 0; + ft_fmt_validate_flag_conv(fmt); + ft_fmt_validate_flag_flag(fmt); +} diff --git a/ls/libft/src/ft_printf/ft_fmt_validate_mod.c b/ls/libft/src/ft_printf/ft_fmt_validate_mod.c new file mode 100644 index 00000000..e5cdb60e --- /dev/null +++ b/ls/libft/src/ft_printf/ft_fmt_validate_mod.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fmt_validate_conv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 16:53:42 by jhalford #+# #+# */ +/* Updated: 2016/11/07 16:53:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_fmt_validate_mod(t_fmt *fmt) +{ + if (fmt->conversion == 's' || fmt->conversion == 'c') + if (fmt->modifier[0] && ft_strcmp(fmt->modifier, "l")) + ft_fmt_error_mod_conv(fmt->modifier, fmt->conversion); +} diff --git a/ls/libft/src/ft_printf/ft_printf.c b/ls/libft/src/ft_printf/ft_printf.c new file mode 100644 index 00000000..79a78e4f --- /dev/null +++ b/ls/libft/src/ft_printf/ft_printf.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */ +/* Updated: 2016/11/21 18:22:26 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +t_conv g_convs[] = +{ + {'d', "0- +", "0123456789", &ft_signed_conversion, NULL}, + {'i', "0- +", "0123456789", &ft_signed_conversion, NULL}, + {'u', "0-", "0123456789", &ft_unsigned_conversion, NULL}, + {'o', "#0-", "01234567", &ft_unsigned_conversion, &ft_pad_sharp_o}, + {'x', "#0-", "0123456789abcdef", &ft_unsigned_conversion, &ft_pad_sharp_xb}, + {'X', "#0-", "0123456789ABCDEF", &ft_unsigned_conversion, &ft_pad_sharp_xb}, + {'b', "#0-", "01", &ft_unsigned_conversion, &ft_pad_sharp_xb}, + {'s', "-", "", &ft_str_conversion, NULL}, + {'c', "-", "", &ft_char_conversion, NULL}, +}; + +int ft_printf(const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + return (ft_vdprintf(1, format, ap)); +} + +int ft_dprintf(int fd, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + return (ft_vdprintf(fd, format, ap)); +} + +int ft_vdprintf(int fd, const char *format, va_list ap) +{ + char *str; + char *tmp; + char *final; + + str = (char *)format; + final = ft_strnew(1); + while (*str) + { + tmp = final; + if (*str == '%') + { + if (ft_fmtcalc(&final, &str, ap)) + return (1); + } + else + final = ft_strjoin(final, (char[]){*str++, 0}); + ft_strdel(&tmp); + } + ft_putstr_fd(final, fd); + ft_strdel(&final); + return (0); +} + +int ft_fmtcalc(char **final, char **str, va_list ap) +{ + t_fmt *fmt; + char *transform; + + *str += 1; + if (!(fmt = ft_printf_parse(str, ap))) + return (1); + if (!fmt->valid) + ft_strncat(*final, &fmt->conversion, 1); + else + { + transform = ft_transform(fmt, ap); + *final = ft_strjoin(*final, transform); + ft_strdel(&transform); + } + free(fmt); + return (0); +} diff --git a/ls/libft/src/ft_printf/ft_printf_parse.c b/ls/libft/src/ft_printf/ft_printf_parse.c new file mode 100644 index 00000000..fe3d2674 --- /dev/null +++ b/ls/libft/src/ft_printf/ft_printf_parse.c @@ -0,0 +1,123 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:24 by jhalford #+# #+# */ +/* Updated: 2016/11/10 12:59:50 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +t_fmt *ft_printf_parse(char **format, va_list ap) +{ + t_fmt *fmt; + + fmt = ft_fmt_init(); + ft_printf_parse_flags(fmt, format); + ft_printf_parse_width(fmt, format, ap); + ft_printf_parse_precision(fmt, format, ap); + ft_printf_parse_modifiers(fmt, format); + fmt->conversion = **format; + (*format)++; + ft_fmt_validate_mod(fmt); + ft_fmt_validate_flags(fmt); + ft_fmt_simplify(fmt); + fmt->valid = ft_fmt_validate_conv(fmt) ? 0 : 1; + return (fmt); +} + +void ft_printf_parse_flags(t_fmt *fmt, char **format) +{ + int i; + char *str; + + i = 0; + str = *format; + while (str[i]) + { + if (ft_strchr(ALL_FLAGS, (int)str[i])) + { + if (!ft_strchr(fmt->flags, (int)str[i])) + ft_strncat(fmt->flags, str + i, 1); + } + else + break ; + i++; + } + *format += i; +} + +void ft_printf_parse_width(t_fmt *fmt, char **format, va_list ap) +{ + int i; + char buf[10]; + char *str; + + i = 0; + str = *format; + if (str[i] == '*') + { + i++; + fmt->width = va_arg(ap, int); + } + else + { + ft_strcpy(buf, "0"); + while (ft_isdigit((int)(str[i]))) + ft_strncat(buf, str + i++, 1); + fmt->width = ft_atoi(buf); + } + *format += i; +} + +void ft_printf_parse_precision(t_fmt *fmt, char **format, va_list ap) +{ + int i; + char buf[10]; + char *str; + + i = 0; + str = *format; + if (str[i] == '.') + { + if (str[++i] == '*') + { + i++; + fmt->precision = va_arg(ap, int); + } + else + { + ft_strcpy(buf, "0"); + while (ft_isdigit(str[i])) + ft_strncat(buf, str + i++, 1); + fmt->precision = ft_atoi(buf); + } + } + *format += i; +} + +void ft_printf_parse_modifiers(t_fmt *fmt, char **format) +{ + char *str; + + str = *format; + if (str[0] == 'h' && str[1] == 'h') + ft_strcpy(fmt->modifier, "hh"); + else if (str[0] == 'h' && str[1] != 'h') + ft_strcpy(fmt->modifier, "h"); + else if (str[0] == 'l' && str[1] == 'l') + ft_strcpy(fmt->modifier, "ll"); + else if (str[0] == 'l' && str[1] != 'l') + ft_strcpy(fmt->modifier, "l"); + else if (str[0] == 'j') + ft_strcpy(fmt->modifier, "j"); + else if (str[0] == 'z') + ft_strcpy(fmt->modifier, "z"); + else + ft_strcpy(fmt->modifier, ""); + *format += ft_strlen(fmt->modifier); +} diff --git a/ls/libft/src/ft_printf/ft_transform.c b/ls/libft/src/ft_printf/ft_transform.c new file mode 100644 index 00000000..edadbed2 --- /dev/null +++ b/ls/libft/src/ft_printf/ft_transform.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_transform.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:32 by jhalford #+# #+# */ +/* Updated: 2016/11/16 17:56:37 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +char *ft_transform(t_fmt *fmt, va_list ap) +{ + char *ret; + int i; + + i = 0; + while (fmt->conversion != g_convs[i].id) + i++; + fmt->conv = g_convs[i]; + ret = (*fmt->conv.converter)(fmt, ap); + if (fmt->width > (int)ft_strlen(ret)) + ret = ft_realloc(ret, fmt->width + 5); + if (ft_strchr(fmt->flags, '-')) + ft_pad_right(ret, fmt); + else + ft_pad_left(ret, fmt); + return (ret); +} diff --git a/ls/libft/src/ft_printf/lib_fmt.c b/ls/libft/src/ft_printf/lib_fmt.c new file mode 100644 index 00000000..5865af59 --- /dev/null +++ b/ls/libft/src/ft_printf/lib_fmt.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_fmt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:35 by jhalford #+# #+# */ +/* Updated: 2016/11/16 17:46:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +t_fmt *ft_fmt_init(void) +{ + t_fmt *fmt; + + fmt = (t_fmt *)malloc(sizeof(t_fmt) + 1); + ft_bzero(fmt->flags, 6); + ft_bzero(fmt->modifier, 3); + fmt->conversion = '\0'; + fmt->width = 0; + fmt->precision = 0; + fmt->valid = 0; + return (fmt); +} + +void ft_fmt_print(t_fmt *fmt) +{ + ft_putendl("\n---"); + ft_putstr("valid: "); + ft_putnbr(fmt->valid); + ft_putendl(""); + ft_putstr("conv.: "); + ft_putchar(fmt->conversion); + ft_putendl(""); + ft_putstr("flags: "); + ft_putendl(fmt->flags); + ft_putstr("width: "); + ft_putnbr(fmt->width); + ft_putendl(""); + ft_putstr("prec.: "); + ft_putnbr(fmt->precision); + ft_putendl(""); + ft_putstr("modifier: "); + ft_putendl(fmt->modifier); + ft_putendl("---"); +} diff --git a/ls/libft/src/ft_printf/lib_fmt_error.c b/ls/libft/src/ft_printf/lib_fmt_error.c new file mode 100644 index 00000000..a10954ff --- /dev/null +++ b/ls/libft/src/ft_printf/lib_fmt_error.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_fmt_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:38 by jhalford #+# #+# */ +/* Updated: 2016/11/07 17:22:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_fmt_error_conv(char conv) +{ + ft_putstr("Warning: invalid or unsupported conversion specifier '"); + ft_putchar(conv); + ft_putendl("'"); +} + +void ft_fmt_error_mod_conv(char *mod, char conv) +{ + ft_putstr("warning: length modifier '"); + ft_putstr(mod); + ft_putstr("' results in undefined behaviour or no effect with '"); + ft_putchar(conv); + ft_putendl("' conversion specifier"); +} + +void ft_fmt_error_flag_conv(char flag, char conv) +{ + ft_putstr("warning: flag '"); + ft_putchar(flag); + ft_putstr("' results in undefined behaviour with '"); + ft_putchar(conv); + ft_putendl("' conversion specifier"); +} + +void ft_fmt_error_flag_flag(char flag1, char flag2) +{ + ft_putstr("warning: flag '"); + ft_putchar(flag1); + ft_putstr("' is ignored when flag '"); + ft_putchar(flag2); + ft_putendl("' is present"); +} diff --git a/ls/libft/src/ft_printf/lib_pad.c b/ls/libft/src/ft_printf/lib_pad.c new file mode 100644 index 00000000..20919955 --- /dev/null +++ b/ls/libft/src/ft_printf/lib_pad.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_pad.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:45 by jhalford #+# #+# */ +/* Updated: 2016/11/16 18:13:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_pad_right(char *str, t_fmt *fmt) +{ + if (ft_strchr(fmt->flags, '#')) + (fmt->conv.sharp_func)(str, fmt); + while ((int)ft_strlen(str) < fmt->width) + ft_strcat(str, " "); +} + +void ft_pad_left(char *str, t_fmt *fmt) +{ + char sign; + + sign = 0; + if (str[0] == '-' || str[0] == '+' || str[0] == ' ') + { + sign = str[0]; + str++; + } + if (ft_strchr(fmt->flags, '0')) + while ((int)ft_strlen(str) < fmt->width - (sign ? 1 : 0)) + ft_strcatf(str, "0"); + if (sign) + str--; + if (ft_strchr(fmt->flags, '#')) + (fmt->conv.sharp_func)(str, fmt); + while ((int)ft_strlen(str) < fmt->width) + ft_strcatf(str, " "); +} diff --git a/ls/libft/src/ft_printf/lib_pad_sharp.c b/ls/libft/src/ft_printf/lib_pad_sharp.c new file mode 100644 index 00000000..bca9add2 --- /dev/null +++ b/ls/libft/src/ft_printf/lib_pad_sharp.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_pad_sharp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:33:48 by jhalford #+# #+# */ +/* Updated: 2016/11/16 17:56:42 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_pad_sharp_o(char *str, t_fmt *fmt) +{ + char buf[100]; + + (void)fmt; + ft_bzero(buf, 100); + if (str[0] != '0') + ft_strcatf(buf, "0"); +} + +void ft_pad_sharp_xb(char *str, t_fmt *fmt) +{ + char buf[100]; + int i; + + i = 0; + ft_bzero(buf, 100); + ft_strcpy(buf, "0"); + ft_strcat(buf, &fmt->conversion); + if (*str == '0') + i++; + if (*str == '0') + i++; + ft_strcat(buf, str + i); + ft_strcpy(str, buf); +} diff --git a/ls/libft/src/get_next_line/get_next_line.c b/ls/libft/src/get_next_line/get_next_line.c new file mode 100644 index 00000000..5ba7280a --- /dev/null +++ b/ls/libft/src/get_next_line/get_next_line.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/15 13:12:06 by jhalford #+# #+# */ +/* Updated: 2016/11/17 13:12:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +static int ft_fdcmp(t_save *a, int *b) +{ + return (a->fd - *b); +} + +static t_list *ft_newfd(t_list **head, int fd) +{ + t_save new; + + new.fd = fd; + new.str = ft_memalloc((BUFF_SIZE > 0 ? BUFF_SIZE : 0) + 1); + ft_lstadd(head, ft_lstnew(&new, sizeof(t_save))); + return (*head); +} + +static int ft_loop_read(int fd, char **line, char *save) +{ + char buf[BUFF_SIZE + 1]; + char *pos; + char *tmp; + int ret; + + while ((ret = read(fd, buf, BUFF_SIZE)) > 0) + { + buf[ret] = 0; + tmp = *line; + if ((pos = ft_strchr(buf, '\n'))) + { + ft_strcpy(save, pos + 1); + *pos = 0; + } + if (!(*line = ft_strjoin(*line, buf))) + return (-1); + ft_strdel(&tmp); + if (pos) + return (1); + } + if (ret < 0) + return (-1); + return (**line ? 1 : 0); +} + +int get_next_line(int const fd, char **line) +{ + static t_list *head; + t_list *tmp; + char *pos; + char *save; + + if (fd < 0 || !line) + return (-1); + if (!(tmp = ft_lst_find(head, (void *)&fd, &ft_fdcmp))) + tmp = ft_newfd(&head, fd); + save = ((t_save*)tmp->content)->str; + if (!(*line = ft_strdup(save))) + return (-1); + ft_bzero(save, BUFF_SIZE + 1); + if ((pos = ft_strchr(*line, '\n'))) + { + ft_strcpy(save, pos + 1); + *pos = 0; + return (1); + } + return (ft_loop_read(fd, line, save)); +} diff --git a/ls/libft/src/lst/ft_id.c b/ls/libft/src/lst/ft_id.c new file mode 100644 index 00000000..a1a622a7 --- /dev/null +++ b/ls/libft/src/lst/ft_id.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_id.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:08:55 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:10:25 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_id(t_list *a) +{ + return (a); +} diff --git a/ls/libft/src/lst/ft_lst_cfree.c b/ls/libft/src/lst/ft_lst_cfree.c new file mode 100644 index 00000000..3297a219 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_cfree.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_cfree.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:10 by jhalford #+# #+# */ +/* Updated: 2016/11/08 11:09:49 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_cfree(void *ptr, size_t size) +{ + (void)size; + if (ptr) + free(ptr); +} diff --git a/ls/libft/src/lst/ft_lst_delif.c b/ls/libft/src/lst/ft_lst_delif.c new file mode 100644 index 00000000..091b813d --- /dev/null +++ b/ls/libft/src/lst/ft_lst_delif.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_delif.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:12 by jhalford #+# #+# */ +/* Updated: 2016/11/21 14:22:51 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_delif( + t_list **alst, + void *data_ref, + int (*cmp)(), + void (*del)(void *, size_t)) +{ + t_list *tmp; + t_list **indirect; + + indirect = alst; + while (*indirect) + { + if ((*cmp)((*indirect)->content, data_ref) == 0) + { + tmp = (*indirect); + (*indirect) = (*indirect)->next; + ft_lstdelone(&tmp, del); + } + else + indirect = &(*indirect)->next; + } +} diff --git a/ls/libft/src/lst/ft_lst_delsub.c b/ls/libft/src/lst/ft_lst_delsub.c new file mode 100644 index 00000000..005e9dd2 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_delsub.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_delsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:15 by jhalford #+# #+# */ +/* Updated: 2016/11/08 13:36:17 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_delsub( + t_list **alst, + t_list *sub, + int (*cmp)(), + void (*del)(void *, size_t)) +{ + t_list *tmp; + t_list **indirect; + + indirect = alst; + while (*indirect) + { + if ((*cmp)((*indirect)->content, sub->content) == 0) + { + tmp = *indirect; + (*indirect) = (*indirect)->next; + ft_lstdelone(&tmp, del); + sub = sub->next; + } + indirect = &(*indirect)->next; + } +} diff --git a/ls/libft/src/lst/ft_lst_filter.c b/ls/libft/src/lst/ft_lst_filter.c new file mode 100644 index 00000000..2df3a067 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_filter.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_filter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:17 by jhalford #+# #+# */ +/* Updated: 2016/11/21 12:36:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lst_filter( + t_list *lst, + void const *data_ref, + t_list *(*f)(t_list *elem, void const *)) +{ + t_list *out; + t_list *elem; + + out = NULL; + while (lst) + { + elem = (*f)(lst, data_ref); + elem = ft_lstnew(elem->content, elem->content_size); + ft_lsteadd(&out, elem); + lst = lst->next; + } + return (out); +} diff --git a/ls/libft/src/lst/ft_lst_find.c b/ls/libft/src/lst/ft_lst_find.c new file mode 100644 index 00000000..140814e1 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_find.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:20 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:20 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lst_find(t_list *begin_list, void *data_ref, int (*cmp)()) +{ + t_list *list_ptr; + + list_ptr = begin_list; + while (list_ptr) + { + if ((*cmp)(list_ptr->content, data_ref) == 0) + return (list_ptr); + list_ptr = list_ptr->next; + } + return (list_ptr); +} diff --git a/ls/libft/src/lst/ft_lst_merge.c b/ls/libft/src/lst/ft_lst_merge.c new file mode 100644 index 00000000..f8160381 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_merge.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/14 13:50:32 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_merge(t_list **begin_list1, t_list *begin_list2) +{ + t_list *list_ptr; + + if (*begin_list1) + { + list_ptr = *begin_list1; + while (list_ptr->next) + list_ptr = list_ptr->next; + list_ptr->next = begin_list2; + } + else + *begin_list1 = begin_list2; +} diff --git a/ls/libft/src/lst/ft_lst_order_delsub.c b/ls/libft/src/lst/ft_lst_order_delsub.c new file mode 100644 index 00000000..5bf2067c --- /dev/null +++ b/ls/libft/src/lst/ft_lst_order_delsub.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_order_delsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:25 by jhalford #+# #+# */ +/* Updated: 2016/11/04 12:01:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_order_delsub( + t_list **alst, + t_list *sub, + int (*cmp)(), + void (*del)(void *, size_t)) +{ + t_list *tmp; + t_list **indirect; + + indirect = alst; + while (*indirect) + { + if ((*cmp)((*indirect)->content, sub->content) > 0) + { + sub = sub->next; + continue ; + } + if ((*cmp)((*indirect)->content, sub->content) == 0) + { + tmp = *indirect; + (*indirect) = (*indirect)->next; + ft_lstdelone(&tmp, del); + sub = sub->next; + } + indirect = &(*indirect)->next; + } +} diff --git a/ls/libft/src/lst/ft_lst_print.c b/ls/libft/src/lst/ft_lst_print.c new file mode 100644 index 00000000..94e5f315 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_print.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:27 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_print(t_list *list, void (*printer)()) +{ + while (list) + { + ft_putstr("["); + (*printer)(list->content); + ft_putstr("]->"); + list = list->next; + } + ft_putendl("X\n"); +} diff --git a/ls/libft/src/lst/ft_lst_print2.c b/ls/libft/src/lst/ft_lst_print2.c new file mode 100644 index 00000000..875167d1 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_print2.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_print2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:29 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:29 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_print2(t_list *list, void (*printer)()) +{ + t_list *list2; + + while (list) + { + ft_putendl("---"); + list2 = *(t_list**)list->content; + while (list2) + { + ft_putstr("["); + (*printer)(*(int *)list2->content); + ft_putstr("]->"); + list2 = list2->next; + } + ft_putendl("X"); + ft_putendl("---"); + ft_putendl(" |"); + ft_putendl(" V"); + list = list->next; + } + ft_putendl(" X\n"); +} diff --git a/ls/libft/src/lst/ft_lst_removeif.c b/ls/libft/src/lst/ft_lst_removeif.c new file mode 100644 index 00000000..2cf2d53d --- /dev/null +++ b/ls/libft/src/lst/ft_lst_removeif.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_removeif.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:30 by jhalford #+# #+# */ +/* Updated: 2016/11/16 14:00:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lst_removeif(t_list **alst, void *data_ref, int (*cmp)()) +{ + t_list *tmp; + t_list **indirect; + + indirect = alst; + while (*indirect) + { + if ((*cmp)((*indirect)->content, data_ref) == 0) + { + tmp = (*indirect); + (*indirect) = (*indirect)->next; + tmp->next = NULL; + return (tmp); + } + indirect = &(*indirect)->next; + } + return (NULL); +} diff --git a/ls/libft/src/lst/ft_lst_reverse.c b/ls/libft/src/lst/ft_lst_reverse.c new file mode 100644 index 00000000..48b14d9e --- /dev/null +++ b/ls/libft/src/lst/ft_lst_reverse.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/14 13:20:13 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:34 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_reverse(t_list **begin_list) +{ + t_list *new_start; + t_list *tmp; + + new_start = NULL; + while (*begin_list) + { + tmp = (*begin_list)->next; + (*begin_list)->next = new_start; + new_start = *begin_list; + *begin_list = tmp; + } + *begin_list = new_start; +} diff --git a/ls/libft/src/lst/ft_lst_size.c b/ls/libft/src/lst/ft_lst_size.c new file mode 100644 index 00000000..1f8c03b8 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_size.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:35 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:36 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + if (lst) + { + i = 1; + while (lst->next) + { + lst = lst->next; + i++; + } + } + return (i); +} diff --git a/ls/libft/src/lst/ft_lst_sorted_insert.c b/ls/libft/src/lst/ft_lst_sorted_insert.c new file mode 100644 index 00000000..f4821326 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_sorted_insert.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sorted_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:39 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:39 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_sorted_insert(t_list **begin_list, t_list *insert, int (*cmp)()) +{ + t_list *link; + + link = *begin_list; + if (!link || (*cmp)(insert->content, link->content) < 0) + { + *begin_list = insert; + insert->next = link ? link : NULL; + return ; + } + while (link->next) + { + if ((*cmp)(insert->content, link->content) > 0 + && (*cmp)(insert->content, link->next->content) <= 0) + { + insert->next = link->next; + link->next = insert; + return ; + } + link = link->next; + } + link->next = insert; + insert->next = NULL; +} diff --git a/ls/libft/src/lst/ft_lst_sorted_merge.c b/ls/libft/src/lst/ft_lst_sorted_merge.c new file mode 100644 index 00000000..38308849 --- /dev/null +++ b/ls/libft/src/lst/ft_lst_sorted_merge.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sorted_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:40 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_sorted_merge( + t_list **begin_list1, + t_list *begin_list2, + int (*cmp)()) +{ + t_list *tail; + t_list *head1; + t_list *head2; + int comp; + + if (!*begin_list1 || !begin_list2) + { + *begin_list1 = begin_list2 ? begin_list2 : *begin_list1; + return ; + } + comp = (*cmp)(begin_list2->content, (*begin_list1)->content); + head1 = (comp < 0) ? *begin_list1 : (*begin_list1)->next; + head2 = (comp < 0) ? begin_list2->next : begin_list2; + *begin_list1 = (comp < 0) ? begin_list2 : *begin_list1; + tail = *begin_list1; + while (head1 && head2) + { + comp = (*cmp)(head2->content, head1->content); + tail->next = (comp < 0 ? head2 : head1); + head1 = comp < 0 ? head1 : head1->next; + head2 = comp < 0 ? head2->next : head2; + tail = tail->next; + } + tail->next = head2 ? head2 : head1; +} diff --git a/ls/libft/src/lst/ft_lstadd.c b/ls/libft/src/lst/ft_lstadd.c new file mode 100644 index 00000000..f2064d9f --- /dev/null +++ b/ls/libft/src/lst/ft_lstadd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:13 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + if (new) + { + new->next = *alst; + *alst = new; + } +} diff --git a/ls/libft/src/lst/ft_lstdel.c b/ls/libft/src/lst/ft_lstdel.c new file mode 100644 index 00000000..584c25c6 --- /dev/null +++ b/ls/libft/src/lst/ft_lstdel.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 15:18:57 by jhalford #+# #+# */ +/* Updated: 2016/11/21 14:02:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +{ + if (alst && *alst && del) + { + ft_lstdel(&(*alst)->next, del); + ft_lstdelone(alst, del); + *alst = NULL; + } +} diff --git a/ls/libft/src/lst/ft_lstdelone.c b/ls/libft/src/lst/ft_lstdelone.c new file mode 100644 index 00000000..8c200615 --- /dev/null +++ b/ls/libft/src/lst/ft_lstdelone.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:15 by jhalford #+# #+# */ +/* Updated: 2016/11/21 14:02:31 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + if (alst && *alst) + { + if (del) + (*del)((*alst)->content, (*alst)->content_size); + free(*alst); + *alst = NULL; + } +} diff --git a/ls/libft/src/lst/ft_lsteadd.c b/ls/libft/src/lst/ft_lsteadd.c new file mode 100644 index 00000000..bcc3be54 --- /dev/null +++ b/ls/libft/src/lst/ft_lsteadd.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lsteadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:17 by jhalford #+# #+# */ +/* Updated: 2016/11/04 13:11:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lsteadd(t_list **alst, t_list *new) +{ + t_list *lst; + + lst = *alst; + if (lst) + { + while (lst->next) + lst = lst->next; + lst->next = new; + } + else + *alst = new; +} diff --git a/ls/libft/src/lst/ft_lstiter.c b/ls/libft/src/lst/ft_lstiter.c new file mode 100644 index 00000000..6bf86a06 --- /dev/null +++ b/ls/libft/src/lst/ft_lstiter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:19 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:19 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + while (lst) + { + (*f)(lst); + lst = lst->next; + } +} diff --git a/ls/libft/src/lst/ft_lstlast.c b/ls/libft/src/lst/ft_lstlast.c new file mode 100644 index 00000000..b8019f8f --- /dev/null +++ b/ls/libft/src/lst/ft_lstlast.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:48 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:49 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst) + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/ls/libft/src/lst/ft_lstmap.c b/ls/libft/src/lst/ft_lstmap.c new file mode 100644 index 00000000..edf178d2 --- /dev/null +++ b/ls/libft/src/lst/ft_lstmap.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:21 by jhalford #+# #+# */ +/* Updated: 2016/11/04 13:11:19 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *out; + t_list *elem; + + out = NULL; + while (lst) + { + elem = (*f)(lst); + elem = ft_lstnew(elem->content, elem->content_size); + ft_lsteadd(&out, elem); + lst = lst->next; + } + return (out); +} diff --git a/ls/libft/src/lst/ft_lstnadd.c b/ls/libft/src/lst/ft_lstnadd.c new file mode 100644 index 00000000..3f542fad --- /dev/null +++ b/ls/libft/src/lst/ft_lstnadd.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:51 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstnadd(t_list **alst, t_list *new, int n) +{ + t_list *lst; + int i; + + lst = *alst; + if (lst) + { + i = 0; + while (lst->next && i < n) + { + lst = lst->next; + i++; + } + while (lst->next) + lst = lst->next; + lst->next = new; + } + else + *alst = new; +} diff --git a/ls/libft/src/lst/ft_lstnew.c b/ls/libft/src/lst/ft_lstnew.c new file mode 100644 index 00000000..302aff27 --- /dev/null +++ b/ls/libft/src/lst/ft_lstnew.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:24 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *new; + + if (!(new = (t_list *)malloc(sizeof(*new)))) + return (NULL); + new->next = NULL; + if (!content) + { + new->content_size = 0; + new->content = NULL; + } + else + { + new->content_size = content_size; + new->content = ft_memalloc(content_size + 1); + ft_memcpy(new->content, content, content_size); + } + return (new); +} diff --git a/ls/libft/src/lst/ft_lstnew_range.c b/ls/libft/src/lst/ft_lstnew_range.c new file mode 100644 index 00000000..26632a3b --- /dev/null +++ b/ls/libft/src/lst/ft_lstnew_range.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew_range.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:54 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:54 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew_range(int a, int b) +{ + t_list *lst; + + if (a >= b) + return (NULL); + lst = NULL; + while (a < b) + { + b--; + ft_lstadd(&lst, ft_lstnew(&b, sizeof(int))); + } + return (lst); +} diff --git a/ls/libft/src/lst/ft_lstpop.c b/ls/libft/src/lst/ft_lstpop.c new file mode 100644 index 00000000..2b0ac748 --- /dev/null +++ b/ls/libft/src/lst/ft_lstpop.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:56 by jhalford #+# #+# */ +/* Updated: 2016/11/04 11:09:56 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstpop(t_list **lst) +{ + t_list *top; + + top = *lst; + if (*lst) + *lst = (*lst)->next; + return (top); +} diff --git a/ls/libft/src/lst/ft_lstsort.c b/ls/libft/src/lst/ft_lstsort.c new file mode 100644 index 00000000..725b532f --- /dev/null +++ b/ls/libft/src/lst/ft_lstsort.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:09:58 by jhalford #+# #+# */ +/* Updated: 2016/11/23 15:43:48 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstsort(t_list **begin_list, int (*cmp)()) +{ + t_list **indirect; + t_list *tmp; + t_list *tmp2; + + indirect = begin_list; + if (!*begin_list) + return ; + while (*indirect && (*indirect)->next) + { + if ((*cmp)((*indirect)->content, (*indirect)->next->content) > 0) + { + tmp = *indirect; + tmp2 = (*indirect)->next; + (*indirect)->next = (*indirect)->next->next; + *indirect = tmp2; + (*indirect)->next = tmp; + indirect = begin_list; + } + else + indirect = &(*indirect)->next; + } +} diff --git a/ls/libft/src/math/ft_addrcmp.c b/ls/libft/src/math/ft_addrcmp.c new file mode 100644 index 00000000..f3b4f790 --- /dev/null +++ b/ls/libft/src/math/ft_addrcmp.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_addrcmp.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/14 15:59:10 by jhalford #+# #+# */ +/* Updated: 2016/11/14 15:59:39 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_addrcmp(void *a, void *b) +{ + return (a - b); +} diff --git a/ls/libft/src/math/ft_ilen.c b/ls/libft/src/math/ft_ilen.c new file mode 100644 index 00000000..e22953ce --- /dev/null +++ b/ls/libft/src/math/ft_ilen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ilen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:53:53 by jhalford #+# #+# */ +/* Updated: 2016/11/07 14:44:35 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_ilen(int n) +{ + size_t i; + + i = 1; + while (n /= 10) + i++; + return (i); +} diff --git a/ls/libft/src/math/ft_ilen_base.c b/ls/libft/src/math/ft_ilen_base.c new file mode 100644 index 00000000..3c6f9ae4 --- /dev/null +++ b/ls/libft/src/math/ft_ilen_base.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ilen_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:53:53 by jhalford #+# #+# */ +/* Updated: 2016/11/07 14:45:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_ilen_base(int n, int base) +{ + size_t i; + + i = 1; + while (n /= base) + i++; + return (i); +} diff --git a/ls/libft/src/math/ft_itoa.c b/ls/libft/src/math/ft_itoa.c new file mode 100644 index 00000000..c67a6f39 --- /dev/null +++ b/ls/libft/src/math/ft_itoa.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:10 by jhalford #+# #+# */ +/* Updated: 2016/11/04 13:11:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t ft_size(int n) +{ + size_t i; + + i = 1; + while (n /= 10) + i++; + return (i); +} + +char *ft_itoa(int n) +{ + int i; + char *str; + int neg; + + i = 0; + str = ft_strnew(ft_size(n) + 1); + neg = FT_NEG(n) ? 1 : 0; + if (n == 0) + { + str[i++] = '0'; + str[i] = '\0'; + return (str); + } + while (n) + { + str[i++] = FT_ABS(n % 10) + '0'; + n /= 10; + } + if (neg) + str[i++] = '-'; + str[i] = '\0'; + return (ft_strrev(str)); +} diff --git a/ls/libft/src/math/ft_itoa_base.c b/ls/libft/src/math/ft_itoa_base.c new file mode 100644 index 00000000..05a4158d --- /dev/null +++ b/ls/libft/src/math/ft_itoa_base.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:52:51 by jhalford #+# #+# */ +/* Updated: 2016/11/07 14:45:15 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *ft_flags(char *str, int *i, char *flags, int neg) +{ + if (neg) + str[*i++] = '-'; + else if (ft_strchr(flags, '+')) + str[*i++] = '+'; + else if (ft_strchr(flags, ' ')) + str[*i++] = ' '; + return (str); +} + +char *ft_itoa_base(int nbr, char *base, char *flags) +{ + int i; + int neg; + int base_size; + char *str; + + i = 0; + base_size = ft_strlen(base); + str = ft_strnew(ft_ilen_base(nbr, base_size) + 1); + neg = FT_NEG(nbr); + if (nbr == 0) + { + str[i++] = '0'; + str[i] = '\0'; + return (str); + } + while (nbr) + { + str[i++] = base[FT_ABS(nbr % base_size)]; + nbr = nbr / base_size; + } + str = ft_flags(str, &i, flags, neg); + str[i] = '\0'; + return (ft_strrev(str)); +} diff --git a/ls/libft/src/math/ft_lllen.c b/ls/libft/src/math/ft_lllen.c new file mode 100644 index 00000000..4ef07cc8 --- /dev/null +++ b/ls/libft/src/math/ft_lllen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lllen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:52:35 by jhalford #+# #+# */ +/* Updated: 2016/11/07 14:45:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_lllen(long long n) +{ + size_t i; + + i = 1; + while (n /= 10) + i++; + return (i); +} diff --git a/ls/libft/src/math/ft_lllen_base.c b/ls/libft/src/math/ft_lllen_base.c new file mode 100644 index 00000000..74b934eb --- /dev/null +++ b/ls/libft/src/math/ft_lllen_base.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lllen_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:52:35 by jhalford #+# #+# */ +/* Updated: 2016/11/07 14:46:15 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_lllen_base(long long n, int base) +{ + size_t i; + + i = 1; + while (n /= base) + i++; + return (i); +} diff --git a/ls/libft/src/math/ft_lltoa_base.c b/ls/libft/src/math/ft_lltoa_base.c new file mode 100644 index 00000000..0b2c6131 --- /dev/null +++ b/ls/libft/src/math/ft_lltoa_base.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lltoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:10:42 by jhalford #+# #+# */ +/* Updated: 2016/11/07 16:30:42 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *ft_flags(char *str, int *i, char *flags, int neg) +{ + if (neg) + str[*i++] = '-'; + else if (ft_strchr(flags, '+')) + str[*i++] = '+'; + else if (ft_strchr(flags, ' ')) + str[*i++] = ' '; + return (str); +} + +char *ft_lltoa_base(long long nbr, char *base, char *flags) +{ + int i; + int neg; + int base_size; + char *str; + + i = 0; + base_size = ft_strlen(base); + str = ft_strnew(ft_lllen_base(nbr, base_size) + 1); + neg = FT_NEG(nbr); + if (nbr == 0) + { + str[i++] = '0'; + str[i] = '\0'; + return (str); + } + while (nbr) + { + str[i++] = base[FT_ABS(nbr % base_size)]; + nbr = nbr / base_size; + } + str = ft_flags(str, &i, flags, neg); + str[i] = '\0'; + ft_strrev(str); + return (str); +} diff --git a/ls/libft/src/math/ft_uilen.c b/ls/libft/src/math/ft_uilen.c new file mode 100644 index 00000000..9e87ca1f --- /dev/null +++ b/ls/libft/src/math/ft_uilen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_uilen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:52:35 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:54:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_uilen(unsigned int n) +{ + size_t i; + + i = 1; + while (n /= 10) + i++; + return (i); +} diff --git a/ls/libft/src/math/ft_uitoa_base.c b/ls/libft/src/math/ft_uitoa_base.c new file mode 100644 index 00000000..a09cb11e --- /dev/null +++ b/ls/libft/src/math/ft_uitoa_base.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_uitoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:08:10 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:10:35 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t ft_size(unsigned int n, int base) +{ + size_t i; + + i = 1; + while (n /= base) + i++; + return (i); +} + +char *ft_uitoa_base(unsigned int nbr, char *base) +{ + int i; + int base_size; + char *str; + + i = 0; + base_size = ft_strlen(base); + str = ft_strnew(ft_size(nbr, base_size) + 1); + if (nbr == 0) + { + str[i++] = '0'; + str[i] = '\0'; + return (str); + } + while (nbr) + { + str[i++] = base[nbr % base_size]; + nbr = nbr / base_size; + } + str[i] = '\0'; + return (ft_strrev(str)); +} diff --git a/ls/libft/src/math/ft_ulltoa_base.c b/ls/libft/src/math/ft_ulltoa_base.c new file mode 100644 index 00000000..5c33f30d --- /dev/null +++ b/ls/libft/src/math/ft_ulltoa_base.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ulltoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:07:50 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:12:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t ft_size(unsigned long long n, int base) +{ + size_t i; + + i = 1; + while (n /= base) + i++; + return (i); +} + +char *ft_ulltoa_base(unsigned long long nbr, char *base) +{ + int i; + int base_size; + char *str; + + i = 0; + base_size = ft_strlen(base); + str = ft_strnew(ft_size(nbr, base_size) + 1); + if (nbr == 0) + { + str[i++] = '0'; + str[i] = '\0'; + return (str); + } + while (nbr) + { + str[i++] = base[nbr % base_size]; + nbr = nbr / base_size; + } + str[i] = '\0'; + return (ft_strrev(str)); +} diff --git a/ls/libft/src/mem/ft_bzero.c b/ls/libft/src/mem/ft_bzero.c new file mode 100644 index 00000000..6f1834b5 --- /dev/null +++ b/ls/libft/src/mem/ft_bzero.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:56:08 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:56:09 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + size_t i; + + i = -1; + while (++i < n) + *(char *)s++ = 0; +} diff --git a/ls/libft/src/mem/ft_memalloc.c b/ls/libft/src/mem/ft_memalloc.c new file mode 100644 index 00000000..505043f8 --- /dev/null +++ b/ls/libft/src/mem/ft_memalloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:25 by jhalford #+# #+# */ +/* Updated: 2016/11/11 17:40:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + void *addr; + size_t i; + + addr = malloc(size); + if (addr == NULL) + return (NULL); + i = -1; + while (++i < size) + ((char *)addr)[i] = 0; + return (addr); +} diff --git a/ls/libft/src/mem/ft_memccpy.c b/ls/libft/src/mem/ft_memccpy.c new file mode 100644 index 00000000..699c2836 --- /dev/null +++ b/ls/libft/src/mem/ft_memccpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:26 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:36:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memccpy(void *dst, const void *src, int c, size_t n) +{ + size_t i; + + i = -1; + while (++i < n) + { + *(char *)dst++ = *(char *)src; + if (*(char *)src++ == c) + return (dst); + } + return (NULL); +} diff --git a/ls/libft/src/mem/ft_memchr.c b/ls/libft/src/mem/ft_memchr.c new file mode 100644 index 00000000..96ddf009 --- /dev/null +++ b/ls/libft/src/mem/ft_memchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:28 by jhalford #+# #+# */ +/* Updated: 2016/11/11 17:41:21 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + void *a; + size_t i; + + i = -1; + a = (unsigned char *)s; + while (++i < n) + { + if (*(unsigned char *)a == (unsigned char)c) + return (a); + a++; + } + return (NULL); +} diff --git a/ls/libft/src/mem/ft_memcmp.c b/ls/libft/src/mem/ft_memcmp.c new file mode 100644 index 00000000..9dd827ee --- /dev/null +++ b/ls/libft/src/mem/ft_memcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:30 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:44:21 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + int cmp; + + i = -1; + while (++i < n) + { + cmp = *(unsigned char *)s1++ - *(unsigned char *)s2++; + if (cmp) + return (cmp); + } + return (cmp); +} diff --git a/ls/libft/src/mem/ft_memcpy.c b/ls/libft/src/mem/ft_memcpy.c new file mode 100644 index 00000000..0b95291e --- /dev/null +++ b/ls/libft/src/mem/ft_memcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:31 by jhalford #+# #+# */ +/* Updated: 2016/11/11 17:39:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + char *c1; + char *c2; + + if (n == 0 || dst == src) + return (dst); + c1 = (char *)dst; + c2 = (char *)src; + while (--n) + *c1++ = *c2++; + *c1 = *c2; + return (dst); +} diff --git a/ls/libft/src/mem/ft_memdel.c b/ls/libft/src/mem/ft_memdel.c new file mode 100644 index 00000000..52e6508b --- /dev/null +++ b/ls/libft/src/mem/ft_memdel.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:33 by jhalford #+# #+# */ +/* Updated: 2016/11/16 12:23:13 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memdel(void **ap) +{ + if (ap && *ap) + { + free(*ap); + *ap = NULL; + } +} diff --git a/ls/libft/src/mem/ft_memmove.c b/ls/libft/src/mem/ft_memmove.c new file mode 100644 index 00000000..904d1aca --- /dev/null +++ b/ls/libft/src/mem/ft_memmove.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:34 by jhalford #+# #+# */ +/* Updated: 2016/11/11 17:41:14 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + char *srcc; + char *dstc; + size_t i; + + i = -1; + srcc = (char *)src; + dstc = (char *)dst; + if (srcc < dstc) + while ((int)(--len) >= 0) + *(dstc + len) = *(srcc + len); + else + while (++i < len) + *(dstc + i) = *(srcc + i); + return (dst); +} diff --git a/ls/libft/src/mem/ft_memset.c b/ls/libft/src/mem/ft_memset.c new file mode 100644 index 00000000..cae3e999 --- /dev/null +++ b/ls/libft/src/mem/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:36 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:36 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + size_t i; + + i = -1; + while (++i < len) + ((unsigned char *)b)[i] = (unsigned char)c; + return (b); +} diff --git a/ls/libft/src/mem/ft_realloc.c b/ls/libft/src/mem/ft_realloc.c new file mode 100644 index 00000000..1f57254d --- /dev/null +++ b/ls/libft/src/mem/ft_realloc.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_realloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/11 17:37:53 by jhalford #+# #+# */ +/* Updated: 2016/11/16 17:56:17 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_realloc(void *data, int size) +{ + void *new; + + new = ft_memalloc(size); + ft_memcpy(new, data, ft_strlen(data)); + ft_memdel(&data); + return (new); +} diff --git a/ls/libft/src/misc/ft_debug.c b/ls/libft/src/misc/ft_debug.c new file mode 100644 index 00000000..c3a5341f --- /dev/null +++ b/ls/libft/src/misc/ft_debug.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_debug.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:45:16 by jhalford #+# #+# */ +/* Updated: 2016/11/07 15:43:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_debug(void) +{ + static int n = 0; + + n++; + ft_dprintf(2, "----------\n check %02i\n----------\n", n); +} diff --git a/ls/libft/src/path/ft_path_notdir.c b/ls/libft/src/path/ft_path_notdir.c new file mode 100644 index 00000000..b00d21fe --- /dev/null +++ b/ls/libft/src/path/ft_path_notdir.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_path_notdir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/04 11:45:07 by jhalford #+# #+# */ +/* Updated: 2016/11/23 15:46:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_path_notdir(char *path) +{ + char *slash; + char *ret; + + ret = path; + if ((slash = ft_strrchr(path, '/')) && slash != path) + ret = slash + 1; + return (ret); +} diff --git a/ls/libft/src/printing/ft_putaddr.c b/ls/libft/src/printing/ft_putaddr.c new file mode 100644 index 00000000..2ba4028b --- /dev/null +++ b/ls/libft/src/printing/ft_putaddr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putaddr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/21 15:13:34 by jhalford #+# #+# */ +/* Updated: 2016/11/21 15:13:35 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putaddr(void *a) +{ + ft_printf("%p", a); +} diff --git a/ls/libft/src/printing/ft_putchar.c b/ls/libft/src/printing/ft_putchar.c new file mode 100644 index 00000000..afd2d6bb --- /dev/null +++ b/ls/libft/src/printing/ft_putchar.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:37 by jhalford #+# #+# */ +/* Updated: 2016/11/04 13:11:49 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_putchar(int c) +{ + write(1, &c, 1); + return (0); +} diff --git a/ls/libft/src/printing/ft_putchar_fd.c b/ls/libft/src/printing/ft_putchar_fd.c new file mode 100644 index 00000000..1d1e9a4b --- /dev/null +++ b/ls/libft/src/printing/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:39 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:39 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/ls/libft/src/printing/ft_putendl.c b/ls/libft/src/printing/ft_putendl.c new file mode 100644 index 00000000..a0fc0b6b --- /dev/null +++ b/ls/libft/src/printing/ft_putendl.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:40 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char const *s) +{ + char nl; + + nl = '\n'; + write(1, s, ft_strlen(s)); + write(1, &nl, 1); +} diff --git a/ls/libft/src/printing/ft_putendl_fd.c b/ls/libft/src/printing/ft_putendl_fd.c new file mode 100644 index 00000000..9b33674c --- /dev/null +++ b/ls/libft/src/printing/ft_putendl_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:42 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:42 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + char nl; + + nl = '\n'; + write(fd, s, ft_strlen(s)); + write(fd, &nl, 1); +} diff --git a/ls/libft/src/printing/ft_putnbr.c b/ls/libft/src/printing/ft_putnbr.c new file mode 100644 index 00000000..d8aa57a4 --- /dev/null +++ b/ls/libft/src/printing/ft_putnbr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/02 21:25:03 by jhalford #+# #+# */ +/* Updated: 2016/08/04 21:28:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + if (n == -2147483648) + { + ft_putchar('-'); + ft_putchar('2'); + ft_putnbr(147483648); + return ; + } + else if (n < 0) + ft_putchar('-'); + n = FT_ABS(n); + if (n >= 10) + ft_putnbr(n / 10); + ft_putchar(n % 10 + '0'); +} diff --git a/ls/libft/src/printing/ft_putnbr_fd.c b/ls/libft/src/printing/ft_putnbr_fd.c new file mode 100644 index 00000000..da7cf739 --- /dev/null +++ b/ls/libft/src/printing/ft_putnbr_fd.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/02 21:25:03 by jhalford #+# #+# */ +/* Updated: 2016/08/04 21:28:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + { + ft_putchar_fd('-', fd); + ft_putchar_fd('2', fd); + ft_putnbr_fd(147483648, fd); + return ; + } + else if (n < 0) + ft_putchar_fd('-', fd); + n = FT_ABS(n); + if (n >= 10) + ft_putnbr_fd(n / 10, fd); + ft_putchar_fd(n % 10 + '0', fd); +} diff --git a/ls/libft/src/printing/ft_putstr.c b/ls/libft/src/printing/ft_putstr.c new file mode 100644 index 00000000..c4e16816 --- /dev/null +++ b/ls/libft/src/printing/ft_putstr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/03 16:13:07 by jhalford #+# #+# */ +/* Updated: 2016/08/25 17:03:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + write(1, s, ft_strlen(s)); +} diff --git a/ls/libft/src/printing/ft_putstr_fd.c b/ls/libft/src/printing/ft_putstr_fd.c new file mode 100644 index 00000000..ee82000f --- /dev/null +++ b/ls/libft/src/printing/ft_putstr_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:48 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:49 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char const *s, int fd) +{ + write(fd, s, ft_strlen(s)); +} diff --git a/ls/libft/src/sstr/ft_sstradd.c b/ls/libft/src/sstr/ft_sstradd.c new file mode 100644 index 00000000..f0ba213b --- /dev/null +++ b/ls/libft/src/sstr/ft_sstradd.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstradd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:03:58 by jhalford #+# #+# */ +/* Updated: 2016/11/14 16:31:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char **ft_sstradd(char **sstr, char *new) +{ + int i; + int size; + char **newlist; + + i = 0; + size = 0; + if (sstr) + while (sstr[size]) + size++; + if (!(newlist = (char **)malloc(sizeof(char *) * (size + 2)))) + return (NULL); + if (sstr) + while (sstr[i]) + { + newlist[i] = sstr[i]; + i++; + } + newlist[i++] = ft_strdup(new); + newlist[i] = NULL; + free(sstr); + return (newlist); +} diff --git a/ls/libft/src/sstr/ft_sstrdel.c b/ls/libft/src/sstr/ft_sstrdel.c new file mode 100644 index 00000000..f539d73e --- /dev/null +++ b/ls/libft/src/sstr/ft_sstrdel.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:04:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sstrdel(char **sstr, int index) +{ + int i; + + i = index; + while (sstr[i]) + { + sstr[i] = sstr[i + 1]; + i++; + } +} diff --git a/ls/libft/src/sstr/ft_sstrdup.c b/ls/libft/src/sstr/ft_sstrdup.c new file mode 100644 index 00000000..d79d1467 --- /dev/null +++ b/ls/libft/src/sstr/ft_sstrdup.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:04:13 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:04:13 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char **ft_sstrdup(char **list) +{ + int i; + int size; + char **cpy; + + i = 0; + size = 0; + while (list[size]) + size++; + cpy = (char **)malloc(sizeof(char *) * (size + 1)); + while (*list) + { + cpy[i++] = ft_strdup(*list); + list++; + } + return (cpy); +} diff --git a/ls/libft/src/sstr/ft_sstrfree.c b/ls/libft/src/sstr/ft_sstrfree.c new file mode 100644 index 00000000..14a833eb --- /dev/null +++ b/ls/libft/src/sstr/ft_sstrfree.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrfree.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/08 17:01:24 by jhalford #+# #+# */ +/* Updated: 2016/11/14 11:08:19 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sstrfree(char **sstr) +{ + int i; + + i = 0; + if (sstr) + { + while (sstr[i]) + { + ft_strdel(sstr + i); + i++; + } + ft_strdel(sstr + i); + free(sstr); + } +} diff --git a/ls/libft/src/sstr/ft_sstrprint.c b/ls/libft/src/sstr/ft_sstrprint.c new file mode 100644 index 00000000..a6b5f248 --- /dev/null +++ b/ls/libft/src/sstr/ft_sstrprint.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:04:15 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:04:15 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sstrprint(char **list, char sep) +{ + int i; + + i = 0; + while (list[i]) + { + ft_putstr(list[i++]); + if (list[i]) + ft_putchar(sep); + } +} diff --git a/ls/libft/src/sstr/ft_sstrsort.c b/ls/libft/src/sstr/ft_sstrsort.c new file mode 100644 index 00000000..180d4ea0 --- /dev/null +++ b/ls/libft/src/sstr/ft_sstrsort.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:03:37 by jhalford #+# #+# */ +/* Updated: 2016/11/23 14:46:54 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sstrsort(char **list, int (*cmp)()) +{ + int i; + char *tmp; + + i = 0; + while (list[i] && list[i + 1]) + { + if ((*cmp)(list[i], list[i + 1]) > 0) + { + tmp = list[i]; + list[i] = list[i + 1]; + list[i + 1] = tmp; + i = 0; + } + else + i++; + } +} diff --git a/ls/libft/src/str/ft_atoi.c b/ls/libft/src/str/ft_atoi.c new file mode 100644 index 00000000..711e3d68 --- /dev/null +++ b/ls/libft/src/str/ft_atoi.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/03 16:17:21 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:13:04 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_iswhitespace(char c) +{ + if (c == ' ' || c == '\t' || c == '\n') + return (1); + else if (c == '\v' || c == '\f' || c == '\r') + return (1); + return (0); +} + +int ft_atoi(const char *str) +{ + int i; + int res; + int sign; + + i = 0; + res = 0; + sign = 1; + while (ft_iswhitespace(str[i])) + i++; + if (str[i] == '-' || str[i] == '+') + { + if (str[i + 1] >= '0' && str[i + 1] <= '9') + { + sign = (str[i] == '+') ? 1 : -1; + i++; + } + else + return (0); + } + while (str[i] >= '0' && str[i] <= '9') + res = res * 10 + str[i++] - '0'; + res *= sign; + return (res); +} diff --git a/ls/libft/src/str/ft_convert_base.c b/ls/libft/src/str/ft_convert_base.c new file mode 100644 index 00000000..633a82ef --- /dev/null +++ b/ls/libft/src/str/ft_convert_base.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_convert_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 23:31:20 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:03:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int get_size(char *str) +{ + int i; + int j; + + i = 0; + while (str[i] != '\0') + { + if (str[i] == '+' || str[i] == '-') + return (0); + if (str[i] < 32 || str[i] > 126) + return (0); + j = 0; + while (j < i) + { + if (str[j] == str[i]) + return (0); + j++; + } + i++; + } + return (i); +} + +static int get_pos(char c, char *str) +{ + int i; + + i = 0; + while (c != str[i] && str[i]) + i++; + return (i); +} + +static int ft_check_str(char *str, char *base, int base_size) +{ + while (*str) + { + if (!(get_pos(*str, base) < base_size || *str == '-' || *str == '+')) + return (0); + str++; + } + return (1); +} + +char *ft_convert_base( + char *str, char *base_from, char *base_to, char *flags) +{ + int base_size; + int res; + int sign; + + base_size = get_size(base_from); + res = 0; + sign = 1; + if (!ft_check_str(str, base_from, base_size)) + return (ft_itoa_base(0, "0", flags)); + if (base_size > 1) + { + if (*str == '-' || *str == '+') + { + if (get_pos(*(str + 1), base_from) < base_size) + sign = (*str == '+') ? 1 : -1; + else + return (ft_itoa_base(0, "0", flags)); + str++; + } + while (get_pos(*str, base_from) < base_size) + res = res * base_size + sign * get_pos(*str++, base_from); + } + return (ft_itoa_base(res, base_to, flags)); +} diff --git a/ls/libft/src/str/ft_split_whitespaces.c b/ls/libft/src/str/ft_split_whitespaces.c new file mode 100644 index 00000000..869ced14 --- /dev/null +++ b/ls/libft/src/str/ft_split_whitespaces.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_whitespaces.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/08 09:33:37 by jhalford #+# #+# */ +/* Updated: 2016/08/20 23:23:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char **alloc_table(char **table, char *str) +{ + int i; + int n_words; + + i = 0; + n_words = 0; + while (FT_WS(str[i])) + i++; + while (str[i] != '\0') + { + i++; + if (FT_WS(str[i])) + { + n_words++; + while (FT_WS(str[i])) + i++; + } + } + if (!FT_WS(str[i - 1])) + n_words++; + table = (char**)malloc(sizeof(*table) * (n_words + 1)); + table[n_words] = 0x0; + return (table); +} + +char **alloc_words(char **table, char *str) +{ + int i; + int j; + int k; + + i = 0; + j = 0; + k = 0; + while (FT_WS(str[i])) + i++; + while (str[i] != '\0') + { + i++; + if (FT_WS(str[i]) || str[i] == '\0') + { + table[j] = (char*)malloc(sizeof(**table) * (k + 1)); + j++; + k = 0; + while (FT_WS(str[i])) + i++; + } + k++; + } + return (table); +} + +char **fill_table(char **table, char *str) +{ + int i; + int j; + int k; + + i = 0; + j = 0; + k = 0; + while (FT_WS(str[i])) + i++; + while (str[i] != '\0') + { + table[j][k] = str[i]; + i++; + k++; + if (FT_WS(str[i])) + { + table[j][k] = '\0'; + j++; + k = 0; + while (FT_WS(str[i])) + i++; + } + } + return (table); +} + +char **ft_split_whitespaces(char *str) +{ + char **table; + + table = NULL; + table = alloc_table(table, str); + table = alloc_words(table, str); + table = fill_table(table, str); + return (table); +} diff --git a/ls/libft/src/str/ft_str3join.c b/ls/libft/src/str/ft_str3join.c new file mode 100644 index 00000000..81bb69dc --- /dev/null +++ b/ls/libft/src/str/ft_str3join.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str3join.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:03:26 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:03:26 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_str3join(char const *s1, char const *s2, char const *s3) +{ + char *join; + int size; + + size = ft_strlen(s1) + ft_strlen(s2) + ft_strlen(s3); + join = ft_strnew(size + 1); + ft_strcpy(join, s1); + ft_strcat(join, s2); + ft_strcat(join, s3); + return (join); +} diff --git a/ls/libft/src/str/ft_strcat.c b/ls/libft/src/str/ft_strcat.c new file mode 100644 index 00000000..f376d850 --- /dev/null +++ b/ls/libft/src/str/ft_strcat.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:56:53 by jhalford #+# #+# */ +/* Updated: 2016/11/10 12:18:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *s1, const char *s2) +{ + size_t size; + size_t j; + + size = ft_strlen(s1); + j = 0; + while (s2 && s2[j]) + { + s1[size + j] = s2[j]; + j++; + } + s1[size + j] = '\0'; + return (s1); +} diff --git a/ls/libft/src/str/ft_strcatf.c b/ls/libft/src/str/ft_strcatf.c new file mode 100644 index 00000000..c4520d64 --- /dev/null +++ b/ls/libft/src/str/ft_strcatf.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcatf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 15:46:03 by jhalford #+# #+# */ +/* Updated: 2016/11/16 17:58:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcatf(char *s1, const char *s2) +{ + char buf[ft_strlen(s2)]; + + ft_strcpy(buf, s1); + ft_strcpy(s1, s2); + ft_strcat(s1, buf); + return (s1); +} diff --git a/ls/libft/src/str/ft_strchr.c b/ls/libft/src/str/ft_strchr.c new file mode 100644 index 00000000..7263e413 --- /dev/null +++ b/ls/libft/src/str/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:53 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:07:30 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + char *a; + + a = (char *)s; + while (*a) + { + if (*a == (char)c) + return (a); + a++; + } + if (*a == (char)c) + return (a); + return (NULL); +} diff --git a/ls/libft/src/str/ft_strclr.c b/ls/libft/src/str/ft_strclr.c new file mode 100644 index 00000000..834eb6f1 --- /dev/null +++ b/ls/libft/src/str/ft_strclr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:54 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:57:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + size_t size; + size_t i; + + size = ft_strlen(s); + i = -1; + while (++i < size) + s[i] = 0; +} diff --git a/ls/libft/src/str/ft_strcmp.c b/ls/libft/src/str/ft_strcmp.c new file mode 100644 index 00000000..641bc5b8 --- /dev/null +++ b/ls/libft/src/str/ft_strcmp.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:49:02 by jhalford #+# #+# */ +/* Updated: 2016/11/03 16:08:51 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + int i; + + i = 0; + while (*(s1 + i) && *(s1 + i) == *(s2 + i)) + i++; + return (*((unsigned char*)s1 + i) - *((unsigned char*)s2 + i)); +} diff --git a/ls/libft/src/str/ft_strcpy.c b/ls/libft/src/str/ft_strcpy.c new file mode 100644 index 00000000..95818fff --- /dev/null +++ b/ls/libft/src/str/ft_strcpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:48:12 by jhalford #+# #+# */ +/* Updated: 2016/08/20 23:37:18 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dst, const char *src) +{ + int i; + + i = 0; + while (src[i] != '\0') + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + return (dst); +} diff --git a/ls/libft/src/str/ft_strcut.c b/ls/libft/src/str/ft_strcut.c new file mode 100644 index 00000000..75b4d51b --- /dev/null +++ b/ls/libft/src/str/ft_strcut.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcut.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:04:37 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:04:37 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcut(char *str, char *cut) +{ + char *target; + + while ((target = ft_strstr(str, cut))) + { + ft_strcpy(target, target + ft_strlen(cut)); + } + return (str); +} diff --git a/ls/libft/src/str/ft_strdel.c b/ls/libft/src/str/ft_strdel.c new file mode 100644 index 00000000..19df79fe --- /dev/null +++ b/ls/libft/src/str/ft_strdel.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:00 by jhalford #+# #+# */ +/* Updated: 2016/11/21 18:03:21 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strdel(char **as) +{ + if (as) + { + free(*as); + *as = NULL; + } +} diff --git a/ls/libft/src/str/ft_strdup.c b/ls/libft/src/str/ft_strdup.c new file mode 100644 index 00000000..9233b26e --- /dev/null +++ b/ls/libft/src/str/ft_strdup.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:01 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:58:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *dup; + int size; + int i; + + i = 0; + size = ft_strlen(s1); + dup = (char*)malloc(sizeof(*dup) * (size + 1)); + while (s1[i] != '\0') + { + dup[i] = s1[i]; + i++; + } + dup[i] = '\0'; + return (dup); +} diff --git a/ls/libft/src/str/ft_strequ.c b/ls/libft/src/str/ft_strequ.c new file mode 100644 index 00000000..88e5580c --- /dev/null +++ b/ls/libft/src/str/ft_strequ.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:04 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:02:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + return (ft_strcmp(s1, s2) == 0); +} diff --git a/ls/libft/src/str/ft_strinsert.c b/ls/libft/src/str/ft_strinsert.c new file mode 100644 index 00000000..fcf8b1b6 --- /dev/null +++ b/ls/libft/src/str/ft_strinsert.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/21 18:21:57 by jhalford #+# #+# */ +/* Updated: 2016/11/21 18:21:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strinsert(char *str, char c, int n) +{ + char tmp[ft_strlen(str)]; + char *out; + + ft_strcpy(tmp, str + n); + str[n] = 0; + out = ft_str3join(str, (char[]){c, 0}, tmp); + return (out); +} diff --git a/ls/libft/src/str/ft_striter.c b/ls/libft/src/str/ft_striter.c new file mode 100644 index 00000000..8c309549 --- /dev/null +++ b/ls/libft/src/str/ft_striter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:13 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:58:13 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striter(char *s, void (*f)(char *)) +{ + size_t size; + size_t i; + + size = ft_strlen(s); + i = -1; + while (++i < size) + (*f)(s + i); +} diff --git a/ls/libft/src/str/ft_striteri.c b/ls/libft/src/str/ft_striteri.c new file mode 100644 index 00000000..80cdd8b5 --- /dev/null +++ b/ls/libft/src/str/ft_striteri.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:15 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:58:15 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + size_t size; + size_t i; + + size = ft_strlen(s); + i = -1; + while (++i < size) + (*f)(i, s + i); +} diff --git a/ls/libft/src/str/ft_strjoin.c b/ls/libft/src/str/ft_strjoin.c new file mode 100644 index 00000000..ece5a205 --- /dev/null +++ b/ls/libft/src/str/ft_strjoin.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:18 by jhalford #+# #+# */ +/* Updated: 2016/11/21 18:10:22 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *join; + + join = ft_strnew(ft_strlen(s1) + ft_strlen(s2) + 1); + ft_strcpy(join, s1); + ft_strcat(join, s2); + return (join); +} diff --git a/ls/libft/src/str/ft_strlcat.c b/ls/libft/src/str/ft_strlcat.c new file mode 100644 index 00000000..df94eea4 --- /dev/null +++ b/ls/libft/src/str/ft_strlcat.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:57:16 by jhalford #+# #+# */ +/* Updated: 2016/08/07 21:44:13 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t i; + size_t dst_size; + size_t src_size; + + dst_size = ft_strlen(dst); + src_size = ft_strlen(src); + i = 0; + while (src[i] != '\0' && ((dst_size + i) < (size - 1))) + { + dst[dst_size + i] = src[i]; + i++; + } + dst[dst_size + i] = '\0'; + return (src_size + ((dst_size < size) ? dst_size : size)); +} diff --git a/ls/libft/src/str/ft_strlen.c b/ls/libft/src/str/ft_strlen.c new file mode 100644 index 00000000..536f2bcf --- /dev/null +++ b/ls/libft/src/str/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:22 by jhalford #+# #+# */ +/* Updated: 2016/11/10 10:14:18 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + int i; + + i = 0; + if (!s) + return (0); + while (s[i]) + i++; + return (i); +} diff --git a/ls/libft/src/str/ft_strmap.c b/ls/libft/src/str/ft_strmap.c new file mode 100644 index 00000000..295214f0 --- /dev/null +++ b/ls/libft/src/str/ft_strmap.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:24 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:58:25 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + size_t size; + size_t i; + char *out; + + size = ft_strlen(s); + out = (char *)malloc(sizeof(char) * (size + 1)); + if (out == NULL) + return (NULL); + i = -1; + while (++i < size) + out[i] = (*f)(s[i]); + return (out); +} diff --git a/ls/libft/src/str/ft_strmapi.c b/ls/libft/src/str/ft_strmapi.c new file mode 100644 index 00000000..0d6fab4c --- /dev/null +++ b/ls/libft/src/str/ft_strmapi.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:28 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:58:29 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + size_t size; + size_t i; + char *out; + + size = ft_strlen(s); + out = (char *)malloc(sizeof(char) * (size + 1)); + if (out == NULL) + return (NULL); + i = -1; + while (++i < size) + out[i] = (*f)(i, s[i]); + return (out); +} diff --git a/ls/libft/src/str/ft_strncat.c b/ls/libft/src/str/ft_strncat.c new file mode 100644 index 00000000..31ab2262 --- /dev/null +++ b/ls/libft/src/str/ft_strncat.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:57:07 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:02:27 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *s1, const char *s2, size_t n) +{ + size_t size; + size_t j; + + size = ft_strlen(s1); + j = 0; + while (s2[j] != '\0' && j < n) + { + s1[size + j] = s2[j]; + j++; + } + s1[size + j] = '\0'; + return (s1); +} diff --git a/ls/libft/src/str/ft_strncmp.c b/ls/libft/src/str/ft_strncmp.c new file mode 100644 index 00000000..57b55b5f --- /dev/null +++ b/ls/libft/src/str/ft_strncmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:49:12 by jhalford #+# #+# */ +/* Updated: 2016/11/03 16:11:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + int i; + + i = 0; + while (*(s1 + i) && *(s1 + i) == *(s2 + i) && i < (int)n) + i++; + if (i < (int)n) + return (*((unsigned char*)s1 + i) - *((unsigned char*)s2 + i)); + else + return (0); +} diff --git a/ls/libft/src/str/ft_strncpy.c b/ls/libft/src/str/ft_strncpy.c new file mode 100644 index 00000000..85dd41c7 --- /dev/null +++ b/ls/libft/src/str/ft_strncpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:48:21 by jhalford #+# #+# */ +/* Updated: 2016/08/07 10:48:25 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dst, const char *src, size_t len) +{ + size_t i; + + i = 0; + while (src[i] != '\0' && i < len) + { + dst[i] = src[i]; + i++; + } + while (i < len) + { + dst[i] = '\0'; + i++; + } + return (dst); +} diff --git a/ls/libft/src/str/ft_strnequ.c b/ls/libft/src/str/ft_strnequ.c new file mode 100644 index 00000000..5dfd6005 --- /dev/null +++ b/ls/libft/src/str/ft_strnequ.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:32 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:02:36 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + return (ft_strncmp(s1, s2, n) == 0); +} diff --git a/ls/libft/src/str/ft_strnew.c b/ls/libft/src/str/ft_strnew.c new file mode 100644 index 00000000..4a60408f --- /dev/null +++ b/ls/libft/src/str/ft_strnew.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:34 by jhalford #+# #+# */ +/* Updated: 2016/11/03 14:58:35 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnew(size_t size) +{ + char *addr; + size_t i; + + addr = (char *)malloc(size + 1); + if (addr == NULL) + return (NULL); + i = -1; + while (++i <= size) + addr[i] = '\0'; + return (addr); +} diff --git a/ls/libft/src/str/ft_strnstr.c b/ls/libft/src/str/ft_strnstr.c new file mode 100644 index 00000000..efa7e54f --- /dev/null +++ b/ls/libft/src/str/ft_strnstr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:36 by jhalford #+# #+# */ +/* Updated: 2016/11/03 16:34:42 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + int j; + + i = -1; + if (!*little) + return ((char *)big); + while (big[++i] && i < len) + { + j = 0; + while (big[i + j] == little[j] && i + j < len) + { + j++; + if (!little[j]) + return ((char *)big + i); + } + } + return (NULL); +} diff --git a/ls/libft/src/str/ft_strrchr.c b/ls/libft/src/str/ft_strrchr.c new file mode 100644 index 00000000..e41d4566 --- /dev/null +++ b/ls/libft/src/str/ft_strrchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:38 by jhalford #+# #+# */ +/* Updated: 2016/11/03 15:08:33 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + char *a; + int i; + + a = (char *)s; + i = ft_strlen(a); + while (i >= 0) + { + if (a[i] == (char)c) + return (a + i); + i--; + } + return (NULL); +} diff --git a/ls/libft/src/str/ft_strrev.c b/ls/libft/src/str/ft_strrev.c new file mode 100644 index 00000000..73884204 --- /dev/null +++ b/ls/libft/src/str/ft_strrev.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:01:36 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:01:36 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrev(char *str) +{ + int len; + char tmp; + int i; + + i = 0; + len = 0; + while (str[len] != '\0') + len++; + while (i < len / 2) + { + tmp = str[len - (i + 1)]; + str[len - (i + 1)] = str[i]; + str[i] = tmp; + i++; + } + return (str); +} diff --git a/ls/libft/src/str/ft_strsplit.c b/ls/libft/src/str/ft_strsplit.c new file mode 100644 index 00000000..9264c2ee --- /dev/null +++ b/ls/libft/src/str/ft_strsplit.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:40 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:07:01 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int countwords(char const *s, char c) +{ + if (c == '\0') + return ((*s == '\0') ? 0 : 1); + while (*s == c) + s++; + if (*s == '\0') + return (0); + while (*s != c && *s != '\0') + s++; + return (1 + countwords(s, c)); +} + +char **ft_strsplit(char const *s, char c) +{ + char **arr; + int i; + int j; + int w; + + if (!s) + return (0); + w = countwords(s, c); + if ((arr = (char **)malloc((w + 1) * sizeof(char *)))) + { + i = 0; + while (i < w) + { + while (*s == c) + ++s; + j = 0; + while (*(s + j) != c) + ++j; + if ((arr[i] = ft_strnew(j))) + ft_strncpy(arr[i++], s, j); + s += j; + } + arr[i] = 0; + } + return (arr); +} diff --git a/ls/libft/src/str/ft_strstr.c b/ls/libft/src/str/ft_strstr.c new file mode 100644 index 00000000..bf8ab37a --- /dev/null +++ b/ls/libft/src/str/ft_strstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/07 10:48:35 by jhalford #+# #+# */ +/* Updated: 2016/11/03 16:28:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *big, const char *little) +{ + int i; + int j; + char *a; + + a = (char *)big; + i = 0; + if (!*little) + return (a); + while (a[i]) + { + j = 0; + while (a[i + j] == little[j]) + { + j++; + if (!little[j]) + return (a + i); + } + i++; + } + return (NULL); +} diff --git a/ls/libft/src/str/ft_strsub.c b/ls/libft/src/str/ft_strsub.c new file mode 100644 index 00000000..f4259a58 --- /dev/null +++ b/ls/libft/src/str/ft_strsub.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:43 by jhalford #+# #+# */ +/* Updated: 2016/11/25 11:31:36 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *out; + size_t i; + + if (!(out = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + i = -1; + while (++i < len) + out[i] = s[i + start]; + out[i] = '\0'; + return (out); +} diff --git a/ls/libft/src/str/ft_strtrim.c b/ls/libft/src/str/ft_strtrim.c new file mode 100644 index 00000000..844ac335 --- /dev/null +++ b/ls/libft/src/str/ft_strtrim.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:58:45 by jhalford #+# #+# */ +/* Updated: 2016/11/04 13:11:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s) +{ + char *out; + size_t size; + + out = ft_strdup(s); + while (*out && FT_WS(*out)) + out++; + size = ft_strlen(out); + while (size - 1 && FT_WS(out[size - 1])) + { + size--; + out[size] = '\0'; + } + return (out); +} diff --git a/ls/libft/src/time/ft_mytime_free.c b/ls/libft/src/time/ft_mytime_free.c new file mode 100644 index 00000000..08c06dde --- /dev/null +++ b/ls/libft/src/time/ft_mytime_free.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mytime_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 11:45:29 by jhalford #+# #+# */ +/* Updated: 2016/11/25 11:48:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_mytime_free(t_mytime **time) +{ + ft_strdel(&(*time)->year); + ft_strdel(&(*time)->month); + ft_strdel(&(*time)->day); + ft_strdel(&(*time)->hour); + ft_strdel(&(*time)->min); + ft_strdel(&(*time)->sec); + ft_memdel((void **)time); +} diff --git a/ls/libft/src/time/ft_mytime_get.c b/ls/libft/src/time/ft_mytime_get.c new file mode 100644 index 00000000..730c4e68 --- /dev/null +++ b/ls/libft/src/time/ft_mytime_get.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getstrtime.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 11:34:56 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:18:39 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_mytime *ft_mytime_get(time_t epoch) +{ + char *date; + t_mytime *time; + + time = (t_mytime*)malloc(sizeof(*time)); + date = ctime(&epoch); + date[ft_strlen(date) - 1] = 0; + time->year = ft_isdigit(date[20]) ? + ft_strsub(date, 20, 4) : ft_strsub(date, 24, 5); + time->month = ft_strsub(date, 4, 3); + time->day = ft_strsub(date, 8, 2); + time->hour = ft_strsub(date, 11, 2); + time->min = ft_strsub(date, 14, 2); + time->sec = ft_strsub(date, 17, 2); + return (time); +} diff --git a/ls/libft/src/time/ft_time_isrecent.c b/ls/libft/src/time/ft_time_isrecent.c new file mode 100644 index 00000000..04712152 --- /dev/null +++ b/ls/libft/src/time/ft_time_isrecent.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_time_isrecent.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:01:04 by jhalford #+# #+# */ +/* Updated: 2016/11/25 11:43:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_time_isrecent(time_t event) +{ + time_t now; + + now = time(&now); + if (now - event >= 0 && now - event <= 6 * 365 / 12 * 24 * 60 * 60) + return (1); + else + return (0); +} diff --git a/ls/libft/src/xattr/ft_xattr_count.c b/ls/libft/src/xattr/ft_xattr_count.c new file mode 100644 index 00000000..01b96eb5 --- /dev/null +++ b/ls/libft/src/xattr/ft_xattr_count.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_xattr_count.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:00:52 by jhalford #+# #+# */ +/* Updated: 2016/11/25 17:22:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_xattr_count(char *path) +{ + ssize_t listlen; + char list[FT_XATTR_SIZE]; + int i; + int count; + + i = 0; + ft_bzero(list, FT_XATTR_SIZE); + listlen = listxattr(path, list, FT_XATTR_SIZE, XATTR_NOFOLLOW); + if (listlen == -1) + return (-1); + count = 0; + while (i < listlen) + { + i += ft_strlen(list) + 1; + count++; + } + return (count); +} diff --git a/ls/libft/src/xattr/ft_xattr_print.c b/ls/libft/src/xattr/ft_xattr_print.c new file mode 100644 index 00000000..de7a009f --- /dev/null +++ b/ls/libft/src/xattr/ft_xattr_print.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_xattr_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 18:00:43 by jhalford #+# #+# */ +/* Updated: 2016/11/03 18:00:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_xattr_print(char *path) +{ + ssize_t listlen; + ssize_t valuelen; + char list[FT_XATTR_SIZE]; + char value[FT_XATTR_SIZE]; + int i; + + i = 0; + listlen = listxattr(path, list, FT_XATTR_SIZE, XATTR_NOFOLLOW); + if (listlen == -1) + return (1); + while (i < listlen) + { + valuelen = getxattr(path, list + i, value, + FT_XATTR_SIZE, 0, XATTR_NOFOLLOW); + if (valuelen == -1) + ft_printf("couldn't get value\n"); + else + ft_printf("%s:\n%s\n", list + i, value); + i += ft_strlen(list) + 1; + } + return (0); +} diff --git a/ls/src/ft_dir_get_ents.c b/ls/src/ft_dir_get_ents.c new file mode 100644 index 00000000..cee49317 --- /dev/null +++ b/ls/src/ft_dir_get_ents.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_dirs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 14:58:49 by jhalford #+# #+# */ +/* Updated: 2016/11/23 16:20:39 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +t_list *ft_dir_get_ents(t_lsdata *topdir, int *opts) +{ + t_list *ent; + struct dirent *dirent; + t_lsdata data; + DIR *stream; + char *prepath; + + ent = NULL; + if (!(stream = opendir(topdir->path))) + { + *opts |= OPTS_ERR_A; + return (NULL); + } + prepath = ft_strjoin(topdir->path, "/"); + while ((dirent = readdir(stream))) + { + data.path = ft_strjoin(prepath, dirent->d_name); + lstat(data.path, &data.stat); + data.dirent = ft_memalloc(sizeof(*dirent) + 1); + ft_memcpy(data.dirent, dirent, sizeof(*dirent)); + ft_lstadd(&ent, ft_lstnew(&data, sizeof(data))); + } + ft_strdel(&prepath); + closedir(stream); + return (ent); +} diff --git a/ls/src/ft_ent_free.c b/ls/src/ft_ent_free.c new file mode 100644 index 00000000..4e2a29fc --- /dev/null +++ b/ls/src/ft_ent_free.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ent_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/08 11:10:13 by jhalford #+# #+# */ +/* Updated: 2016/11/21 14:22:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_ent_free(void *ptr, size_t size) +{ + t_lsdata *data; + + (void)size; + data = (t_lsdata *)ptr; + if (data) + { + if (data->path) + ft_strdel(&data->path); + if (data->dirent) + ft_memdel((void **)&data->dirent); + free(data); + } +} diff --git a/ls/src/ft_ent_get_dirs.c b/ls/src/ft_ent_get_dirs.c new file mode 100644 index 00000000..1e7ebe2a --- /dev/null +++ b/ls/src/ft_ent_get_dirs.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ent_get_dirs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/16 15:46:27 by jhalford #+# #+# */ +/* Updated: 2016/11/16 18:18:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +t_list *ft_ent_get_dirs(t_list **ent) +{ + t_lsdata *data; + struct dirent *dirent; + t_list *dir; + t_list *lst; + t_list *tmp; + + dir = NULL; + lst = *ent; + while (lst) + { + tmp = lst; + data = lst->content; + dirent = data->dirent; + lst = lst->next; + if (dirent->d_type == DT_DIR + && ft_strcmp(ft_path_notdir(data->path), ".") + && ft_strcmp(ft_path_notdir(data->path), "..")) + { + ft_lsteadd(&dir, ft_lst_removeif(ent, tmp->content, &ft_addrcmp)); + } + } + return (dir); +} diff --git a/ls/src/ft_ent_has_dir.c b/ls/src/ft_ent_has_dir.c new file mode 100644 index 00000000..72e696d5 --- /dev/null +++ b/ls/src/ft_ent_has_dir.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ent_has_dir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/16 15:42:54 by jhalford #+# #+# */ +/* Updated: 2016/11/23 18:24:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +int ft_ent_has_dir(t_list *ent) +{ + t_lsdata *data; + struct dirent *dirent; + + while (ent) + { + data = ent->content; + dirent = data->dirent; + if (dirent->d_type == DT_DIR + && ft_strcmp(ft_path_notdir(data->path), ".") + && ft_strcmp(ft_path_notdir(data->path), "..")) + { + return (1); + } + ent = ent->next; + } + return (0); +} diff --git a/ls/src/ft_ls.c b/ls/src/ft_ls.c new file mode 100644 index 00000000..f5b91cd3 --- /dev/null +++ b/ls/src/ft_ls.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ls.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 14:57:21 by jhalford #+# #+# */ +/* Updated: 2016/11/25 12:15:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_ls_dirs(t_list *dir, int opts) +{ + t_list *dir_r; + t_list *ent; + t_lsdata *dirdata; + t_list *tmp; + + ft_ent_sort(&dir, opts); + ent = NULL; + while (dir) + { + dirdata = dir->content; + tmp = dir; + dir = dir->next; + ft_lstdel(&ent, &ft_ent_free); + ent = ft_dir_get_ents(dirdata, &opts); + ft_ls_files(&ent, &dir, dirdata, &opts); + ft_lstdelone(&tmp, &ft_ent_free); + if (opts & OPTS_UR && !(opts & OPTS_LD)) + { + dir_r = ft_ent_get_dirs(&ent); + ft_lst_merge(&dir_r, dir); + dir = dir_r; + } + } + ft_lstdel(&ent, &ft_ent_free); +} + +void ft_ls_files(t_list **ent, t_list **dir, t_lsdata *topdir, int *opts) +{ + if (ent) + { + if (topdir) + ft_ent_filter(ent, *opts); + ft_ent_sort(ent, *opts); + ft_ent_print(*ent, opts, topdir, *dir); + } +} diff --git a/ls/src/ft_ls_color.c b/ls/src/ft_ls_color.c new file mode 100644 index 00000000..427b8d77 --- /dev/null +++ b/ls/src/ft_ls_color.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ls_color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 13:39:11 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:57:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_ls_color(mode_t m) +{ + t_color color; + + ft_color_mk(&color, FG_DEFAULT, BG_DEFAULT); + ft_color_mkif(&color, S_ISDIR(m), FG_BLUE, BG_DEFAULT); + ft_color_mkif(&color, S_ISLNK(m), FG_MAGENTA, BG_DEFAULT); + ft_color_mkif(&color, S_ISLNK(m), FG_MAGENTA, BG_DEFAULT); + ft_color_mkif(&color, S_ISSOCK(m), FG_GREEN, BG_DEFAULT); + ft_color_mkif(&color, S_ISFIFO(m), FG_YELLOW, BG_DEFAULT); + ft_color_mkif(&color, S_ISREG(m) && (S_IXUGO & m), FG_RED, BG_DEFAULT); + ft_color_mkif(&color, S_ISBLK(m), FG_BLUE, BG_CYAN); + ft_color_mkif(&color, S_ISCHR(m), FG_BLUE, BG_YELLOW); + ft_color_mkif(&color, S_ISREG(m) && (S_IXUGO & m) && (S_ISGID & m), + FG_BLACK, BG_CYAN); + ft_color_mkif(&color, S_ISREG(m) && (S_IXUGO & m) && (S_ISUID & m), + FG_BLACK, BG_RED); + ft_color_mkif(&color, S_ISDIR(m) && (S_IWOTH & m) && (S_ISVTX & m), + FG_BLACK, BG_GREEN); + ft_color_mkif(&color, S_ISDIR(m) && (S_IWOTH & m) && !(S_ISVTX & m), + FG_BLACK, BG_YELLOW); + ft_color_set(color); +} diff --git a/ls/src/ft_ls_long.c b/ls/src/ft_ls_long.c new file mode 100644 index 00000000..8c9fec6f --- /dev/null +++ b/ls/src/ft_ls_long.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ls_long.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 14:58:05 by jhalford #+# #+# */ +/* Updated: 2016/11/25 14:18:29 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +int ft_ls_long(t_list *ent, t_lsdata *topdir, int opts) +{ + t_pads pads; + + if (topdir) + ft_ls_long_total(ent); + if (ft_ls_long_pads(ent, &pads)) + return (1); + while (ent) + { + if (ft_ls_long_print(ent, topdir, pads, opts)) + return (1); + ent = ent->next; + } + return (0); +} + +int ft_ls_long_print(t_list *ent, t_lsdata *topdir, t_pads pads, int opts) +{ + t_lsdata *data; + struct stat stat; + struct dirent *dirent; + + data = ent->content; + stat = data->stat; + dirent = data->dirent; + ent = ent->next; + ft_ls_long_type(stat.st_mode); + ft_ls_long_rights(stat.st_mode); + if (ft_ls_long_xattr(stat.st_mode, data->path)) + return (1); + if (ft_ls_long_middle(&stat, &pads, opts)) + return (1); + ft_ls_long_date(&stat, opts); + ft_printf(" "); + ft_lsdata_filename(data, topdir, opts); + if (ft_ls_long_lnk(data)) + return (1); + return (0); +} diff --git a/ls/src/ft_ls_postname.c b/ls/src/ft_ls_postname.c new file mode 100644 index 00000000..7ace1c60 --- /dev/null +++ b/ls/src/ft_ls_postname.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ls_postname.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 14:19:17 by jhalford #+# #+# */ +/* Updated: 2016/11/25 15:59:56 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_ls_postname(mode_t m, int opts) +{ + char c; + + c = 0; + if (opts & OPTS_UF) + { + c = S_ISDIR(m) ? '/' : c; + c = S_ISREG(m) && (S_IXUGO & m) ? '*' : c; + c = S_ISLNK(m) ? '@' : c; + c = S_ISSOCK(m) ? '=' : c; + c = S_ISWHT(m) ? '%' : c; + c = S_ISFIFO(m) ? '|' : c; + } + else if (opts & OPTS_LP && S_ISDIR(m)) + c = '/'; + if (c) + ft_putchar(c); +} diff --git a/ls/src/ft_ls_short.c b/ls/src/ft_ls_short.c new file mode 100644 index 00000000..d0a4df59 --- /dev/null +++ b/ls/src/ft_ls_short.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ls_short.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 14:56:28 by jhalford #+# #+# */ +/* Updated: 2016/11/23 18:50:46 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_ls_short(t_list *ent, t_lsdata *topdir, int opts) +{ + while (ent) + { + ft_lsdata_filename(ent->content, topdir, opts); + ft_printf("\n"); + ent = ent->next; + } +} diff --git a/ls/src/lib_ent.c b/ls/src/lib_ent.c new file mode 100644 index 00000000..880752cc --- /dev/null +++ b/ls/src/lib_ent.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_dirents.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 15:00:05 by jhalford #+# #+# */ +/* Updated: 2016/11/25 18:27:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_ent_filter(t_list **ent, int opts) +{ + if (!(opts & (OPTS_LA | OPTS_LF))) + { + if (!(opts & OPTS_UA)) + ft_lst_delif(ent, ".", &ft_lsdata_cmp0, &ft_ent_free); + else + ft_lst_delif(ent, ".", &ft_lsdata_cmpa, &ft_ent_free); + } +} + +void ft_ent_sort(t_list **ent, int opts) +{ + int (*ft_sort)(); + + ft_lst_reverse(ent); + ft_sort = &ft_lsdata_cmp_name; + if (opts & OPTS_LF) + return ; + if (opts & OPTS_US) + ft_sort = &ft_lsdata_cmp_size; + else if (opts & OPTS_LT) + { + if (opts & OPTS_LC) + ft_sort = &ft_cmp_ctime; + else if (opts & OPTS_LU) + ft_sort = &ft_cmp_atime; + else if (opts & OPTS_UU) + ft_sort = &ft_cmp_btime; + else + ft_sort = &ft_cmp_mtime; + } + ft_lstsort(ent, ft_sort); + if (opts & OPTS_LR) + ft_lst_reverse(ent); +} + +void ft_ent_print(t_list *ent, int *opts, t_lsdata *topdir, t_list *nextdir) +{ + if (topdir) + { + if (*opts & OPTS_HEAD) + ft_printf("%s:\n", topdir->path); + else + *opts |= OPTS_HEAD; + } + if (ent) + { + if (*opts & (OPTS_LL | OPTS_LG | OPTS_LO)) + ft_ls_long(ent, topdir, *opts); + else + ft_ls_short(ent, topdir, *opts); + } + else if (*opts & OPTS_ERR_A) + { + *opts &= ~OPTS_ERR_A; + ft_error_access(topdir->path); + } + if ((!topdir && nextdir && ent) + || (topdir && nextdir) + || (((*opts & OPTS_UR) && !(*opts & OPTS_LD) + && ent && ft_ent_has_dir(ent)))) + ft_putchar('\n'); +} diff --git a/ls/src/lib_error.c b/ls/src/lib_error.c new file mode 100644 index 00000000..f26c3ed9 --- /dev/null +++ b/ls/src/lib_error.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 14:57:43 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:57:23 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_error_option(char c) +{ + ft_dprintf(2, LS_MSG_ILLEGAL_OPT, c); + ft_dprintf(2, LS_MSG_USAGE, LS_LEGAL_OPTS); +} + +void ft_error_access(char *s) +{ + ft_dprintf(2, "ls: "); + perror(ft_path_notdir(s)); +} + +void ft_error_dir(char *s) +{ + ft_dprintf(2, "ls: "); + if (*s) + perror(s); + else + { + perror("fts_open"); + exit(1); + } +} diff --git a/ls/src/lib_ls_long.c b/ls/src/lib_ls_long.c new file mode 100644 index 00000000..0d10bcf3 --- /dev/null +++ b/ls/src/lib_ls_long.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_ls_long.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 14:59:51 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:57:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +int ft_ls_long_middle(struct stat *stat, t_pads *pads, int opts) +{ + struct passwd *pwd; + struct group *grp; + + if ((pwd = getpwuid(stat->st_uid)) == NULL) + return (1); + if ((grp = getgrgid(stat->st_gid)) == NULL) + return (1); + ft_printf(" %*hu ", pads->nlink, stat->st_nlink); + if (!(opts & OPTS_LG)) + ft_printf("%-*s", pads->name, pwd->pw_name); + if (!(opts & (OPTS_LO | OPTS_LG))) + ft_putstr(" "); + if (!(opts & OPTS_LO)) + ft_printf("%-*s", pads->gr_name, grp->gr_name); + if (S_ISBLK(stat->st_mode) || S_ISCHR(stat->st_mode)) + ft_printf(" % *i,% *i", + pads->major + 1, major(stat->st_rdev), + pads->minor + 1, minor(stat->st_rdev)); + else + ft_printf(" %*lld", pads->size, stat->st_size); + return (0); +} + +void ft_ls_long_date(struct stat *stat, int opts) +{ + time_t st_time; + t_mytime *time; + + if (opts & OPTS_LC) + st_time = stat->st_ctime; + else if (opts & OPTS_LU) + st_time = stat->st_atime; + else if (opts & OPTS_UU) + st_time = stat->st_birthtime; + else + st_time = stat->st_mtime; + time = ft_mytime_get(st_time); + if (opts & OPTS_UT) + ft_printf(" %s %s %s:%s:%s %s", time->month, time->day, time->hour, + time->min, time->sec, time->year); + else if (ft_time_isrecent(st_time)) + ft_printf(" %s %s %s:%s", + time->month, time->day, time->hour, time->min); + else + ft_printf(" %s %s %s", time->month, time->day, time->year); + ft_mytime_free(&time); +} + +void ft_ls_long_type(mode_t m) +{ + char type; + + type = '\0'; + type = S_ISREG(m) ? '-' : type; + type = S_ISDIR(m) ? 'd' : type; + type = S_ISFIFO(m) ? 'p' : type; + type = S_ISSOCK(m) ? 's' : type; + type = S_ISCHR(m) ? 'c' : type; + type = S_ISBLK(m) ? 'b' : type; + type = S_ISLNK(m) ? 'l' : type; + ft_printf("%c", type); +} diff --git a/ls/src/lib_ls_long2.c b/ls/src/lib_ls_long2.c new file mode 100644 index 00000000..0432fff1 --- /dev/null +++ b/ls/src/lib_ls_long2.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_ls_long2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 15:01:01 by jhalford #+# #+# */ +/* Updated: 2016/11/27 13:08:09 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_ls_long_rights(mode_t m) +{ + ft_putchar(m & S_IRUSR ? 'r' : '-'); + ft_putchar(m & S_IWUSR ? 'w' : '-'); + if (m & S_ISUID) + ft_putchar(m & S_IXUSR ? 's' : 'S'); + else + ft_putchar(m & S_IXUSR ? 'x' : '-'); + ft_putchar(m & S_IRGRP ? 'r' : '-'); + ft_putchar(m & S_IWGRP ? 'w' : '-'); + if (m & S_ISGID) + ft_putchar(m & S_IXGRP ? 's' : 'S'); + else + ft_putchar(m & S_IXGRP ? 'x' : '-'); + ft_putchar(m & S_IROTH ? 'r' : '-'); + ft_putchar(m & S_IWOTH ? 'w' : '-'); + if (m & S_ISVTX) + ft_putchar(m & S_IXOTH ? 't' : 'T'); + else + ft_putchar(m & S_IXOTH ? 'x' : '-'); +} + +int ft_ls_long_lnk(t_lsdata *data) +{ + int ret; + char buf[1024]; + + if (S_ISLNK(data->stat.st_mode)) + { + if ((ret = readlink(data->path, buf, 1024)) < 0) + return (-1); + buf[ret] = '\0'; + ft_printf(" -> %s\n", buf); + } + else + ft_putendl(""); + return (0); +} + +int ft_ls_long_xattr(mode_t m, char *path) +{ + char x; + acl_t acl; + + x = ' '; + if (!S_ISBLK(m) && !S_ISCHR(m) && ft_xattr_count(path) > 0) + x = '@'; + else if ((acl = acl_get_file(path, ACL_TYPE_EXTENDED))) + { + x = '+'; + acl_free((void*)acl); + } + ft_putchar(x); + return (0); +} + +void ft_ls_long_total(t_list *ent) +{ + struct stat stat; + t_lsdata *data; + int total; + + total = 0; + while (ent) + { + data = ent->content; + stat = data->stat; + ent = ent->next; + total += stat.st_blocks; + } + ft_printf("total %i\n", total); +} + +#define FT_MAX_WR(a,b) a = FT_MAX(a,b) + +int ft_ls_long_pads(t_list *ent, t_pads *pads) +{ + struct passwd *pwd; + struct group *grp; + struct stat stat; + + ft_bzero(pads, sizeof(*pads)); + while (ent) + { + stat = ((t_lsdata*)ent->content)->stat; + ent = ent->next; + if (!(pwd = getpwuid(stat.st_uid))) + return (1); + if (!(grp = getgrgid(stat.st_gid))) + return (1); + FT_MAX_WR(pads->name, (int)ft_strlen(pwd->pw_name)); + FT_MAX_WR(pads->gr_name, (int)ft_strlen(grp->gr_name)); + FT_MAX_WR(pads->nlink, (int)ft_uilen(stat.st_nlink)); + FT_MAX_WR(pads->size, (int)ft_ilen(stat.st_size)); + if (S_ISCHR(stat.st_mode) || S_ISBLK(stat.st_mode)) + FT_MAX_WR(pads->minor, (int)ft_ilen(minor(stat.st_rdev))); + if (S_ISCHR(stat.st_mode) || S_ISBLK(stat.st_mode)) + FT_MAX_WR(pads->major, (int)ft_ilen(major(stat.st_rdev))); + } + pads->size = FT_MAX(pads->size, pads->minor ? + pads->minor + pads->major + 3 : pads->size); + return (0); +} diff --git a/ls/src/lib_lsdata.c b/ls/src/lib_lsdata.c new file mode 100644 index 00000000..d33763ab --- /dev/null +++ b/ls/src/lib_lsdata.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_lsdata.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 14:59:09 by jhalford #+# #+# */ +/* Updated: 2016/11/27 11:15:37 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +void ft_lsdata_filename(t_lsdata *data, t_lsdata *topdir, int opts) +{ + struct dirent *dirent; + + dirent = data->dirent; + if (opts & OPTS_UG && isatty(1)) + ft_ls_color(data->stat.st_mode); + if (data->path) + ft_printf("%s", topdir ? ft_path_notdir(data->path) : data->path); + else if (dirent && *dirent->d_name) + ft_printf("%s", dirent->d_name); + if (opts & OPTS_UG && isatty(1)) + ft_color_reset(); + ft_ls_postname(data->stat.st_mode, opts); +} + +int ft_lsdata_cmp_name(t_lsdata *dat1, t_lsdata *dat2) +{ + return (ft_strcmp(dat1->path, dat2->path)); +} + +int ft_lsdata_cmp0(t_lsdata *data, char *dataref) +{ + char *filename; + + if (data->path) + filename = ft_path_notdir(data->path); + else + filename = data->dirent->d_name; + return (*filename != *dataref); +} + +int ft_lsdata_cmpa(t_lsdata *data, char *dataref) +{ + char *filename; + + (void)dataref; + if (data->path) + filename = ft_path_notdir(data->path); + else + filename = data->dirent->d_name; + if (!ft_strcmp(filename, ".") || !ft_strcmp(filename, "..")) + return (0); + return (1); +} + +int ft_lsdata_cmp_size(t_lsdata *dat1, t_lsdata *dat2) +{ + if (dat2->stat.st_size - dat1->stat.st_size) + return (dat2->stat.st_size - dat1->stat.st_size); + else + return (ft_strcmp(dat1->path, dat2->path)); +} diff --git a/ls/src/lib_parse.c b/ls/src/lib_parse.c new file mode 100644 index 00000000..6d54a36f --- /dev/null +++ b/ls/src/lib_parse.c @@ -0,0 +1,134 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 15:02:46 by jhalford #+# #+# */ +/* Updated: 2016/11/27 12:49:32 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +t_opts g_opts[] = +{ + {'A', OPTS_UA, 0}, + {'F', OPTS_UF, OPTS_LP}, + {'G', OPTS_UG, 0}, + {'R', OPTS_UR, 0}, + {'S', OPTS_US, 0}, + {'T', OPTS_UT, 0}, + {'U', OPTS_UU, TIME_MASK}, + {'a', OPTS_LA, 0}, + {'c', OPTS_LC, TIME_MASK}, + {'d', OPTS_LD, 0}, + {'f', OPTS_LF, 0}, + {'g', OPTS_LG, 0}, + {'l', OPTS_LL, 0}, + {'o', OPTS_LO, 0}, + {'p', OPTS_LP, OPTS_UF}, + {'r', OPTS_LR, 0}, + {'t', OPTS_LT, 0}, + {'u', OPTS_LU, TIME_MASK}, + {'1', OPTS_ONE, (OPTS_LL | OPTS_LG | OPTS_LO)}, + {0, 0, 0}, +}; + +static void ft_ls_parse_files(char **av, int opts, t_list **dir, t_list **ent) +{ + t_lsdata data; + int i; + + data.dirent = NULL; + i = -1; + while (av[++i]) + { + if (stat(av[i], &data.stat) < 0) + if (lstat(av[i], &data.stat) < 0) + { + ft_error_dir(av[i]); + continue ; + } + if (opts & OPTS_LL || !S_ISDIR(data.stat.st_mode)) + if (lstat(av[i], &data.stat) < 0) + { + ft_error_dir(av[i]); + continue ; + } + data.path = ft_strdup(av[i]); + if (opts & OPTS_LD || !S_ISDIR(data.stat.st_mode)) + ft_lstadd(ent, ft_lstnew(&data, sizeof(data))); + else + ft_lstadd(dir, ft_lstnew(&data, sizeof(data))); + } +} + +static int ft_getopts(char *str) +{ + int i; + int j; + int opts; + + i = 0; + opts = 0; + while (str[i]) + { + j = -1; + while (g_opts[++j].id) + if (g_opts[j].id == str[i]) + { + opts &= ~g_opts[j].off; + opts |= g_opts[j].on; + break ; + } + if (!ft_strchr(LS_LEGAL_OPTS, str[i])) + { + ft_error_option(str[i]); + exit(1); + } + i++; + } + return (opts); +} + +static int ft_ls_parse_options(int ac, char **av, int *opts) +{ + int i; + + i = 0; + while (++i < ac) + { + if (av[i][0] == '-') + { + if (!av[i][1]) + break ; + if (av[i][1] == '-' && !av[i][2]) + { + i++; + break ; + } + *opts |= ft_getopts(av[i] + 1); + } + else + break ; + } + return (i); +} + +int ft_ls_parse(int ac, char **av, t_list **dir, t_list **ent) +{ + int i; + int opts; + + opts = 0; + i = ft_ls_parse_options(ac, av, &opts); + opts |= (ac - i > 1) ? OPTS_HEAD : 0; + ft_sstrsort(av + i, &ft_strcmp); + if (i == ac) + ft_ls_parse_files((char *[2]){"."}, opts, dir, ent); + else + ft_ls_parse_files(av + i, opts, dir, ent); + return (opts); +} diff --git a/ls/src/lib_time.c b/ls/src/lib_time.c new file mode 100644 index 00000000..2b5ed97f --- /dev/null +++ b/ls/src/lib_time.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_time.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/23 16:49:05 by jhalford #+# #+# */ +/* Updated: 2016/11/23 18:46:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +int ft_cmp_mtime(t_lsdata *dat1, t_lsdata *dat2) +{ + if (dat2->stat.st_mtime - dat1->stat.st_mtime) + return (dat2->stat.st_mtime - dat1->stat.st_mtime); + else + return (ft_strcmp(dat1->path, dat2->path)); +} + +int ft_cmp_ctime(t_lsdata *dat1, t_lsdata *dat2) +{ + if (dat2->stat.st_ctime - dat1->stat.st_ctime) + return (dat2->stat.st_ctime - dat1->stat.st_ctime); + else + return (ft_strcmp(dat1->path, dat2->path)); +} + +int ft_cmp_atime(t_lsdata *dat1, t_lsdata *dat2) +{ + if (dat2->stat.st_atime - dat1->stat.st_atime) + return (dat2->stat.st_atime - dat1->stat.st_atime); + else + return (ft_strcmp(dat1->path, dat2->path)); +} + +int ft_cmp_btime(t_lsdata *dat1, t_lsdata *dat2) +{ + if (dat2->stat.st_birthtime - dat1->stat.st_birthtime) + return (dat2->stat.st_birthtime - dat1->stat.st_birthtime); + else + return (ft_strcmp(dat1->path, dat2->path)); +} diff --git a/ls/src/main.c b/ls/src/main.c new file mode 100644 index 00000000..c3508820 --- /dev/null +++ b/ls/src/main.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 15:00:15 by jhalford #+# #+# */ +/* Updated: 2016/11/27 12:06:22 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ls.h" + +int main(int ac, char **av) +{ + t_list *dir; + t_list *ent; + int opts; + + dir = NULL; + ent = NULL; + opts = ft_ls_parse(ac, av, &dir, &ent); + ft_ls_files(&ent, &dir, NULL, &opts); + ft_lstdel(&ent, &ft_ent_free); + ft_ls_dirs(dir, opts); + return (errno); +}