aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
blob: c9f184cccd98ee21782e78df14188a7f68fc3626 (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
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   path.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/27 15:51:01 by cacharle          #+#    #+#             */
/*   Updated: 2020/04/03 13:53:01 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

/*
** \file   path.c
** \brief  Path hash table manipulation
*/

#include "minishell.h"

/*
** \brief  Number of buckets of a path hash table
*/

#define MS_PATH_HT_SIZE 8192

/*
** \brief          Update `path` with all files
**                 in the directory named `dirname`.
** \param path     Path hash table
** \param dirname  directory name
** \return         Same path or NULL on error
*/

static int			st_add_file(char *dirname, struct dirent *entry, void *path)
{
	char	*filepath;

	if ((filepath = ft_strjoin3(dirname, "/", entry->d_name)) == NULL ||
		ft_htset((t_path)path, entry->d_name, filepath, free) == NULL)
	{
		free(filepath);
		return (-1);
	}
	return (0);
}

/*
** \brief           Update the path
** \param path		Path hash table or NULL to create a new one
** \param path_var  PATH environment variable where
**                  each directory is separated by a ':'
** \return          The updated/created path hash table or NULL on error
*/

t_path					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)  // carefull with non existant dir error
		if (utils_directory_iter(dirs[i], path, st_add_file) == -1)
			return (ft_split_destroy(dirs));
	ft_split_destroy(dirs);
	return (path);
}