From 2c4eedb94faa1b08bbd49a6642aa17a38744de05 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Mar 2017 15:49:51 +0100 Subject: [PATCH 1/4] changed malloc, to ft_malloc. what have I done... --- 42sh/libft/Makefile | 1 + 42sh/libft/includes/libft.h | 4 +++- 42sh/libft/src/btree/btree_create_node.c | 4 ++-- 42sh/libft/src/btree/btree_map.c | 4 ++-- 42sh/libft/src/dlst/ft_dlstnew.c | 6 +++--- 42sh/libft/src/ft_printf/ft_conversion.c | 4 ++-- 42sh/libft/src/ft_printf/ft_printf.c | 2 +- 42sh/libft/src/ft_printf/ft_vprintf.c | 6 +++--- 42sh/libft/src/ft_printf/lib_fmt.c | 4 ++-- 42sh/libft/src/get_next_line/get_next_line.c | 2 +- 42sh/libft/src/lst/ft_lstmap.c | 4 ++-- 42sh/libft/src/lst/ft_lstnew.c | 4 ++-- 42sh/libft/src/mem/ft_memalloc.c | 4 ++-- 42sh/libft/src/mem/ft_realloc.c | 2 +- 42sh/libft/src/sstr/ft_sstradd.c | 2 +- 42sh/libft/src/sstr/ft_sstrdup.c | 4 ++-- 42sh/libft/src/str/ft_split_whitespaces.c | 6 +++--- 42sh/libft/src/str/ft_strdup.c | 4 ++-- 42sh/libft/src/str/ft_strdupchr.c | 2 +- 42sh/libft/src/str/ft_strduptr.c | 2 +- 42sh/libft/src/str/ft_strmap.c | 4 ++-- 42sh/libft/src/str/ft_strmapi.c | 4 ++-- 42sh/libft/src/str/ft_strnew.c | 4 ++-- 42sh/libft/src/str/ft_strsplit.c | 4 ++-- 42sh/libft/src/str/ft_strsub.c | 4 ++-- 42sh/libft/src/time/ft_mytime_get.c | 4 ++-- 42sh/libft/src/time/ft_time_isrecent.c | 2 +- 42sh/src/builtin/builtin_cd.c | 2 +- 42sh/src/builtin/builtin_exit.c | 5 ++--- 42sh/src/parser/tree_wrapper.c | 2 +- 30 files changed, 54 insertions(+), 52 deletions(-) diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 66124ab9..726fddbc 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -121,6 +121,7 @@ math/ft_uitoa_base.c\ math/ft_ulltoa_base.c\ math/id.c\ mem/ft_bzero.c\ +mem/ft_malloc.c\ mem/ft_memalloc.c\ mem/ft_memccpy.c\ mem/ft_memchr.c\ diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index e34e6c54..55cd9537 100644 --- a/42sh/libft/includes/libft.h +++ b/42sh/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/20 09:37:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:44:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define LIBFT_H # include +# include # include # include # include @@ -64,6 +65,7 @@ int ft_vdprintf(int fd, const char *format, va_list ap); int ft_asprintf(char **ret, const char *format, ...); int ft_vasprintf(char **ret, const char *format, va_list ap); +void *ft_malloc(size_t size); 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); diff --git a/42sh/libft/src/btree/btree_create_node.c b/42sh/libft/src/btree/btree_create_node.c index 53a90bf7..cc365537 100644 --- a/42sh/libft/src/btree/btree_create_node.c +++ b/42sh/libft/src/btree/btree_create_node.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ -/* Updated: 2017/03/08 00:35:44 by ariard ### ########.fr */ +/* Updated: 2017/03/21 15:43:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ t_btree *btree_create_node(void const *item, size_t content_size) { t_btree *new; - if (!(new = (t_btree *)malloc(sizeof(t_btree)))) + if (!(new = (t_btree *)ft_malloc(sizeof(t_btree)))) return (NULL); new->left = 0; new->right = 0; diff --git a/42sh/libft/src/btree/btree_map.c b/42sh/libft/src/btree/btree_map.c index 42fd89a5..85439a1a 100644 --- a/42sh/libft/src/btree/btree_map.c +++ b/42sh/libft/src/btree/btree_map.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 11:50:46 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:09:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ t_btree *btree_map(t_btree *root, void *(*f)(void *)) if (!root) return (NULL); - if (!(new = (t_btree *)malloc(sizeof(*new)))) + if (!(new = (t_btree *)ft_malloc(sizeof(*new)))) return (NULL); new->item = (*f)(root->item); new->left = btree_map(root->left, f); diff --git a/42sh/libft/src/dlst/ft_dlstnew.c b/42sh/libft/src/dlst/ft_dlstnew.c index f97ab6fb..411bffca 100644 --- a/42sh/libft/src/dlst/ft_dlstnew.c +++ b/42sh/libft/src/dlst/ft_dlstnew.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:27:20 by jhalford #+# #+# */ -/* Updated: 2016/12/07 17:35:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:43:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ t_dlist *ft_dlstnew(void const *content, size_t content_size) if (!content) { - new = malloc(sizeof(*new)); + new = ft_malloc(sizeof(*new)); if (!new) return (NULL); new->content_size = 0; @@ -26,7 +26,7 @@ t_dlist *ft_dlstnew(void const *content, size_t content_size) } else { - new = (t_dlist *)malloc(sizeof(*new)); + new = (t_dlist *)ft_malloc(sizeof(*new)); if (!new) return (NULL); new->content_size = content_size; diff --git a/42sh/libft/src/ft_printf/ft_conversion.c b/42sh/libft/src/ft_printf/ft_conversion.c index 6303d971..99f0cc08 100644 --- a/42sh/libft/src/ft_printf/ft_conversion.c +++ b/42sh/libft/src/ft_printf/ft_conversion.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:31:48 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:50:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,7 @@ char *ft_char_conversion(t_fmt *fmt, va_list ap) char *ret; (void)fmt; - ret = (char *)malloc(sizeof(char) + 1); + ret = (char *)ft_malloc(sizeof(char) + 1); ret[0] = (char)va_arg(ap, int); ret[1] = '\0'; return (ret); diff --git a/42sh/libft/src/ft_printf/ft_printf.c b/42sh/libft/src/ft_printf/ft_printf.c index 2ec92b04..d81e505b 100644 --- a/42sh/libft/src/ft_printf/ft_printf.c +++ b/42sh/libft/src/ft_printf/ft_printf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:20:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:28:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/ft_printf/ft_vprintf.c b/42sh/libft/src/ft_printf/ft_vprintf.c index 803d4f97..569efdf1 100644 --- a/42sh/libft/src/ft_printf/ft_vprintf.c +++ b/42sh/libft/src/ft_printf/ft_vprintf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 15:52:57 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:19:56 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:29:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,8 +56,8 @@ int ft_vasprintf(char **ret, const char *format, va_list ap) if (ft_fmtcalc(&final, &str, ap)) return (-1); } - else - final = ft_strjoin(final, (char[]){*str++, 0}); + else if (!(final = ft_strjoin(final, (char[]){*str++, 0}))) + return (-1); ft_strdel(&tmp); } *ret = final; diff --git a/42sh/libft/src/ft_printf/lib_fmt.c b/42sh/libft/src/ft_printf/lib_fmt.c index 59caf57a..28b7313d 100644 --- a/42sh/libft/src/ft_printf/lib_fmt.c +++ b/42sh/libft/src/ft_printf/lib_fmt.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:35 by jhalford #+# #+# */ -/* Updated: 2017/03/10 16:54:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ t_fmt *ft_fmt_init(void) { t_fmt *fmt; - fmt = (t_fmt *)malloc(sizeof(t_fmt) + 1); + fmt = (t_fmt *)ft_malloc(sizeof(t_fmt) + 1); ft_bzero(fmt->flags, 6); ft_bzero(fmt->modifier, 3); fmt->conversion = '\0'; diff --git a/42sh/libft/src/get_next_line/get_next_line.c b/42sh/libft/src/get_next_line/get_next_line.c index f6462feb..3eed0dee 100644 --- a/42sh/libft/src/get_next_line/get_next_line.c +++ b/42sh/libft/src/get_next_line/get_next_line.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/15 13:12:06 by jhalford #+# #+# */ -/* Updated: 2017/03/21 13:16:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/lst/ft_lstmap.c b/42sh/libft/src/lst/ft_lstmap.c index c233eff0..e0edbbaa 100644 --- a/42sh/libft/src/lst/ft_lstmap.c +++ b/42sh/libft/src/lst/ft_lstmap.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:57:21 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:06:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *)) if (!lst) return (NULL); - if (!(elem = (t_list *)malloc(sizeof(*elem)))) + if (!(elem = (t_list *)ft_malloc(sizeof(*elem)))) return (NULL); elem->content = (*f)(lst->content); elem->next = ft_lstmap(lst->next, f); diff --git a/42sh/libft/src/lst/ft_lstnew.c b/42sh/libft/src/lst/ft_lstnew.c index 17a43d6f..e72d9ab7 100644 --- a/42sh/libft/src/lst/ft_lstnew.c +++ b/42sh/libft/src/lst/ft_lstnew.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 15:57:38 by jhalford #+# #+# */ -/* Updated: 2017/03/20 12:23:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ t_list *ft_lstnew(void const *content, size_t content_size) { t_list *new; - if (!(new = (t_list *)malloc(sizeof(*new)))) + if (!(new = (t_list *)ft_malloc(sizeof(*new)))) return (NULL); new->next = NULL; new->content_size = content_size; diff --git a/42sh/libft/src/mem/ft_memalloc.c b/42sh/libft/src/mem/ft_memalloc.c index e923d08b..6a4a1112 100644 --- a/42sh/libft/src/mem/ft_memalloc.c +++ b/42sh/libft/src/mem/ft_memalloc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:57:25 by jhalford #+# #+# */ -/* Updated: 2017/03/06 15:41:01 by ariard ### ########.fr */ +/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void *ft_memalloc(size_t size) void *addr; size_t i; - addr = malloc(size); + addr = ft_malloc(size); if (addr == NULL) return (NULL); i = -1; diff --git a/42sh/libft/src/mem/ft_realloc.c b/42sh/libft/src/mem/ft_realloc.c index 5297a2b6..33f667a8 100644 --- a/42sh/libft/src/mem/ft_realloc.c +++ b/42sh/libft/src/mem/ft_realloc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:37:53 by jhalford #+# #+# */ -/* Updated: 2016/12/09 18:31:03 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/sstr/ft_sstradd.c b/42sh/libft/src/sstr/ft_sstradd.c index 58aa0e32..c81a256d 100644 --- a/42sh/libft/src/sstr/ft_sstradd.c +++ b/42sh/libft/src/sstr/ft_sstradd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 18:03:58 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:28:29 by ariard ### ########.fr */ +/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/sstr/ft_sstrdup.c b/42sh/libft/src/sstr/ft_sstrdup.c index e68e0d35..8517a125 100644 --- a/42sh/libft/src/sstr/ft_sstrdup.c +++ b/42sh/libft/src/sstr/ft_sstrdup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 14:25:45 by jhalford #+# #+# */ -/* Updated: 2017/03/07 19:58:12 by ariard ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ char **ft_sstrdup(char **list) size = 0; while (list[size]) size++; - cpy = (char **)malloc(sizeof(char *) * (size + 1)); + cpy = (char **)ft_malloc(sizeof(char *) * (size + 1)); while (*list) { cpy[i++] = ft_strdup(*list); diff --git a/42sh/libft/src/str/ft_split_whitespaces.c b/42sh/libft/src/str/ft_split_whitespaces.c index 4124878a..ed744d0d 100644 --- a/42sh/libft/src/str/ft_split_whitespaces.c +++ b/42sh/libft/src/str/ft_split_whitespaces.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 16:07:17 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:55:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ char *ft_strdupi_w(char const *s) i = 0; while (s[i] && s[i] != ' ' && s[i] != '\t') i++; - str = (char *)malloc(sizeof(char) * (i + 1)); + str = (char *)ft_malloc(sizeof(char) * (i + 1)); if (str) { str[i--] = '\0'; @@ -59,7 +59,7 @@ char **ft_split_whitespaces(char const *s) str = NULL; if (!(s)) return (str); - str = (char **)malloc(sizeof(char *) * (ft_len_words(s) + 1)); + str = (char **)ft_malloc(sizeof(char *) * (ft_len_words(s) + 1)); i = 0; j = 0; if (!(str)) diff --git a/42sh/libft/src/str/ft_strdup.c b/42sh/libft/src/str/ft_strdup.c index bb3ade4b..556558f4 100644 --- a/42sh/libft/src/str/ft_strdup.c +++ b/42sh/libft/src/str/ft_strdup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 14:25:41 by jhalford #+# #+# */ -/* Updated: 2017/03/20 08:27:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ char *ft_strdup(const char *s1) { char *dup; - if (!s1 || !(dup = (char*)malloc(sizeof(*dup) * (ft_strlen(s1) + 1)))) + if (!s1 || !(dup = (char*)ft_malloc(sizeof(*dup) * (ft_strlen(s1) + 1)))) return (NULL); ft_strcpy(dup, s1); return (dup); diff --git a/42sh/libft/src/str/ft_strdupchr.c b/42sh/libft/src/str/ft_strdupchr.c index 97340f13..aee98aaf 100644 --- a/42sh/libft/src/str/ft_strdupchr.c +++ b/42sh/libft/src/str/ft_strdupchr.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 11:01:15 by ariard #+# #+# */ -/* Updated: 2017/03/07 11:24:27 by ariard ### ########.fr */ +/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/str/ft_strduptr.c b/42sh/libft/src/str/ft_strduptr.c index ca2f1180..59cfd835 100644 --- a/42sh/libft/src/str/ft_strduptr.c +++ b/42sh/libft/src/str/ft_strduptr.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/06 13:37:12 by ariard #+# #+# */ -/* Updated: 2017/03/07 12:41:20 by ariard ### ########.fr */ +/* Updated: 2017/03/21 15:43:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/str/ft_strmap.c b/42sh/libft/src/str/ft_strmap.c index 295214f0..5c0353ae 100644 --- a/42sh/libft/src/str/ft_strmap.c +++ b/42sh/libft/src/str/ft_strmap.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:58:24 by jhalford #+# #+# */ -/* Updated: 2016/11/03 14:58:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ char *ft_strmap(char const *s, char (*f)(char)) char *out; size = ft_strlen(s); - out = (char *)malloc(sizeof(char) * (size + 1)); + out = (char *)ft_malloc(sizeof(char) * (size + 1)); if (out == NULL) return (NULL); i = -1; diff --git a/42sh/libft/src/str/ft_strmapi.c b/42sh/libft/src/str/ft_strmapi.c index 0d6fab4c..7cc6d089 100644 --- a/42sh/libft/src/str/ft_strmapi.c +++ b/42sh/libft/src/str/ft_strmapi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:58:28 by jhalford #+# #+# */ -/* Updated: 2016/11/03 14:58:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) char *out; size = ft_strlen(s); - out = (char *)malloc(sizeof(char) * (size + 1)); + out = (char *)ft_malloc(sizeof(char) * (size + 1)); if (out == NULL) return (NULL); i = -1; diff --git a/42sh/libft/src/str/ft_strnew.c b/42sh/libft/src/str/ft_strnew.c index 56742a50..2e2ae1d6 100644 --- a/42sh/libft/src/str/ft_strnew.c +++ b/42sh/libft/src/str/ft_strnew.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:58:34 by jhalford #+# #+# */ -/* Updated: 2017/01/11 16:37:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ char *ft_strnew(size_t size) char *addr; size_t i; - addr = (char *)malloc(size + 1); + addr = (char *)ft_malloc(size + 1); if (addr == NULL) return (NULL); i = -1; diff --git a/42sh/libft/src/str/ft_strsplit.c b/42sh/libft/src/str/ft_strsplit.c index 10323439..049aa7ed 100644 --- a/42sh/libft/src/str/ft_strsplit.c +++ b/42sh/libft/src/str/ft_strsplit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:58:40 by jhalford #+# #+# */ -/* Updated: 2017/03/20 16:10:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ char **ft_strsplit(char const *s, char c) int k; char **str2; - if (!s || !(str2 = (char **)malloc(sizeof(*str2) * + if (!s || !(str2 = (char **)ft_malloc(sizeof(*str2) * (ft_countwords(s, c) + 1)))) return (NULL); i = -1; diff --git a/42sh/libft/src/str/ft_strsub.c b/42sh/libft/src/str/ft_strsub.c index 3f6c31d0..0c3fb11b 100644 --- a/42sh/libft/src/str/ft_strsub.c +++ b/42sh/libft/src/str/ft_strsub.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:58:43 by jhalford #+# #+# */ -/* Updated: 2017/03/21 11:38:40 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ 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)))) + if (!(out = (char *)ft_malloc(sizeof(char) * (len + 1)))) return (NULL); i = -1; while (++i < len) diff --git a/42sh/libft/src/time/ft_mytime_get.c b/42sh/libft/src/time/ft_mytime_get.c index f222530e..ab8f25b3 100644 --- a/42sh/libft/src/time/ft_mytime_get.c +++ b/42sh/libft/src/time/ft_mytime_get.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/25 11:34:56 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:54:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ t_mytime *ft_mytime_get(time_t epoch) char *date; t_mytime *time; - time = (t_mytime*)malloc(sizeof(*time)); + time = (t_mytime*)ft_malloc(sizeof(*time)); date = ctime(&epoch); date[ft_strlen(date) - 1] = 0; time->year = ft_isdigit(date[20]) ? diff --git a/42sh/libft/src/time/ft_time_isrecent.c b/42sh/libft/src/time/ft_time_isrecent.c index 04712152..ec195653 100644 --- a/42sh/libft/src/time/ft_time_isrecent.c +++ b/42sh/libft/src/time/ft_time_isrecent.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 18:01:04 by jhalford #+# #+# */ -/* Updated: 2016/11/25 11:43:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:44:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index f722ac76..1c1f8851 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:23:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:28:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index f238a25a..f8ab3a61 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:48:13 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:49:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,7 @@ int builtin_exit(const char *path, char *const av[], char *const envp[]) { notified = 1; if (has_stopped_job() || has_running_job()) - SH_ERR("There are running and/or stopped jobs"); - return (0); + return (SH_ERR("There are running and/or stopped jobs")); } if (av && av[1] && !ft_stris(av[1], ft_isdigit)) SH_ERR(EXITERR_0); diff --git a/42sh/src/parser/tree_wrapper.c b/42sh/src/parser/tree_wrapper.c index 51002717..93622211 100644 --- a/42sh/src/parser/tree_wrapper.c +++ b/42sh/src/parser/tree_wrapper.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:57:44 by ariard #+# #+# */ -/* Updated: 2017/03/20 10:35:11 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:46:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 151b4ec02d75d32cc73487aaffd2b78212dfd0d1 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Mar 2017 16:08:26 +0100 Subject: [PATCH 2/4] added ft_malloc --- 42sh/libft/src/ft_printf/ft_vprintf.c | 2 +- 42sh/libft/src/mem/ft_malloc.c | 26 ++++++++++++++++++++++++++ 42sh/src/builtin/bt_read_get.c | 2 +- 42sh/src/builtin/builtin_exit.c | 2 +- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 42sh/libft/src/mem/ft_malloc.c diff --git a/42sh/libft/src/ft_printf/ft_vprintf.c b/42sh/libft/src/ft_printf/ft_vprintf.c index 569efdf1..78ffa130 100644 --- a/42sh/libft/src/ft_printf/ft_vprintf.c +++ b/42sh/libft/src/ft_printf/ft_vprintf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 15:52:57 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:29:36 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:08:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/mem/ft_malloc.c b/42sh/libft/src/mem/ft_malloc.c new file mode 100644 index 00000000..3caec1b4 --- /dev/null +++ b/42sh/libft/src/mem/ft_malloc.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_malloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/21 15:45:00 by jhalford #+# #+# */ +/* Updated: 2017/03/21 15:45:20 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_malloc(size_t size) +{ + void *ptr; + + ptr = malloc(size); + if (!ptr) + { + ft_dprintf(2, "{red}%s{eoc}", strerror(errno)); + exit(errno); + } + return (ptr); +} diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c index 77360eaa..60463fd3 100644 --- a/42sh/src/builtin/bt_read_get.c +++ b/42sh/src/builtin/bt_read_get.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */ -/* Updated: 2017/03/20 08:41:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:52:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index f8ab3a61..fae8ee28 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:49:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:51:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 6313e7ff4caeaa9f3aca97168f894c8316f8e24a Mon Sep 17 00:00:00 2001 From: M600 Date: Tue, 21 Mar 2017 16:14:01 +0100 Subject: [PATCH 3/4] Replaced malloc to ft_malloc for completion --- 42sh/src/completion/c_find_env.c | 2 +- 42sh/src/completion/c_init.c | 2 +- 42sh/src/completion/c_parser.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/42sh/src/completion/c_find_env.c b/42sh/src/completion/c_find_env.c index 02c1d296..3d36ea24 100644 --- a/42sh/src/completion/c_find_env.c +++ b/42sh/src/completion/c_find_env.c @@ -20,7 +20,7 @@ static int c_addnode(t_comp *c, char *value) { t_clst *tmp; - if (!(tmp = (t_clst *)malloc(sizeof(t_clst)))) + if (!(tmp = (t_clst *)ft_malloc(sizeof(t_clst)))) return (-1); tmp->name = value; tmp->len = ft_strlen(tmp->name); diff --git a/42sh/src/completion/c_init.c b/42sh/src/completion/c_init.c index 93962e51..3cfd34ca 100644 --- a/42sh/src/completion/c_init.c +++ b/42sh/src/completion/c_init.c @@ -82,7 +82,7 @@ void c_init(t_data *s, long int input) { int len_trail; - if (!(s->comp = (t_comp *)malloc((sizeof(t_comp))))) + if (!(s->comp = (t_comp *)ft_malloc((sizeof(t_comp))))) return ; c_init_base(s->comp); s->comp->rcmd = c_trimmer(s->line.input, s->line.pos, s->line.pos); diff --git a/42sh/src/completion/c_parser.c b/42sh/src/completion/c_parser.c index c59e1ad4..49721404 100644 --- a/42sh/src/completion/c_parser.c +++ b/42sh/src/completion/c_parser.c @@ -52,7 +52,7 @@ static int c_storing(t_comp *c, char *value, unsigned char type) return (0); if (c->match && ft_strnequ(c->match, value, ft_strlen(c->match)) != 1) return (0); - if (!(tmp = (t_clst *)malloc(sizeof(t_clst)))) + if (!(tmp = (t_clst *)ft_malloc(sizeof(t_clst)))) return (-1); tmp->name = ft_strdup(value); tmp->len = ft_strlen(tmp->name); From fb806bfa906b6e0f4c79111892700060e503ecee Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Mar 2017 16:41:00 +0100 Subject: [PATCH 4/4] issue #128 part 1 --- 42sh/libft/Makefile | 1 + 42sh/libft/includes/libft.h | 2 +- 42sh/libft/includes/sys.h | 3 ++- 42sh/libft/src/ft_printf/ft_vprintf.c | 2 +- 42sh/libft/src/mem/ft_malloc.c | 2 +- 42sh/libft/src/sys/fd_replace.c | 20 ++++++++++++++++++++ 42sh/src/builtin/is_builtin.c | 2 +- 42sh/src/exec/bad_fd.c | 5 ++--- 42sh/src/exec/process_redirect.c | 12 +++--------- 42sh/src/exec/redirect_greatand.c | 9 +++------ 42sh/src/exec/redirect_lessand.c | 9 +++------ 11 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 42sh/libft/src/sys/fd_replace.c diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 726fddbc..4ce95f9b 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -191,6 +191,7 @@ str/ft_strsub.c\ str/ft_strtok.c\ str/ft_strtrim.c\ sys/dup2_close.c\ +sys/fd_replace.c\ sys/ft_getenv.c\ sys/ft_xattr_count.c\ sys/ft_xattr_print.c\ diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index 55cd9537..98477d16 100644 --- a/42sh/libft/includes/libft.h +++ b/42sh/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:44:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:32:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/includes/sys.h b/42sh/libft/includes/sys.h index 9f63db23..972805ae 100644 --- a/42sh/libft/includes/sys.h +++ b/42sh/libft/includes/sys.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ -/* Updated: 2017/03/20 09:38:36 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:33:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,5 +29,6 @@ int ft_xattr_print(char *path); int ft_xattr_count(char *path); char *ft_getenv(char **env, char *key); int dup2_close(int fd1, int fd2); +int fd_replace(int fd1, int fd2); #endif diff --git a/42sh/libft/src/ft_printf/ft_vprintf.c b/42sh/libft/src/ft_printf/ft_vprintf.c index 78ffa130..718897e9 100644 --- a/42sh/libft/src/ft_printf/ft_vprintf.c +++ b/42sh/libft/src/ft_printf/ft_vprintf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 15:52:57 by jhalford #+# #+# */ -/* Updated: 2017/03/21 16:08:02 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:34:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/mem/ft_malloc.c b/42sh/libft/src/mem/ft_malloc.c index 3caec1b4..4560b1e8 100644 --- a/42sh/libft/src/mem/ft_malloc.c +++ b/42sh/libft/src/mem/ft_malloc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/21 15:45:00 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:45:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:34:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c new file mode 100644 index 00000000..905b340c --- /dev/null +++ b/42sh/libft/src/sys/fd_replace.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fd_replace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/21 16:32:26 by jhalford #+# #+# */ +/* Updated: 2017/03/21 16:40:17 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int fd_replace(int fd1, int fd2) +{ + if (fd1 != fd2) + return(dup2_close(fd1, fd2)); + return (0); +} diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 7c2a8769..90a08144 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:14:20 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 16:24:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/bad_fd.c b/42sh/src/exec/bad_fd.c index 8851abcd..4b86b0ae 100644 --- a/42sh/src/exec/bad_fd.c +++ b/42sh/src/exec/bad_fd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:32:43 by jhalford #+# #+# */ -/* Updated: 2017/03/20 10:24:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:24:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ int bad_fd(int fd) { - ft_dprintf(2, "{red}%s: %i: Bad file descriptor{eoc}\n", - SHELL_NAME, fd); + SH_ERR("%i: Bad file descriptor", fd); return (1); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index ce1025cc..80d54701 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:38:13 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 16:40:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,6 @@ t_itof g_redirmap[] = {0, NULL}, }; -static void process_close(int fd1, int fd2) -{ - if (fd1 != fd2) - dup2_close(fd1, fd2); -} - int process_redirect(t_process *p) { t_list *redirs; @@ -38,6 +32,8 @@ int process_redirect(t_process *p) redirs = p->redirs; if (p->to_close != STDIN) close(p->to_close); + fd_replace(p->fdin, STDIN); + fd_replace(p->fdout, STDOUT); while (redirs) { redir = redirs->content; @@ -53,7 +49,5 @@ int process_redirect(t_process *p) } redirs = redirs->next; } - process_close(p->fdin, STDIN); - process_close(p->fdout, STDOUT); return (0); } diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index ee798a6b..76724439 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ -/* Updated: 2017/03/20 12:36:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:35:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,18 +24,15 @@ int redirect_greatand(t_redir *redir) } if (!ft_stris(redir->word, ft_isdigit)) { - ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", - data_singleton()->argv[0], redir->word); + SH_ERR("%s: can only be digits{eoc}\n", redir->word); return (1); } fdold = ft_atoi(redir->word); fdnew = redir->n; - if (fdold == fdnew) - return (0); if (fdold > 9) return (bad_fd(fdold)); if (fd_is_valid(fdold, O_WRONLY | O_RDWR)) - dup2_close(fdold, fdnew); + fd_replace(fdold, fdnew); else return (bad_fd(fdold)); return (0); diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index a5c7efe4..0f4f5928 100644 --- a/42sh/src/exec/redirect_lessand.c +++ b/42sh/src/exec/redirect_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */ -/* Updated: 2017/03/20 12:36:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:33:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,18 +24,15 @@ int redirect_lessand(t_redir *redir) } if (!ft_stris(redir->word, ft_isdigit)) { - ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", - data_singleton()->argv[0], redir->word); + SH_ERR("%s: can only be digits{eoc}\n", redir->word); return (1); } fdold = ft_atoi(redir->word); fdnew = redir->n; - if (fdold == fdnew) - return (0); if (fdold > 9) return (bad_fd(fdold)); if (fd_is_valid(fdold, O_RDONLY | O_RDWR)) - dup2_close(fdold, fdnew); + fd_replace(fdold, fdnew); else return (bad_fd(fdold)); return (0);