Ejemplo n.º 1
0
t_var			*delete_node_var(t_var *v, char *command)
{
	t_var		*tmp;

	tmp = NULL;
	if (v == NULL)
		return (v);
	if (ft_sort_str(command, v->key) == 1)
		v->right = delete_node_var(v->right, command);
	else if (ft_strcmp(command, v->key) != 0)
		v->left = delete_node_var(v->left, command);
	else
	{
		if (v->left == NULL)
			return ((tmp = free_node_var(v->right, v, tmp)));
		else if (v->right == NULL)
			return ((tmp = free_node_var(v->left, v, tmp)));
		tmp = min_value_var(v->right);
		ft_free(&v->key);
		ft_free(&v->value);
		v->key = ft_strdup(tmp->key);
		v->value = ft_strdup(tmp->value);
		v->right = delete_node_var(v->right, tmp->key);
	}
	return (v);
}
Ejemplo n.º 2
0
void			ft_sub(t_env *env, t_process *proc)
{
	t_list	*params;
	t_param	**param;
	int		op_size;

	params = get_parameters(env, proc);
	op_size = calculate_size(env->arena[(proc->pc + 1) % MEM_SIZE], 3, 4);
	if (!(param = check_params(params)))
	{
		proc->pc = ft_mod((proc->pc + op_size + 2) % MEM_SIZE);
		ft_free(params);
		return ;
	}
	proc->carry = 0;
	proc->reg[param[2]->value - 1] = param[0]->real_value
		- param[1]->real_value;
	if (env->flags->v && env->flags->verbose->show_operations)
		verb_sub(env, proc);
	proc->pc = ft_mod((proc->pc + op_size + 2) % MEM_SIZE);
	if (proc->reg[param[2]->value - 1] == 0)
		proc->carry = 1;
	ft_free(params);
	free(param);
}
Ejemplo n.º 3
0
Archivo: ld.c Proyecto: magouin/Corewar
void			ft_ld(t_env *env, t_process *proc)
{
	t_list	*params;
	int		op_size;
	t_param **param;

	params = get_parameters(env, proc);
	op_size = calculate_size(env->arena[(proc->pc + 1) % MEM_SIZE], 2, 4);
	if (!(param = check_params(params)))
	{
		proc->pc = ft_mod((proc->pc + op_size + 2) % MEM_SIZE);
		ft_free(params);
		return ;
	}
	proc->carry = 0;
	if (param[0]->type == IND_CODE)
		proc->reg[param[1]->value - 1] = ft_getnumber(env->arena
			, (proc->pc + param[0]->value % IDX_MOD) % MEM_SIZE, 4);
	else
		proc->reg[param[1]->value - 1] = param[0]->value;
	if (env->flags->v && env->flags->verbose->show_operations)
		verb_ld(proc, param);
	proc->pc = ft_mod((proc->pc + op_size + 2) % MEM_SIZE);
	if (proc->reg[param[1]->value - 1] == 0)
		proc->carry = 1;
	ft_free(params);
	free(param);
}
Ejemplo n.º 4
0
void		ft_lldi(t_env *env, t_process *proc)
{
	t_list	*params;
	int		value;
	int		op_size;

	op_size = calculate_size(env->arena[proc->pc + 1], 3, 2);
	params = get_parameters(env, proc);
	if (count_t_param(params) != 3
			|| va(1, 1, 1, (t_param *)params->content) == -1
			|| va(1, 1, 0, (t_param *)params->next->content) == -1
			|| va(1, 0, 0, (t_param *)params->next->next->content) == -1)
	{
		proc->pc = ft_mod((proc->pc + op_size + 2) % MEM_SIZE);
		ft_free(params);
		return ;
	}
	value = ft_getnumber(env->arena, proc->pc
				+ (((t_param *)params->content)->real_value
				+ ((t_param *)params->next->content)->real_value), 4);
	proc->reg[((t_param *)params->next->next->content)->value - 1] = value;
	if (env->flags->v && env->flags->verbose->show_operations)
		verb_lldi(params, proc);
	proc->pc = ft_mod((proc->pc + op_size + 2) % MEM_SIZE);
	proc->carry = (value == 0) ? 1 : 0;
	ft_free(params);
}
Ejemplo n.º 5
0
void				replace_dol(char **str, char **cmd, int nbr)
{
	int				i;
	int				c;
	char			**tmp;

	i = -1;
	tmp = ft_split_mult(4, *str, ' ', '\t', '\n', '\r');
	while (cmd[nbr])
		nbr++;
	while (tmp[++i])
	{
		if (tmp[i][0] == '$')
		{
			tmp[i]++;
			c = ft_atoi(tmp[i]);
			tmp[i]--;
			if (nbr > c)
			{
				ft_free(&tmp[i]);
				tmp[i] = cmd[c];
			}
		}
	}
	ft_free(str);
	*str = ft_tab_to_str(tmp, ' ');
}
Ejemplo n.º 6
0
void		open_lst_tetri(t_app *app)
{
	int				fd;
	int				rt;
	unsigned char	*data;
	t_tetri			*cursor;

	rt = 1;
	fd = ft_open(app->av[1], O_RDONLY);
	rt = readfile(fd, &data, rt);
	app->tetri = new_tetri(idfrompiece(lire_piece(data), app->pieces));
	ft_free(data);
	cursor = app->tetri;
	rt = readfile(fd, &data, rt);
	while (rt != 0)
	{
		cursor = add_tetri(cursor, idfrompiece(lire_piece(data), app->pieces));
		ft_free(data);
		rt = readfile(fd, &data, rt);
	}
	close(fd);
	app->nb_tetri = count_tetri(app->tetri);
	if (app->nb_tetri > 26)
		ft_puterror();
	app->nb_point = app->nb_tetri * 4;
}
Ejemplo n.º 7
0
char				*get_next_line(int const fd)
{
	static char		*tmp;
	char			*buf;
	char			*size;
	int				ret;

	tmp = (tmp ? tmp : ft_strdup(""));
	if (fd < 0 || BUFF_SIZE < 1)
		return (ft_exit(&tmp));
	buf = ft_strnew(BUFF_SIZE + 1);
	if (!buf)
		return (ft_exit(&tmp));
	while (!(size = ft_strchr(tmp, '\n')) && (ret = read(fd, buf, BUFF_SIZE)))
	{
		buf[ret] = 0;
		tmp = ft_strcleanjoin(tmp, buf);
	}
	ft_free(&buf);
	if (tmp[0] && (size || (size = ft_strrchr(tmp, 0))))
	{
		buf = ft_strsub(tmp, 0, size - tmp);
		ft_free(&tmp);
		tmp = (size[0] ? ft_strdup(size + 1) : ft_strdup(""));
		return (buf);
	}
	return (ft_exit(&tmp));
}
Ejemplo n.º 8
0
int		wild_card(char **arg, int k, int *j)
{
	int				i;
	unsigned int	l;
	char			*s;
	char			*t;

	i = *j;
	while (arg[k][i - 1] && arg[k][i - 1] != ' ')
		i--;
	t = sub_to(&arg[k][i], *j);
	if (ft_strcmp(t, "./"))
		i = *j;
	while (ft_strcmp(t, "./") && arg[k][i - 1] != '/')
		i--;
	if ((s = wc_bis(&arg[k][i], (*j - i - 1), t)) && s != NULL)
	{
		l = *j - i;
		while (s[l])
			insert(&arg, &k, j, s[l++]);
		insert(&arg, &k, j, ' ');
		ft_free(s);
		ft_free(t);
		return (1);
	}
	ft_free(t);
	return (0);
}
Ejemplo n.º 9
0
void			ft_create_win_sdl(t_env *e, int w, int h)
{
	if (w <= 0 || h <= 0)
	{
		if ((e->win_sdl = SDL_CreateWindow(NAME, POS_X, POS_Y, POS_WIDTH, \
						POS_HEIGHT, SDL_WIN_FLAGS)) == NULL)
		{
			ft_putstr_fd("SDL_CreateWindow error ", 2);
			ft_putendl_fd((char*)SDL_GetError(), 2);
			ft_free(e);
			exit(-1);
		}
	}
	else
	{
		if ((e->win_sdl = SDL_CreateWindow(NAME, POS_X, POS_Y, w, \
						h, SDL_WIN_FLAGS)) == NULL)
		{
			ft_putstr_fd("SDL_CreateWindow error ", 2);
			ft_putendl_fd((char*)SDL_GetError(), 2);
			ft_free(e);
			exit(-1);
		}
	}
}
Ejemplo n.º 10
0
void	ft_test(char **tab, t_copy tab2)
{
	int	i;
	int	ok;
	int	k;

	ok = 0;
	i = 0;
	while (i <= 4)
	{
		ft_put_colle(i, &tab2);
		if (ft_tabcmp(tab, tab2.tab))
		{
			k = -1;
			ft_result(i, tab2.c, tab2.l, ok);
			ok = 1;
		}
		i++;
	}
	if (ok == 0)
		ft_putstr("aucune");
	if (tab2.c <= 1)
		ok = 0;
	ft_free(tab, ok);
	ft_free(tab2.tab, ok);
}
Ejemplo n.º 11
0
static void		free_opt(t_opt *opt)
{
	ft_free(&opt->env);
	if (opt->path)
		free(opt->path);
	if (opt->cmd)
		free(opt->cmd);
	ft_free(&opt->ptr);
	free(opt->extra);
}
Ejemplo n.º 12
0
void	ft_s2lfreelist(t_s2l *list)
{
	t_s2l	*tmp;

	while (list)
	{
		tmp = list;
		list = list->next;
		ft_free(tmp->val);
		ft_free(tmp);
	}
}
Ejemplo n.º 13
0
static void		ft_clean_cmd(t_cmd *cmd)
{
	while (cmd)
	{
		ft_free(cmd->origin);
		ft_free(cmd->clean);
		if (cmd->seq)
			ft_clean_seq(cmd->seq);
		cmd = cmd->next;
	}
	return ;
}
Ejemplo n.º 14
0
void	ft_free_tab(char **tab)
{
	int		i;

	i = 0;
	while (tab[i])
	{
		ft_free(tab[i]);
		tab[i] = NULL;
		i++;
	}
	ft_free(tab);
}
Ejemplo n.º 15
0
void		ft_free_op(char *op[9][3])
{
	ft_free(*op[0]);
	ft_free(*op[1]);
	ft_free(*op[2]);
	ft_free(*op[3]);
	ft_free(*op[4]);
	ft_free(*op[5]);
	ft_free(*op[6]);
	ft_free(*op[7]);
	ft_free(*op[8]);
}
Ejemplo n.º 16
0
void	ft_valid_entries(t_infos *e_list, int i, t_options *list, char *dir)
{
	int	j;
	int	check;

	check = 0;
	j = 0;
	while (j < i)
	{
		if (e_list[j].group == NULL)
			check = 1;
		j++;
	}
	if (check == 0)
		ft_print_entries(e_list, i, list, dir);
	else
	{
		j = 0;
		while (j < i)
		{
			ft_putstr("ls: ");
			ft_putstr(e_list[j].name);
			ft_putendl(": Permission denied");
			j++;
		}
		ft_free(e_list, i);
	}
}
Ejemplo n.º 17
0
int	main(void)
{
	char	*ptr;
	char	*name;
	char	**arg;

	while (1)
	{
		write(1, "$>", 2);
		get_next_line(0, &ptr);
		if (ft_strcmp(ptr, "exit") == 0)
			return (0);
		arg = ft_get_args(&ptr, &name);
		if (!ft_check_builtin(arg))
		{
			if (!ft_run_cmd(name, arg))
			{
				ft_putstr_fd(name, 2);
				ft_putendl_fd(": command not found", 2);
			}
		}
		free(name);
		ft_free(arg);
	}
	return (0);
}
Ejemplo n.º 18
0
int		main(int argc, char **argv)
{
	int		s;
	int		n;
	int		j;
	char	**tab;
	char	**grid;

	n = 0;
	j = 0;
	if (argc != 2)
		ft_error(0);
	else
		tab = ft_tab(argv[1]);
	while (j == 0)
	{
		grid = ft_grid(tab, n, &s);
		j = ft_resolution(tab, &grid, s, 0);
		n++;
		if (j == 0)
			ft_free(&grid, s);
	}
	ft_display(grid, s);
	return (0);
}
Ejemplo n.º 19
0
char	*wc_bis(char *arg, int i, char *dir)
{
	char			*s;
	int				l;
	DIR				*d;
	struct dirent	*t;

	if ((d = opendir(dir)) && ft_putstr("\n"))
		s = 0;
	if (((l = 0) || l == 0) && !d)
		return (NULL);
	while ((t = readdir(d)))
	{
		if (!ft_strncmp(t->d_name, arg, i) || i < 0)
		{
			if (ft_putstr(t->d_name) && !s)
				s = ft_strdup(t->d_name);
			else
				l++;
			all_five(l, t->d_name);
		}
	}
	if (ft_putstr("\n") && !closedir(d) && s && !l)
		return (s);
	else if (s)
		ft_free(s);
	return (NULL);
}
Ejemplo n.º 20
0
void		ft_change_pwds(char *pwd, char ***env)
{
	char		**tmp;

	if ((tmp = (char**)malloc(sizeof(char*) * 4)) != NULL)
	{
		tmp[0] = ft_strdup("setenv");
		tmp[3] = NULL;
		if (ft_getenv("OLDPWD", *env))
		{
			tmp[1] = ft_strdup("OLDPWD");
			tmp[2] = ft_getenv("PWD", *env);
			ft_setenv(tmp, env);
			free(tmp[1]);
		}
		if (ft_getenv("PWD", *env))
		{
			tmp[1] = ft_strdup("PWD");
			tmp[2] = ft_strdup(pwd);
			ft_setenv(tmp, env);
		}
		ft_free(&tmp);
		free(tmp);
	}
}
Ejemplo n.º 21
0
t_funct			*delete_node_funct(t_funct *f, char *command)
{
	t_funct		*tmp;

	tmp = NULL;
	if (f == NULL)
		return (f);
	if (ft_sort_str(command, f->key) == 1)
		f->right = delete_node_funct(f->right, command);
	else if (ft_strcmp(command, f->key) != 0)
		f->left = delete_node_funct(f->left, command);
	else
	{
		if (f->left == NULL)
			return ((tmp = free_node_funct(f->right, f, tmp)));
		else if (f->right == NULL)
			return ((tmp = free_node_funct(f->left, f, tmp)));
		tmp = min_value_funct(f->right);
		ft_free(&f->key);
		ft_free_array(&f->content);
		f->key = ft_strdup(tmp->key);
		f->content = ft_strdup_array(tmp->content);
		f->right = delete_node_funct(f->right, tmp->key);
	}
	return (f);
}
Ejemplo n.º 22
0
void	ft_main2(int fd)
{
	char		*file;
	t_map		map;
	t_tetri		*tetri;

	if (!(file = ft_getfile(fd, &map)) ||
			!(ft_check_file(file)))
		ft_putstr("error\n");
	else
	{
		tetri = ft_create_tetri(map);
		ft_fill(tetri, file, map);
		ft_init_map(&map);
		map.ret = 1;
		ft_solve(tetri, &map);
		while (map.ret == 0)
		{
			map.size++;
			ft_solve(tetri, &map);
		}
		ft_print_map(tetri, map);
		ft_free(&tetri);
	}
}
int			main(void)
{
	char	*str;
	int i;

	str = (char *)ft_malloc(sizeof(char) * 40);
	if (!str)
	{
		printf("str don't exist\n");
		return (-1);
	}

	i = 0;
	while (i < 40)
	{
		str[i]= 'a';
		i++;
	}

	ft_realloc(str, 3);

	i = -17;
	while (i < 10)
	{
		printf("%p => %d\n", str, (int)str[i]);
		i++;
	}

	ft_free(str);
}
Ejemplo n.º 24
0
static void	add_word(t_s2l **out, const char *s, int start, int i)
{
	char	*tmp;

	tmp = ft_strsub(s, start, i);
	ft_s2lpushback(out, ft_s2lcreate(tmp));
	ft_free(tmp);
}
Ejemplo n.º 25
0
  ft_close_stream_by_free( FT_Stream  stream )
  {
    ft_free( NULL, stream->descriptor.pointer );

    stream->descriptor.pointer = NULL;
    stream->size               = 0;
    stream->base               = 0;
  }
