Exemple #1
0
void				ft_fetch_lst(DIR *dirp, char *filename, t_dlst *headfile,
		t_dlst *headdir)
{
	t_node			*nodefile;
	t_node			*nodedir;
	t_dlst			headerror;
	struct dirent	*tmp;

	dlst_init(&headerror);
	while ((tmp = readdir(dirp)))
	{
		if (tmp->d_name[0] == '.' && !GET(C_NODE(t_info, headfile)->opt, OPT_A))
			continue ;
		if (!(nodefile = (t_node *)malloc(sizeof(t_node))))
			ft_error_malloc();
		nodefile->namtyp = *tmp;
		ft_save(filename, nodefile, headfile, &headerror);
		if (S_ISDIR(nodefile->statfile.st_mode)
				&& ft_strcmp(".", nodefile->namtyp.d_name)
				&& ft_strcmp("..", nodefile->namtyp.d_name))
		{
			if (!(nodedir = (t_node *)malloc(sizeof(t_node))))
				ft_error_malloc();
			nodedir->namtyp = *tmp;
			ft_save(filename, nodedir, headdir, &headerror);
		}
	}
	flush_headerror(&headerror);
}
Exemple #2
0
void	ft_realloc_env(char ***env, char* varname, char* varvalue)
{
	char	**tmp;
	int		i;
	int		l;

	i = 0;
	while ((*env)[i] != NULL)
		i++;
	if (!(tmp = (char**)malloc(sizeof(char*) * (i + 2))))
		ft_error_malloc("set new environment variable");
	i = 0;
	while ((*env)[i] != NULL)
	{
		tmp[i] = (*env)[i];
		i++;
	}
	l = ft_strlen(varname) + ft_strlen(varvalue);
	if (!(tmp[i] = (char*)malloc(sizeof(char) * (l + 2))))
		ft_error_malloc("set new environment variable");
	tmp[i] = ft_strjoin(varname, ft_strjoin("=", varvalue));
	tmp[i][l + 2] = '\0';
	tmp[i + 1] = NULL;
	(*env) = tmp;
}
Exemple #3
0
static void		ls_fetch_in_arg(char *filename, t_dlst *headfile,
		t_dlst *headdir, t_dlst *headerror)
{
	t_node		*node;

	if (!(node = (t_node *)malloc(sizeof(t_node))))
		ft_error_malloc();
	ft_strcpy(node->namtyp.d_name, filename);
	if (!(node->path = (char *)malloc(sizeof(char) * ft_strlen(filename) + 1)))
		ft_error_malloc();
	ft_strcpy(node->path, filename);
	if (lstat(node->path, &node->statfile) == -1)
		dlst_add_tail(&node->dlst, headerror);
	else if (S_ISREG(node->statfile.st_mode))
		dlst_add_tail(&node->dlst, headfile);
	else if (S_ISLNK(node->statfile.st_mode) && case_sl_file(node))
		dlst_add_tail(&node->dlst, headfile);
	else
		dlst_add_tail(&node->dlst, headdir);
}
Exemple #4
0
static void			ft_save(char *dirname, t_node *node, t_dlst *head,
					t_dlst *headerror)
{
	if (!(node->path = (char *)malloc(sizeof(char)
					* ((ft_strlen(dirname)
					+ ft_strlen(node->namtyp.d_name) + 2)))))
		ft_error_malloc();
	ft_strcpy(node->path, dirname);
	if (node->path[ft_strlen(node->path) - 1] != '/')
		ft_strcat(node->path, "/");
	ft_strcat(node->path, node->namtyp.d_name);
	if (lstat(node->path, &node->statfile) == -1)
		dlst_add_tail(&node->dlst, headerror);
	else
		dlst_add_tail(&node->dlst, head);
}
Exemple #5
0
char	*ft_strmapi(char const *s, char (*f) (unsigned int, char))
{
	char			*str;
	unsigned int	i;

	i = -1;
	if (!s || !f)
	{
		ft_error_null("ft_strmapi");
		return (NULL);
	}
	if ((str = ft_strnew(ft_strlen(s))))
	{
		while (s[++i])
			str[i] = f(i, s[i]);
		return (str);
	}
	ft_error_malloc("ft_strmapi");
	return (NULL);
}
Exemple #6
0
void	ft_realloc_env_rev(char ***env, char *varname)
{
	char	**tmp;
	int		i;
	int		j;

	i = 0;
	while ((*env)[i] != NULL)
		i++;
	if (!(tmp = (char**)malloc(sizeof(char*) * i)))
		ft_error_malloc("unset environment variable");
	i = j = 0;
	while ((*env)[i] != NULL)
	{
		if (ft_strncmp((*env)[i], varname, ft_strlen(varname)) == 0)
			i++;
		tmp[j] = (*env)[i];
		i++;
		j++;
	}
	tmp[j] = NULL;
	free((*env));
	(*env) = tmp;
}