Example #1
0
static void		update_env(char *path, char *oldpwd, t_env *env)
{
	int		i;
	char	**env_ptr;

	i = 0;
	env_ptr = env->environ;
	while (env_ptr[i])
	{
		if (!ft_strncmp(env_ptr[i], "OLDPWD=", 7))
		{
			ft_strdel(&env_ptr[i]);
			env_ptr[i] = ft_strjoin("OLDPWD=", oldpwd);
		}
		if (!ft_strncmp(env_ptr[i], "PWD=", 4))
		{
			ft_strdel(&env_ptr[i]);
			env_ptr[i] = ft_strjoin("PWD=", path);
		}
		i++;
	}
}
Example #2
0
static	char	*sumstring(char **split, int len)
{
	char	*tmp;
	char	*ret;
	int		i;

	ret = NULL;
	i = -1;
	while (++i < len - 2)
	{
		tmp = ret;
		ret = ft_strjoin(ret, split[i]);
		ft_strdel(&tmp);
		if (i < len - 3)
		{
			tmp = ret;
			ret = ft_strjoin(ret, " ");
			ft_strdel(&tmp);
		}
	}
	return (ret);
}
Example #3
0
int			ft_add_var(t_sh *sh, char *name, char *value)
{
	int		i;
	char	**new_env;

	i = 0;
	sh->env_size += 1;
	new_env = (char**)ft_memalloc(sizeof(char*) * sh->env_size);
	if (!new_env)
		return (-1);
	while (sh->env[i] != NULL)
	{
		new_env[i] = ft_strdup(sh->env[i]);
		free(sh->env[i]);
		i++;
	}
	free(sh->env);
	new_env[i] = ft_strjoin(name, "=");
	new_env[i] = ft_strjoin(new_env[i], value);
	sh->env = new_env;
	return (0);
}
void    ft_cut_flag_u2_llong(t_cut *t_v, t_var **var, char **str, int count[3])
{
	if (var[count[2]]->stars < 0)
		ft_cut_flag_u2_1llong(t_v, var, str, count);
	else
	{
		while ((*t_v).k < var[count[2]]->stars - (*t_v).nbr + 1)
		{
			str[count[0]] = ft_strjoin(" ", str[count[0]]);
			(*t_v).k++;
		}
	}
}
Example #5
0
void	copy_line(t_env *env)
{
	if (I_L1 == NULL && I_L2 == NULL)
		return ;
	if (I_CB != NULL)
		free(I_CB);
	if (I_L1 == NULL && I_L2 != NULL)
		I_CB = ft_strdup(I_L2);
	else if (I_L1 != NULL && I_L2 == NULL)
		I_CB = ft_strdup(I_L1);
	else if (I_L1 != NULL && I_L2 != NULL)
		I_CB = ft_strjoin(I_L1, I_L2);
}
Example #6
0
int				ft_cd(char **args, char ***env)
{
	int		i;

	i = 1;
	while (args[i] && args[i][0] == '-' && args[i][1] && args[i][1] != '-')
	{
		if (ft_strchr(args[i], 'P') == NULL && ft_strchr(args[i], 'L') == NULL)
		{
			ft_strerror(ft_strjoin("42sh: cd: invalid option: ", args[i]));
			ft_putendl("42sh: usage: cd [-L|-P] [dir]");
			return (1);
		}
		i++;
	}
	if (!args[i] || !args[i + 1])
		return (cd_write_in_pwd(args + i - 1, env));
	if (!args[i + 2])
		return (cd_search_in_pwd(args + i - 1, env));
	ft_strerror(ft_strjoin("42sh: cd: too much arguments: ", args[1]));
	return (1);
}
Example #7
0
t_file	*ft_putinfo(t_file *file, char *filename, char *dirname, int isln)
{
	ssize_t		i;
	t_stat	filestats;
	char	*path;
	
	path = ft_strjoin(dirname, "/");
	path = ft_strjoin(path, filename);
	if ((lstat(path, &filestats)) == -1)
		lstat(filename, &filestats);
	file->type = ft_gettype(filestats);
	file->rights = ft_getrights(filestats);
	file->links = (int)(filestats.st_nlink);
	file->uname = (getpwuid(filestats.st_uid))->pw_name;
	file->gname = (getgrgid(filestats.st_gid))->gr_name;
	if (file->type != 'b' && file->type != 'c')
		file->size = filestats.st_size;
	file->lnpath = malloc(sizeof(char ) * ft_strlen(filename) + 1);
	file->laccess = filestats.st_atimespec;
	file->blocks = filestats.st_blocks;
	file->ldatamod = filestats.st_mtimespec;
	file->lstatch = filestats.st_ctimespec;
	file->birthtime = filestats.st_birthtimespec;
	file->dname = ft_strdup(filename);
	//printf("%c\n", file->type);
	if (file->type == 'l')
	{
		if ((i = readlink(filename, file->lnpath, file->size)) != -1)
			file->lnpath[i] = '\0';
	}
		//else
		//	return ;
	if (file->type == 'c' || file->type == 'b')
	{
		file->major = (int)major(filestats.st_rdev);
		file->minor = (int)minor(filestats.st_rdev);
	}
	return(file);
}
Example #8
0
int			ft_recus_4(t_push **stacka, t_push **stackb, int i, t_info *info)
{
	if (ft_size_list(*stacka) >= 3)
	{
		ft_sort_ra(stacka);
		info->str = ft_strjoin(info->str, "ra ");
		if (ft_recus(stacka, stackb, i - 1, info) == 1)
			return (1);
		info->str = ft_del_last_c(info->str, 3);
		ft_sort_rra(stacka);
	}
	if (ft_size_list(*stackb) >= 3)
	{
		ft_sort_rb(stackb);
		info->str = ft_strjoin(info->str, "rb ");
		if (ft_recus(stacka, stackb, i - 1, info) == 1)
			return (1);
		info->str = ft_del_last_c(info->str, 3);
		ft_sort_rrb(stackb);
	}
	return (0);
}
Example #9
0
char	*ft_freejoin(char *s1, char *s2)
{
	char	*s;

	if (s1 == NULL && s2 == NULL)
		return (NULL);
	s = ft_strjoin(s1, s2);
	if (s1)
		ft_strdel(&s1);
	if (s2)
		ft_strdel(&s2);
	return (s);
}
Example #10
0
void            ls_read(t_btree **tree, char *pathname)
{
	struct dirent *file;
	DIR     *rep;

	rep = opendir(pathname);
	if (rep != NULL)
	{
		if (!(check_slash(pathname)))
			pathname = ft_strjoin(pathname, "/");
		while((file = readdir(rep)) != NULL)
		{
			pathname = ft_strjoin(pathname, file->d_name);
			btree_insert_data(tree, file->d_name, file->d_type);
		}
		free(pathname);
		pathname = NULL;
		closedir(rep);
	}
	else
		print_error(pathname);
}
Example #11
0
char		*db(long long i, char *str)
{
	int i2;

	i2 = 0;
	if (i < 0)
		i2 = 1;
	i = plus(i);
	str = ft_utoa(i);
	if (i2 == 1)
		str = ft_strjoin("-", str);
	return (str);
}
Example #12
0
void	copy_options(t_op *o, t_op options, char *name)
{
	o->l = options.l;
	o->r = options.r;
	o->maj_r = options.maj_r;
	o->a = options.a;
	o->t = options.t;
	o->u = options.u;
	o->g = options.g;
	o->c = options.c;
	o->p = options.p;
	o->one = options.one;
	o->flag = 1;
	if (options.path != NULL)
	{
		o->path = ft_strjoin(options.path, "/");
		o->path = ft_strjoin(o->path, name);
	}
	else
		o->path = ft_strdup(name);
	o->files = NULL;
}
Example #13
0
static char		*ft_read(const int fd, char *stock, int *ret)
{
	char	buf[BUFF_SIZE + 1];
	char	*tmp;

	*ret = read(fd, buf, BUFF_SIZE);
	buf[*ret] = '\0';
	tmp = stock;
	if (!(stock = ft_strjoin(stock, buf)))
		return (NULL);
	ft_strdel(&tmp);
	return (stock);
}
Example #14
0
char	*ft_cjoin(char *s1, char *s2)
{
	char	*stock;

	if (s1 == NULL && s2 == NULL)
		return (NULL);
	stock = ft_strjoin(s1, s2);
	if (s1)
		ft_strdel(&s1);
	if (s2)
		ft_strdel(&s2);
	return (stock);
}
Example #15
0
char	*ft_strrevjoinleak(char *s1, char *s2)
{
	char *tmp;

	if (s1 == NULL)
		return (NULL);
	else if (s2 == NULL)
		return (s1);
	tmp = ft_strjoin(s1, s2);
	free(s1);
	s1 = tmp;
	return (s1);
}
Example #16
0
int			ft_recus_2(t_push **stacka, t_push **stackb, int i, t_info *info)
{
	if (*stackb != NULL)
	{
		ft_pa(stacka, stackb);
		info->str = ft_strjoin(info->str, "pa ");
		if (ft_recus(stacka, stackb, i - 1, info) == 1)
			return (1);
		info->str = ft_del_last_c(info->str, 3);
		ft_sort_pb(stacka, stackb);
	}
	if (*stacka != NULL)
	{
		ft_sort_pb(stacka, stackb);
		info->str = ft_strjoin(info->str, "pb ");
		if (ft_recus(stacka, stackb, i - 1, info) == 1)
			return (1);
		info->str = ft_del_last_c(info->str, 3);
		ft_pa(stacka, stackb);
	}
	return (0);
}
Example #17
0
char					*ft_graphic_tna(t_env *env)
{
	char				*ret;
	char				*tmp;
	int					i;

	i = 0;
	ret = NULL;
	while (i < env->max_team)
	{
		tmp = ft_strjoin(ret, "tna ");
		if (ret)
			ft_memdel((void **)&ret);
		ret = ft_strjoin(tmp, env->teams[i].name);
		ft_memdel((void **)&tmp);
		tmp = ft_strjoin(ret, "\n");
		ft_memdel((void **)&ret);
		ret = tmp;
		i++;
	}
	return (ret);
}
Example #18
0
static void		update_env(t_params *p, char *new_dir)
{
	char	*tmp;

	p->oldpwd = p->pwd;
	if (ft_strstr(new_dir, "/nfs/"))
		p->pwd = ft_strdup(ft_strstr(new_dir, "/nfs/"));
	else
		p->pwd = ft_strdup(new_dir);
	if (!(update_var(p, "PWD=", p->pwd)))
	{
		tmp = ft_strjoin("setenv PWD ", p->pwd);
		ft_add_env(tmp, p);
		ft_strdel(&tmp);
	}
	if (!(update_var(p, "OLDPWD=", p->oldpwd)))
	{
		tmp = ft_strjoin("setenv OLDPWD ", p->oldpwd);
		ft_add_env(tmp, p);
		ft_strdel(&tmp);
	}
}
Example #19
0
void	put_file(char *name)
{
	int		fd;

	if ((fd = open(name, O_RDONLY)) < 0)
	{
		printf("Error -> File not found\n");
		send(g_sock, "FAILURE\n", 8, 0);
		return ;
	}

	DIR*dir;
	struct dirent*dirent;

	if ((dir = opendir(".")))
	{
		while ((dirent = readdir(dir)))
		{
			if (ft_strcmp(dirent->d_name, name) == 0)
			{
				if (dirent->d_type == DT_DIR)
				{
					printf("Error -> You can't copy a directory\n");
					send(g_sock, "FAILURE\n", 8, 0);
					close(fd);
					closedir(dir);
					return ;
				}
			}
		}
	}
	closedir(dir);
	int		length;
	char	*addr;
	struct stat sb;

	if (fstat(fd, &sb) == -1)
	{
		printf("Error -> Protected file\n");
		send(g_sock, "FAILURE\n", 8, 0);
		close(fd);
		return ;
	}
	length = sb.st_size;
	addr = mmap(0, length, PROT_READ, MAP_PRIVATE, fd, 0);
	char*str = ft_strjoin(ft_itoa(length), "\n")	;
	send(g_sock, "SUCCESS\n", 8, 0);
	send(g_sock, str, ft_strlen(str), 0);
	send(g_sock, addr, length, 0);
	close(fd);
}
void	ft_cut_flag_o2_maj(t_cut *t_v, t_var **var, char **str, int count[3])
{
	if ((*t_v).c_d >= (*t_v).nbr)
		(*t_v).diff = (*t_v).c_d;
	else
		(*t_v).diff = (*t_v).nbr;
	if (var[count[2]]->entier < 0)
		ft_cut_flag_o2_1maj(t_v, var, str, count);
	else
	{
		while ((*t_v).k < (*t_v).c_d - (*t_v).nbr)
		{
			str[count[0]] = ft_strjoin("0", str[count[0]]);
			(*t_v).k++;
		}
		(*t_v).k = 0;
		while ((*t_v).k < (*t_v).check - (*t_v).diff)
		{
			str[count[0]] = ft_strjoin(" ", str[count[0]]);
			(*t_v).k++;
		}
	}
}
Example #21
0
char			*complete_behind(char *str, int size, const char *type)
{
	char	*res;

	res = ft_strdup(str);
	free(str);
	while (size-- && size > 0)
	{
		str = res;
		res = ft_strjoin(res, (char*)type);
		free(str);
	}
	return (res);
}
Example #22
0
static void	cpb_next(char **line, char *tmp)
{
	char	*cpy;

	if (*line)
	{
		cpy = ft_strjoin(*line, tmp);
		free(*line);
		free(tmp);
		*line = cpy;
	}
	else
		*line = tmp;
}
static void	handle_neg_precision(t_format *f)
{
	char **tmp;

	if (f->precision < 0)
	{
		f->length = -f->precision;
		f->precision = 0;
		f->precision_set = 1;
		tmp = &(f->flags);
		f->flags = ft_strjoin(f->flags, "-");
		free(tmp);
	}
}
Example #24
0
void	ft_relative_path(t_sh *sh, char *path, int i)
{
	char	**pathes;
	int		j;
	int		k;

	j = -1;
	k = ft_getenv_id(sh->env, "HOME", 4);
	pathes = ft_strsplit(path, '/');
	if (path[0] == '~')
		sh->env[i] = ft_strjoin("PWD=", sh->env[k]);
	else
	{
		while (pathes[++j])
		{
			if (!ft_strcmp(pathes[j], ".."))
				sh->env[i] = ft_strsub_rchr(sh->env[i], '/');
			else
				sh->env[i] = ft_strjoin(ft_strjoin(sh->env[i], "/"), pathes[j]);
		}
	}
	sh->pwd = sh->env[i];
}
Example #25
0
void    ft_cut_flag_x4(t_cut *t_v, char **str, int count[3])
{
	if ((*t_v).flag < -1 && (*t_v).c != '0')
		ft_cut_flag_x4_1(t_v, str, count);
	else if ((*t_v).flag > 0 && ((*t_v).c == '0' || (*t_v).c == '.'))
		ft_cut_flag_x4_2(t_v, str, count);
	else if ((*t_v).flag < -1 && (*t_v).c == '0')
	{
		while ((*t_v).k > (*t_v).flag)
		{
			str[count[0]] = ft_strjoin("0", str[count[0]]);
			(*t_v).k--;
		}
	}
	else
	{
		while ((*t_v).k < (*t_v).flag - (*t_v).nbr)
		{
			str[count[0]] = ft_strjoin(" ", str[count[0]]);
			(*t_v).k++;
		}
	}
}
void	make_msg(t_env *e, int cs, char *line)
{
	char	*msg;
	char	*tmp;
	char	*tosend;
	int		i;

	i = 0;
	msg = find_msg(line);
	while (i < e->maxfd)
	{
		if (e->users[i]->type == FD_CLT &&
			ft_strcmp(line, e->users[i]->name) == 0)
		{
			tmp = ft_strjoin(e->users[cs]->name, " send to you : ");
			tosend = ft_strjoin(tmp, msg);
			free(tmp);
			send(e->users[i]->sock, tosend, ft_strlen(tosend) + 1, 0);
			free(tosend);
		}
		i++;
	}
}
Example #27
0
void	test_strjoin()
{
	char	*tmp = "Hello ";
	char	*tmp2 = "MotherFucker";
	char	*dst;

	dprintf(1, "Strjoin			");
	dst = ft_strjoin(tmp, tmp2);
	if (strcmp(dst, "Hello MotherFucker") != 0) {
		dprintf(1, "\x1b[31mFail\x1b[0m\n	Params:\n		s1 : 'Hello '		s2 : 'MotherFucker'\n\n		Valeur attendue : Hello MotherFucker\n		Valeur obtenue : %s\n", dst);
		return;
	}
	dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
Example #28
0
int		ft_read_fd(char **stay, int fd)
{
	char	*buf;
	int		ret;

	buf = ft_strnew(BUFF_SIZE + 1);
	ret = read (fd, buf, BUFF_SIZE);
	if (ret == -1)
		return (-1);
	buf[ret] = '\0';
	*stay = ft_strjoin(*stay, buf);
	ft_strdel(&buf);
	return (ret);
}
Example #29
0
void		ft_father(t_term *term)
{
	char	*tmp;
	int		y;

	tmp = NULL;
	dup2(term->pipe[1], STDOUT_FILENO);
	close(term->pipe[0]);
	(y = -1) ? ft_check_cmds(term) : 0;
	(term->i) ? ft_env_i(term) : 0;
	while (term->cmds[++y])
		(ft_strcmp(term->cmds[y], term->path) == 0) ?\
		term->cmds = &term->cmds[y] : 0;
	y = 0;
	(!term->cmds[1]) ? (tmp = term->cmds[0]) : 0;
	while (term->cmds[++y])
	{
		(!tmp) ? (tmp = ft_strdup(term->cmds[y - 1])) : 0;
		tmp = ft_strjoin(ft_strjoin(tmp, " "), term->cmds[y]);
	}
	execve(term->path, term->cmds, term->env);
	term->path = NULL;
}
Example #30
0
void		ft_check_close1(t_parse *parse, int i)
{
	char *tmp;

	if (parse->cmd[i + 4])
	{
		tmp = ft_strdup(&parse->cmd[i + 4]);
		parse->cmd[i] = '\0';
		parse->cmd = ft_strjoin(parse->cmd, tmp);
	}
	else
		parse->cmd[i] = '\0';
	parse->close1 = 1;
}