summaryrefslogtreecommitdiffstats
path: root/src/s_init.c
blob: 63861e66560db53183f24b5d4c6d896c75ebd37f (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   s_init.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 <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>

#include "b_export_next.h"
#include "d_define.h"
#include "f_fail.h"
#include "m_funptr.h"
#include "s_init.h"
#include "u_vars.h"
#include "u_vars_next.h"

static char
	**dupenv_del(char **nenvp,
					uint64_t i)
{
	while (i > 0)
	{
		ft_memdel((void*)&nenvp[i]);
		i--;
	}
	ft_memdel((void*)&nenvp);
	return (NULL);
}

static char
	**dupenv(char *const envp[])
{
	uint64_t	i;
	char		**nenvp;

	i = 0;
	while (envp[i])
	{
		i++;
	}
	if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*))))
	{
		return (NULL);
	}
	i = 0;
	while (envp[i])
	{
		if (!(nenvp[i] = ft_strdup(envp[i])))
			return (dupenv_del(nenvp, i));
		i++;
	}
	nenvp[i] = NULL;
	return (nenvp);
}

static void
	inc_shlvl(t_msh *msh)
{
	int32_t	shlvl;
	char	*str_one;
	char	*str_two;
	char	*tmp;
	char	fmt[8];

	if ((tmp = u_get_var_value("$SHLVL", msh)) == NULL)
	{
		ft_memcpy(fmt, "SHLVL=1", 8);
		b_export_with_equals(fmt, msh);
	}
	else
	{
		ft_memdel((void*)&tmp);
		str_two = u_get_var_value("$SHLVL", msh);
		shlvl = ft_atoi(str_two);
		if (shlvl >= 999)
			f_shlvl_too_high(shlvl);
		shlvl = (shlvl >= 999) ? 0 : shlvl;
		shlvl = (shlvl < 0) ? 0 : shlvl + 1;
		str_one = ft_itoa(shlvl);
		u_subst_var_value("$SHLVL", str_one, msh);
		ft_memdel((void*)&str_one);
		ft_memdel((void*)&str_two);
	}
}

static char
	*set_cwd(t_msh *msh)
{
	char	*cwd;
	char	fmt[PATH_MAX];
	DIR		*dir;

	if ((cwd = u_get_var_value("$PWD", msh)) == NULL)
	{
		cwd = getcwd(NULL, 0);
		ft_sprintf(fmt, "%s=%s", "PWD", cwd);
		b_export_with_equals(fmt, msh);
		return (cwd);
	}
	if ((dir = opendir(cwd)) != NULL)
		closedir(dir);
	else if (errno == ENOENT)
	{
		ft_memdel((void*)&cwd);
		cwd = getcwd(NULL, 0);
		ft_sprintf(fmt, "%s=%s", "PWD", cwd);
		b_export_with_equals(fmt, msh);
		return (cwd);
	}
	return (cwd);
}

t_msh
	*init_msh(char *const argv[],
			char *const envp[])
{
	t_msh	*msh;

	if (!(msh = (t_msh*)malloc(sizeof(t_msh))))
		return (NULL);
	if (!(msh->ps_one = ft_strdup(FT_PS_ONE)))
		return (NULL);
	if (!(msh->shname = ft_strdup(argv[0])))
		return (NULL);
	/* TODO: shname: care about "./", try with symlinks */
	msh->envp = NULL;
	if (!(msh->envp = dupenv(envp)))
		return (NULL);
	msh->ret = 0;
	init_buptr(msh);
	msh->curr = NULL;
	msh->vars = NULL;
	msh->cwd = set_cwd(msh);
	msh->env_fork_tmp[0][0] = '\0';
	inc_shlvl(msh);
	return (msh);
}