aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
blob: 3ac12d92c0a7292921063a24eaab37f05ef63a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   path.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/27 15:51:01 by cacharle          #+#    #+#             */
/*   Updated: 2020/02/28 12:33:31 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

#define MS_PATH_HT_SIZE 4096

/*
** Update `path` with all files in the directory named `dirname`.
*/

static t_path			st_path_dir_update(t_path path, char *dirname)
{
	DIR				*dir;
	struct dirent	*entry;
	char			*tmp;

	if ((dir = opendir(dirname)) == NULL)
		return (NULL);
	while ((entry = readdir(dir)) != NULL)
	{
		if ((tmp = ft_strjoin3(dirname, "/", entry->d_name)) == NULL)
			return (NULL);
		if (ft_htset(path, entry->d_name, tmp, ms_ht_del_str_entry) == NULL)
			return (NULL);
	}
	if (closedir(dir) == -1)
		return (NULL);
	return (path);
}

/*
** Update (or create if `path` is NULL) `path` according to `path_str`
** (each directory is separated by a ':').
*/

t_path					ms_path_update(t_path path, char *path_var)
{
	int		i;
	char	**dirs;

	if (path_var == NULL)
		return (NULL);
	if (path == NULL && (path = ft_htnew(MS_PATH_HT_SIZE)) == NULL)
		return (NULL);
	if ((dirs = ft_split(path_var, ':')) == NULL)
		return (NULL);
	i = -1;
	while (dirs[++i] != NULL)
		if (st_path_dir_update(path, dirs[i]) == NULL)
			return (ft_split_destroy(dirs));
	ft_split_destroy(dirs);
	return (path);
}