summaryrefslogtreecommitdiffstats
path: root/src/b_type.c
blob: 3dd7b420678a0a2b02c9e2414dbbe6187a86c0c3 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   b_type.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdint.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/limits.h>

#include "d_define.h"
#include "f_fail.h"
#include "s_line.h"
#include "s_struct.h"
#include "u_path.h"
#include "u_utils.h"

static int8_t	absolute_path_exists(char com[])
{
	int32_t	fd;
	DIR		*dir;

	if ((dir = opendir(com)) != NULL)
	{
		closedir(dir);
		return (0);
	}
	if ((fd = open(com, O_RDONLY)) != -1)
	{
		close(fd);
		return (1);
	}
	return (0);
}

static void		type_get_path(char fullpath[], char com[], t_msh *msh)
{
	if (ft_ischarset("/.", com[0]) == TRUE)
	{
		if (absolute_path_exists(com))
		{
			ft_strlcpy(fullpath, com, PATH_MAX);
			return ;
		}
		return ;
	}
	u_search_in_path(fullpath, com, PATH_MAX, msh);
}

static uint8_t	b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh)
{
	char	fullpath[PATH_MAX];

	fullpath[0] = C_NUL;
	type_get_path(fullpath, ptr, msh);
	if (fullpath[0] != C_NUL)
		ft_printf("%s is %s\n", ptr, fullpath);
	else
	{
		ft_printf("minishell: type: %s: not found\n", ptr);
		ret = 1;
	}
	return (ret);
}

static uint8_t	b_check_builtins(char *ptr)
{
	char	tmp[M_BUILTINS_REF_LEN];
	char	*tok_bu;

	ft_strlcpy(tmp, M_BUILTINS_REF, M_BUILTINS_REF_LEN);
	tok_bu = ft_strtok(tmp, ":");
	while (tok_bu != NULL
		&& ft_strncmp(ptr, tok_bu, ft_strlen(tok_bu) + 1) != 0)
	{
		tok_bu = ft_strtok(NULL, ":");
	}
	if (tok_bu != NULL)
	{
		ft_printf("%s is a shell builtin\n", ptr);
		return (0);
	}
	return (1);
}

uint8_t			b_type(char *args[], t_msh *msh)
{
	t_lalias	*p_alias;
	char		**ptr;
	int32_t		ret;

	ptr = args;
	if (*ptr == NULL)
		return (0);
	ret = 0;
	while (*ptr != NULL)
	{
		p_alias = msh->alias;
		while (p_alias != NULL &&
			ft_strncmp(*ptr, p_alias->name,
			ft_strlen(p_alias->name) + 1) != 0)
			p_alias = p_alias->next;
		if (p_alias != NULL)
		{
			ft_printf("%s is aliased to `%s'\n", *ptr, p_alias->val);
		}
		else if (b_check_builtins(*ptr) == 1)
			ret = b_check_nonbuilt(*ptr, ret, msh);
		ptr++;
	}
	return (ret);
}