Ejemplo n.º 26
0
static char		*get_line(char *tmp, char *buffer, int check, int ret)
{
	tmp[ret] = '\0';
	if (check)
		buffer = ft_strjoin(buffer, tmp);
	else
		buffer = ft_strdup(tmp);
	ft_free(tmp);
	return (buffer);
}
Ejemplo n.º 27
0
void			ft_init_sdl(t_env *e)
{
	if (SDL_Init(SDL_INIT_FLAGS | SDL_INIT_AUDIO) < 0)
	{
		ft_putstr_fd("SDL_Init error ", 2);
		ft_putendl_fd((char*)SDL_GetError(), 2);
		ft_free(e);
		exit(-1);
	}
}
Ejemplo n.º 28
0
void			ft_clean_seq(t_seq *seq)
{
	int	i;

	while (seq)
	{
		i = 0;
		ft_free(seq->cmd);
		test_free(&seq->args);
		if (seq->exec_tab)
		{
			while (seq->exec_tab[i])
			{
				ft_free(seq->exec_tab[i]);
				i++;
			}
			ft_free(seq->exec_tab);
		}
		seq = seq->next;
	}
	return ;
}
Ejemplo n.º 29
0
void			*ft_realloc(void *ptr, size_t size)
{
	if (size <= 0)
	{
		ft_free(ptr);
		return (NULL);
	}
	else if (!ptr)
		return (ft_malloc(size));
	else
		return (search_realloc(ptr, size));
	return (NULL);
}
Ejemplo n.º 30
0
char	*ft_strglue(char **split, char glue)
{
	int		i;
	int		nb_split;
	char	*out;
	char	*tmp;
	char	*tmp2;

	i = 0;
	out = ft_strdup("");
	nb_split = ft_splitcount(split);
	while (split[i])
	{
		tmp = (i > 0 && i < nb_split ? ft_strjoinc(out, glue) : ft_strdup(""));
		tmp2 = ft_strjoin(tmp, split[i]);
		i++;
		ft_free(out);
		ft_free(tmp);
		out = tmp2;
	}
	return (out);
}