Exemple #1
0
int			exec_cmd(t_sh *t)
{
	int		i;
	int		j;
	char	*str;

	i = -1;
	j = -1;
	str = ft_strnew(0);;
	t->tmp = NULL;
	t->fo = 0;
	if (!t->args[0])
		return (1);
	while (t->args[++j])
		if (!my_regex(t->args[j], "#-_A~Z0~9a~z\\=\\#-_A~Za~z./0~9*?", 0, '\0'))
			break ;
	if (t->args[j])
	{
		j = 0;
		str = ft_strdup(t->args[0]);
		ft_strdel(&t->args[0]);
		while (t->args[++j])
			str = ft_strjoin_free(str, ft_strjoin_free(" ", t->args[j], 2), 3);
		free(t->args);
		t->args = ft_strsplit(set_local_var(&str, 0, t), ' ');
		return (0);
	}
	if (ft_strchr(t->args[0], '='))
	{
		ft_putendl_fd("Bad variable format", 2);
		return (-1);
	}
	exec_cmd1(t, &i);
	return (exec_cmd2(t, &i));
}
Exemple #2
0
t_prts	*ft_type_p(va_list ap, t_pmts pmts)
{
	size_t				len;
	char				*str;
	t_prts				*node;
	unsigned long long	res;

	res = va_arg(ap, unsigned long long);
	str = ft_itoa_base_ull_ed(res, 16);
	len = ft_strlen(str);
	ft_set_p_flags(&pmts, str, ft_strlen(str));
	node = (t_prts*)malloc(sizeof(t_prts));
	node->next = NULL;
	ft_helper_type_p(&pmts, len, &str, &node);
	if (!ft_calc_flags_sum(pmts))
	{
		if (pmts.prec_value && str[0] == '0')
			str = ft_strjoin_free(str, "x", 1);
		else
			str = ft_strjoin_free("0x", str, 2);
		return (ft_rec_given_data(&node, str));
	}
	else if (pmts.zero_value)
		return (ft_handle_p_zv(pmts, &node, str));
	else
		return (ft_handle_p_v(pmts, &node, str));
}
Exemple #3
0
static char		*line_east(int x, int max, int y, t_zaap *z)
{
	int		i;
	char	flag;
	char	*tmp;
	char	*ret;

	i = 0;
	flag = 1;
	ret = ft_strnew(0);
	while (i < max)
	{
		tmp = see_spot(z->map[y][x], max);
		if (flag)
			flag = 0;
		ft_strjoin_free(&ret, tmp);
		free(tmp);
		i++;
		y = vd(y + 1, z->y);
		ft_strjoin_free(&ret, ", ");
	}
	if (flag)
		ft_strjoin_free(&ret, ", ");
	return (ret);
}
Exemple #4
0
void	ft_move(t_stack **stack, t_stack *stack_two, int dir_ind[], char **sols)
{
	int	i;

	i = 0;
	if (dir_ind[0] % 10 == 1)
		while ((*stack)->index != dir_ind[1])
		{
			ft_r(stack);
			*sols = ft_strjoin_free(*sols, " ra");
			if (dir_ind[0] > 10)
			{
				write(1, "\nAction : ra", 12);
				ft_print_stack(*stack, stack_two);
			}
		}
	else
		while ((*stack)->index != dir_ind[1])
		{
			ft_rr(stack);
			*sols = ft_strjoin_free(*sols, " rra");
			if (dir_ind[0] > 10)
			{
				write(1, "\nAction : rra", 13);
				ft_print_stack(*stack, stack_two);
			}
		}
}
Exemple #5
0
char	*ft_work_with_unprintable(char *str, size_t len)
{
	size_t	k;
	size_t	i;
	size_t	j;
	char	*res;

	i = 0;
	j = 0;
	k = 0;
	res = ft_strdup("");
	while (len)
	{
		if (!ft_isprint(str[k]))
		{
			res = ft_strjoin_free(res, ft_unprint_to_print(str, j, i), 3);
			j = k;
			i = 0;
		}
		len--;
		k++;
		i++;
	}
	if (j < ft_strlen(str))
		res = ft_strjoin_free(res, &str[j], 1);
	return (res);
}
Exemple #6
0
int			get_next_line(const int fd, char **line)
{
	char		*buff;
	int			tete;
	static char	*rest;

	buff = ft_strnew(BUFF_SIZE + 1);
	if (fd < 0 || line == NULL)
		return (-1);
	free(*line);
	*line = ft_strnew(1);
	if (rest == NULL)
		rest = ft_strnew(BUFF_SIZE);
	if (ft_strchr(rest, '\n') != NULL)
		return (ft_cop(rest, line, rest));
	*line = ft_strjoin_free(*line, rest);
	while ((tete = read(fd, buff, BUFF_SIZE)) && tete != -1)
	{
		buff[tete] = 0;
		if (ft_strchr(buff, '\n') != NULL)
			return (ft_cop(buff, line, rest));
		else
			*line = ft_strjoin_free(*line, buff);
	}
	free(buff);
	return (tete);
}
Exemple #7
0
void	ft_disp_swap_a(t_env *e)
{
	ft_swap_stack('a', e);
	if (*(e->op) != '\0')
		e->op = ft_strjoin_free(e->op, ft_strdup(" "));
	e->op = ft_strjoin_free(e->op, ft_strdup("sa"));
	if (e->flag_v == 1)
		ft_stacks_state(e);
}
Exemple #8
0
void	ft_do_padding_switch_4(t_conv *conv, char **ret,
		int offset, char *padding)
{
	if (ft_strchr(conv->flag, '#') != NULL
			&& ft_strchr("X", conv->conversion) != NULL
			&& ft_strcmp(*ret, "0") != 0)
		*ret = ft_strjoin_free(
				padding = ft_strjoin_free(ft_strdup("0X"), padding), *ret);
	else
		ft_do_padding_switch_5(conv, ret, offset, padding);
}
Exemple #9
0
static void	ft_do_precision_2(char **ret, int *i)
{
	if (*i > 0)
		*ret = ft_strjoin_free(ft_strdup("0"), *ret);
	else
	{
		(*i)--;
		if (*i == -1 && **ret == '-')
			**ret = '0';
		else
			*ret = ft_strjoin_free(ft_strdup("0"), *ret);
	}
}
Exemple #10
0
t_prts	*ft_handle_b_v(t_pmts pmts, t_prts **node, char *res)
{
	char	*tmp;
	size_t	len;

	len = ft_strlen(res);
	tmp = ft_malloc_sz(pmts.value - len);
	tmp = ft_memset(tmp, 32, pmts.value - len);
	res = pmts.minus ? ft_strjoin_free(res, tmp, 3)
			: ft_strjoin_free(tmp, res, 3);
	(*node)->str = res;
	(*node)->len = ft_strlen(res);
	return (*node);
}
Exemple #11
0
void	ft_padding_switch_core(t_padding *p, t_conv *conv, int *offset)
{
	if (**(p->ret) == '-' && *(p->i) == 0)
	{
		*(p->padding) = ft_strjoin_free(*(p->padding), ft_strdup("-"));
		*offset = 1;
		(*(p->len))++;
	}
	else if (conv->padding_pos != -1 && conv->precision_pos != -1
			&& ft_strlen(conv->precision) > 0
			&& ft_atoi(conv->padding) > ft_atoi(conv->precision))
		*(p->padding) = ft_strjoin_free(*(p->padding), ft_strdup(" "));
	else
		*(p->padding) = ft_strjoin_free(*(p->padding), ft_strdup("0"));
}
void	foret_if_hexa(t_env2 *env2, unsigned long res_arg, char *c)
{
	if ((size_t)env2->val_precision > ft_strlen(env2->str))
		ft_precision(env2, ft_strlen(env2->str));
	if (env2->precision == 1 && env2->val_precision == 0 && res_arg == 0)
		env2->str[0] = 0;
	if (env2->taille_min != 0 &&
			(size_t)env2->taille_min > ft_strlen(env2->str))
		ft_taille_min(env2);
	if (env2->dieze && res_arg != 0)
	{
		if (env2->test == 0)
			env2->str = ft_strjoin_free(env2->str, c, env2->str);
		else
		{
			if (!env2->moins)
				dieze_x_no_moins(env2, c);
			else
			{
				env2->str = ft_strjoin(c, env2->str);
				if (env2->test == 1)
					(env2->str[ft_strlen(env2->str) - 2] == ' ') ?
						(env2->str[ft_strlen(env2->str) - 2] = 0) :
						(env2->str[ft_strlen(env2->str) - 1] = 0);
			}
		}
	}
}
Exemple #13
0
static int		ft_save(t_save **s, char *buf, char **line)
{
	char	*eol;
	char	*tmp;

	if ((eol = ft_strchr(buf, '\n')) != NULL && eol++)
	{
		if ((*s)->rest && ft_strchr((*s)->rest, '\n') == NULL)
			*line = ft_strjoin_free((*s)->rest, ft_strcpy_limit(buf, '\n'));
		else
			*line = ft_strcpy_limit(buf, '\n');
		tmp = (*s)->rest;
		(*s)->rest = ft_strdup(eol);
		ft_strdel(&tmp);
		return (1);
	}
	else
	{
		tmp = (*s)->rest;
		if ((*s)->rest)
			(*s)->rest = ft_strjoin((*s)->rest, buf);
		else
			(*s)->rest = ft_strdup(buf);
		ft_strdel(&tmp);
	}
	return (0);
}
Exemple #14
0
int			end_cmd(t_sh *t, int *i)
{
	int		j;
	char	*str;

	j = 0;
	str = ft_strnew(0);
	if (t->fo)
	{
		while (t->args[j])
			str = ft_strjoin_free(str, ft_strjoin(" ", t->args[j++]), 3);
		second_static(NULL, NULL, str);
		job_cont(t->fo);
		waitpid(t->fo, &t->status, WUNTRACED);
		WIFEXITED(t->status);
		if (t->status)
			WEXITSTATUS(t->status);
	}
	t->status = no_cmd(t, i);
	if (t->status == 256 || !t->status)
	{
		job_cont(0);
		second_static(NULL, "$suppress", NULL);
	}
	tputs(tgoto(tgetstr("cr", NULL), 0, 0), 1, tputs_putchar);
	return (t->status);
}
Exemple #15
0
char		*ft_strjoin2(char *s1, char *s2, int type)
{
	char	*n;
	int		i[2];

	i[0] = 0;
	while (s1[i[0]])
		i[0]++;
	i[1] = 0;
	while (s2[i[1]])
		i[1]++;
	n = (char *)malloc(sizeof(char) * (i[0] + i[1] + 1));
	i[0] = 0;
	while (s1[i[0]])
	{
		n[i[0]] = s1[i[0]];
		i[0]++;
	}
	i[1] = 0;
	while (s2[i[1]])
	{
		n[i[0] + i[1]] = s2[i[1]];
		i[1]++;
	}
	ft_strjoin_free(s1, s2, type);
	n[i[0] + i[1]] = '\0';
	return (n);
}
Exemple #16
0
static int	store_read(t_file *file, char **line, char *buf)
{
	int		e;

	if (!find_next(file->buf))
	{
		while (!find_next(file->buf) &&
				(e = read(file->fd, buf, BUFF_SIZE)) >= 1)
		{
			file->buf = ft_strjoin_free(file->buf, buf);
			ft_bzero(buf, BUFF_SIZE);
		}
		if (e < 0)
			return (-1);
		if (e == 0 && !find_next(file->buf) && ft_strlen(file->buf))
		{
			*line = ft_strdup(file->buf);
			ft_strclr(file->buf);
			return (1);
		}
		if (!ft_strlen(file->buf) && e == 0)
			return (0);
	}
	*line = strchrsubdup(&file->buf);
	return (1);
}
Exemple #17
0
int			ft_core(t_stack *stack_one, t_stack *stack_two, char *options)
{
    int		tmp[2];
    char	*sols;

    if (stack_one)
    {
        if ((tmp[0] = check_possibilities(stack_one, options, &sols)) != -1)
            return (tmp[0]);
        while (stack_one->next->next)
        {
            tmp[1] = ft_get_min(stack_one);
            tmp[0] = ft_direction(stack_one, tmp[1]) + (options[1] ? 10 : 0);
            ft_move(&stack_one, stack_two, tmp, &sols);
            ft_p(&stack_two, &stack_one, options[1] ? 1 : 0);
            if (options[1])
                ft_print_stack(stack_one, stack_two);
            sols = ft_strjoin_free(sols, " pb");
        }
        if (stack_one->number > stack_one->next->number)
            last(&stack_one, stack_two, options, &sols);
        ft_repush(&stack_two, &stack_one, &sols, options[1]);
        return (ft_display(sols, options, stack_one));
    }
    return (0);
}
Exemple #18
0
void		exec_cmd1(t_sh *t, int *i)
{
	char	*str;

	str = NULL;
	my_alias(&(t->args[0]), 1);	
	while (t->path[++*i])
	{
		t->tmp = ft_strjoin(t->path[*i], "/");
		if (access(str = ft_strjoin_free(t->tmp,
								t->args[0], 1), R_OK) > -1)
		{
			if ((t->fo = fork()) < 0)
			{
				ft_strdel(&str);
				ft_putstr_fd("fork error yeah\n", 2);
				exit(0);
			}
			if (!t->fo)
			{
				execve(str, t->args, t->env);
				exit(0);
			}
			break ;
		}
		ft_strdel(&(str));
	}
}
Exemple #19
0
int				get_next_line(int const fd, char **line)
{
	static char	*tmp;
	t_gnl		gnl;

	if (get_err(fd, line) == -1)
		return (-1);
	if (tmp && ft_strchr(tmp, '\n'))
		return (get_eol(&tmp, line));
	while ((gnl.ret = read(fd, gnl.buf, BUFF_SIZE)) > 0)
	{
		gnl.buf[gnl.ret] = '\0';
		tmp = ft_strjoin_free(tmp, gnl.buf, 1);
		if (ft_strchr(gnl.buf, '\n'))
			break ;
	}
	if (gnl.ret == -1)
		return (-1);
	if (get_pos(tmp, '\n') == -1)
	{
		*line = ft_strdup(tmp);
		ft_strdel(&tmp);
		if (*line)
			return (1);
		return (0);
	}
	return (get_eol(&tmp, line));
}
Exemple #20
0
t_prts	*ft_type_o(va_list ap, t_pmts pmts)
{
	size_t				len;
	char				*str;
	t_prts				*node;
	unsigned long long	res;

	res = va_arg(ap, unsigned long long);
	res = ft_cast_given_mod_u(&pmts, res);
	str = ft_itoa_base_ull_ed(res, 8);
	len = ft_strlen(str);
	ft_set_o_flags(&pmts, &str, ft_strlen(str));
	node = (t_prts*)malloc(sizeof(t_prts));
	node->next = NULL;
	ft_helper_type_o(&pmts, len, &str, &node);
	if (pmts.hash && !pmts.zero_value && !pmts.value)
	{
		pmts.hash = 0;
		if (res != 0)
			str = ft_strjoin_free("0", str, 2);
	}
	if (!ft_calc_flags_sum(pmts))
		return (ft_rec_given_data(&node, str));
	else if (pmts.zero_value)
		return (ft_handle_o_zv(pmts, &node, str));
	else
		return (ft_handle_o_v(pmts, &node, str));
}
Exemple #21
0
char		*ft_get_precision(char *str, t_conv *conv, t_env *e)
{
	char	*ret;
	int		i;
	int		n;

	i = 0;
	conv->precision = ft_strnew(0);
	while (str[i] && str[i] != '%'
		&& ft_strchr("hljz sSpdDioOuUxXcC% .1234567890 #0-+", str[i]) != NULL)
	{
		if (str[i] == '.')
		{
			n = 1;
			conv->precision_pos = i;
			while (ft_isdigit(str[i + n]) == 1)
			{
				conv->precision = ft_strjoin_free(conv->precision,
						ft_char_to_str(str[i + n++]));
			}
			break ;
		}
		i++;
	}
	ret = ft_get_conversion(str, conv, e);
	ft_do_precision(&ret, conv);
	return (ret);
}
Exemple #22
0
static void		handle_cd_switch(t_state *state, char **av)
{
    char	*cd_to;
    char	*tmp;

    if ((tmp = ft_strrstr(state->pwd, av[1])))
    {
        cd_to = ft_strdup(state->pwd);
        cd_to[ft_strlen(state->pwd) - ft_strlen(tmp)] = '\0';
        cd_to = ft_strjoin_free(cd_to, av[2]);
        cd_to = ft_strjoin_free(cd_to, tmp + ft_strlen(av[1]));
        handle_cd_to(state, cd_to);
        ft_strdel(&cd_to);
    }
    else
        print_string_not_in_pwd(state, "cd", av[1]);
}
Exemple #23
0
void	ft_do_padding_switch_3_2(t_conv *conv, char **ret,
		char **padding, int i)
{
	if (ft_strchr(conv->flag, '-') == NULL)
	{
		(*padding)[i - 2] = '0';
		(*padding)[i - 1] = 'x';
		*ret = ft_strjoin_free(
				*padding, *ret);
	}
	else
	{
		if ((int)(ft_strlen(*padding) - 2) >= 0)
			(*padding)[ft_strlen(*padding) - 2] = '\0';
		*ret = ft_strjoin_free(ft_strdup("0x"), ft_strjoin_free(
					*ret, *padding));
	}
}
Exemple #24
0
static int	ft_cop(char *str1, char **line, char *str2)
{
	*(ft_strchr(str1, '\n')) = '\0';
	*line = ft_strjoin_free(*line, str1);
	ft_strcpy(str2, str1 + ft_strlen(str1) + 1);
	if (str1 != str2)
		free(str1);
	return (1);
}
Exemple #25
0
void			ft_see(t_cmd *cmd, t_game *g)
{
	t_var	var;
	char	*msg;
	char	*del;

	msg = NULL;
	var = init_var(g, cmd);
	msg = check_case(g, var, msg, 1);
	var.i = 1;
	del = msg;
	msg = ft_strtrim(msg);
	free(del);
	msg = ft_strjoin_free("{", msg, 2);
	msg = ft_strjoin_free(msg, ",", 1);
	ft_while(g, cmd, var, &msg);
	msg = ft_strjoin_free(msg, "}", 1);
	ft_putendl_fd(msg, cmd->fd);
}
Exemple #26
0
static void	last(t_stack **s_one, t_stack *s_two, char *options, char **sols)
{
    ft_s(s_one);
    if (options[1])
    {
        write(1, "\nAction : sa", 12);
        ft_print_stack(*s_one, s_two);
    }
    *sols = ft_strjoin_free(*sols, " sa");
}
Exemple #27
0
void		modif_e2(t_sh *t, t_linez *lz, int k, int *j)
{
	char	*tmp;
	int x = 0;
	if (ft_strncmp("unsetenv", lz->ep_l, 8))
	{
		tmp = ft_strdup(lz->ep_l);
		t->env = realloc_tb(t->env);
		if (search_oplen(&lz->ep_l[k], " =", 10000)
			< search_oplen(&lz->ep_l[k], "<>|&;", 10000))
		{
			if (ft_strchr(&(tmp[k]), '='))
			{
				t->env[++j[1]] = cpy_to(&(tmp[k]), '=');
				x = strchri(&(tmp[k]), '=');
				while (tmp[k + x] == ' ' || tmp[k + x] == '=')
					x++;
			}
			else
			{
				t->env[++j[1]] = cpy_to(&(tmp[k]), ' ');
				x = strchri(&(tmp[k]), ' ');
				while (tmp[k + x] == ' ' || tmp[k + x] == '=')
					x++;
			}
		}
		else
			t->env[++j[1]] = ft_strdup(&(tmp[k]));
		if (x && tmp[k + x])
		{
			replace_char(t->env[j[1]], '=', ' ');
			t->env[j[1]] = epur_str(t->env[j[1]], t);
			if (t->env[j[1]][len(t->env[j[1]]) - 1] == ' ')
				t->env[j[1]][len(t->env[j[1]]) - 1] = '\0';
			t->env[j[1]] = ft_strjoin_free(t->env[j[1]], "=", 1);
			t->env[j[1]] = ft_strjoin_free(t->env[j[1]], &tmp[k + x], 1);
		}
		else
			t->env[j[1]] = ft_strjoin_free(t->env[j[1]], "=", 1);
		ft_strdel(&tmp);
	}
	t->env[++j[1]] = 0;
}
Exemple #28
0
t_prts	*ft_handle_p_zv(t_pmts pmts, t_prts **node, char *res)
{
	char	*tmp;
	size_t	len;

	len = ft_strlen(res) + 2;
	if (pmts.zero_value < len)
	{
		res = ft_strjoin_free("0x", res, 2);
		return (ft_rec_given_data(node, res));
	}
	tmp = ft_malloc_sz(pmts.zero_value - len);
	tmp = ft_memset(tmp, 48, pmts.zero_value - len);
	res = ft_strjoin_free(tmp, res, 3);
	res = ft_strjoin_free("0x", res, 2);
	(*node)->str = res;
	(*node)->len = ft_strlen(res);
	return (*node);
}
Exemple #29
0
static void		tilde_new_str(char *home, char **new_s, char *value, int tilde)
{
	char	*tmp;

	if (tilde)
		tmp = ft_strdup(home);
	else
		tmp = ft_strdup(value);
	*new_s = ft_strjoin_free(*new_s, tmp);
	ft_strdel(&tmp);
}
Exemple #30
0
t_prts	*ft_handle_o_v(t_pmts pmts, t_prts **node, char *res)
{
	char	*tmp;
	size_t	len;

	len = ft_strlen(res);
	if (pmts.hash)
	{
		pmts.value -= 1;
		res = ft_strjoin_free("0", res, 2);
	}
	if (pmts.value < len)
		return (ft_rec_given_data(node, res));
	tmp = ft_malloc_sz(pmts.value - len);
	tmp = ft_memset(tmp, 32, pmts.value - len);
	res = pmts.minus ? ft_strjoin_free(res, tmp, 3)
			: ft_strjoin_free(tmp, res, 3);
	(*node)->str = res;
	(*node)->len = ft_strlen(res);
	return (*node);
}