t_atom *ft_atom_new(void const *content, size_t size) { t_atom *new_atom; new_atom = (t_atom *)ft_memalloc(sizeof(*new_atom)); if (new_atom) { if (content == NULL) { new_atom->content = NULL; new_atom->content_size = 0; } else { if ((new_atom->content = ft_memdup(content, size)) == NULL) { new_atom->content_size = 0; return (NULL); } new_atom->content_size = size; } new_atom->next = NULL; new_atom->prev = NULL; } return (new_atom); }
t_list *ft_lstnew(void const *content, size_t content_size) { t_list *newlst; newlst = (t_list*)malloc(sizeof(t_list)); if (content_size) newlst->content = ft_memdup(content, content_size); newlst->content_size = 0; newlst->next = 0; return (newlst); }
t_env init_shell_env(char **environ) { t_env neoenv; t_env_key *first_key; char *str; ft_sort_string_array(environ, -1); first_key = parse_env_key(environ); neoenv = ft_memdup(&first_key, sizeof(t_env_key *)); add_env_key(neoenv, "OLDPWD", getcwd(NULL, 0)); add_env_key(neoenv, "PWD", getcwd(NULL, 0)); if ((str = fetch_key_val(neoenv, "SHLVL"))) add_env_key(neoenv, "SHLVL", ft_itoa(ft_atoi(str) + 1)); return (neoenv); }
t_data *ft_datanew(void *data, size_t data_size) { t_data *add; if (!data || !data_size) return (NULL); if (!(add = (t_data*)malloc(sizeof(t_data*)))) return (NULL); if (!(add->data = ft_memdup(data, data_size))) { free(add); return (NULL); } add->data_size = data_size; return (add); }
void msh_saveredir_here(t_mshc *msh, t_red *red, const t_tkn *t[2], const t_cmd *cmd) { red->error |= msh_get_redir_nbr(t[0]->ptr, &red->lhsfd, 0); if (t[1]->type != MTK_FILE) red->error |= MSH_RMISSING; else { red->rhsfd = -2; red->file = ft_memdup((void*)t[1]->ptr, t[1]->len + 1); if (red->file == NULL) msh_errmem(msh); msh_expand_redir_tilde(msh, red, cmd); red->file[t[1]->len] = '\0'; } return ; }
t_btree *ft_btreenew(const void *content, size_t content_size) { t_btree *tmp; if ((tmp = (t_btree*)malloc(sizeof(t_btree)))) { if (!(tmp->content = ft_memdup(content, content_size))) { free(tmp); return (NULL); } tmp->content_size = content_size; tmp->color = RB_RED; tmp->left = NULL; tmp->right = NULL; tmp->parent = NULL; } return (tmp); }
t_arr *ft_lst2arr(const t_lst *lst) { t_arr *arr; t_lst const *l; size_t i; arr = ft_arrnew(ft_lstlen(lst)); if (arr) { i = 0; l = lst; while (l) { arr[i].content = ft_memdup(l->content, l->content_size); arr[i].size = l->content_size; l = l->next; } } return (arr); }
void msh_saveredir_apnd(t_mshc *msh, t_red *red, const t_tkn *t[2], const t_cmd *cmd) { red->error |= msh_get_redir_nbr(t[0]->ptr, &red->lhsfd, 1); if (t[1]->type != MTK_FILE || t[1]->len == 0) red->error |= MSH_RMISSING; else { red->rhsfd = -2; red->file = ft_memdup((void*)t[1]->ptr, t[1]->len + 1); if (red->file == NULL) msh_errmem(msh); red->file[t[1]->len] = '\0'; msh_expand_redir_tilde(msh, red, cmd); /* if (red->file_err || (red->file_err = ft_access(red->file, W_OK)) != 0) */ if (red->file_err || (red->file_err = msh_get_redirout_perm(msh, red))) red->error |= MSH_RINVALID; } return ; }
static void build_string(t_mshc *msh, const t_cmd *cmd, char **str, const t_tkn *tkn) { char *expansion; int ret; if ((*str = (char*)ft_memdup(tkn->ptr, 1 + tkn->len)) == NULL) msh_errmem(msh); (*str)[tkn->len] = '\0'; ret = ft_expand_tilde_envl(*str, (const char**)msh->env, *cmd->alvar, &expansion); if (ret == ENOMEM) msh_errmem(msh); else if (ret >= 0) { free(*str); *str = expansion; } return ; }
void msh_saveredir_read(t_mshc *msh, t_red *red, const t_tkn *t[2], const t_cmd *cmd) { red->error |= msh_get_redir_nbr(t[0]->ptr, &red->lhsfd, 0); if (t[1]->type != MTK_FILE) red->error |= MSH_RMISSING; else if (*t[1]->ptr == '&') red->error |= msh_get_redir_nbr(t[1]->ptr + 1, &red->rhsfd, -1) << 3; else { red->rhsfd = -2; red->file = ft_memdup((void*)t[1]->ptr, t[1]->len + 1); if (red->file == NULL) msh_errmem(msh); red->file[t[1]->len] = '\0'; msh_expand_redir_tilde(msh, red, cmd); if (red->file_err || (red->file_err = ft_access(red->file, R_OK)) != 0) red->error |= MSH_RINVALID; } return ; }
t_list *ft_lstnew(void const *content, size_t content_size) { t_list *ret; if ((ret = (t_list *)malloc(sizeof(*ret))) == NULL) return (NULL); if (content == NULL) { ret->content = NULL; ret->content_size = 0; } else if ((ret->content = ft_memdup(content, content_size)) == NULL) { free(ret); return (NULL); } else ret->content_size = content_size; ret->next = NULL; return (ret); }
t_ldcd_cell ft_ldcd_cellnew(void *elem, size_t elem_size) { t_ldcd_cell newcell; newcell = (t_ldcd_cell)malloc(sizeof(struct s_ldcd_cell)); if (!newcell) return (NULL); if (!elem || elem_size == 0) { newcell->content = elem; newcell->content_size = 0; } else { newcell->content = ft_memdup(elem, elem_size); newcell->content_size = elem_size; newcell->next = NULL; newcell->prev = NULL; } return (newcell); }
static t_list *gnl_rest(char *rst) { static t_list *rest = NULL; t_list *tmp; if (rst != NULL) { if (ft_strlen(rst) != 0) rest = ft_lstnew(rst, ft_strlen(rst)); else rest = NULL; } else if (rest) { tmp = ft_memdup(rest, sizeof(t_list)); free(rest); rest = NULL; } else tmp = NULL; return (tmp); }