-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenviroment.c
58 lines (51 loc) · 1.12 KB
/
enviroment.c
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
#include "holberton.h"
/**
* make_enviroment - make the shell environment from the environment.
* @env: environment passed to main
*
* Return: pointer to the new environment
*/
char **make_enviroment(char **env)
{
char **newenviroment = NULL;
size_t i;
for (i = 0; env[i] != NULL; i++)
;
newenviroment = malloc(sizeof(char *) * (i + 1));
if (newenviroment == NULL)
{
perror("Fatal Error");
exit(1);
}
for (i = 0; env[i] != NULL; i++)
newenviroment[i] = _strdup(env[i]);
newenviroment[i] = NULL;
return (newenviroment);
}
/**
* free_env - free the shell's environment
* @env: shell's environment
*
* Return: void
*/
void free_env(char **env)
{
unsigned int i;
for (i = 0; env[i] != NULL; i++)
free(env[i]);
free(env);
}
/**
* chdir_to_env - go to the directory that points the adress of the
* the environment variable.
* @vars: vars variable of struct vars_t
* @str: name of env var to find.
* Return: always return void.
*/
void chdir_to_env(vars_t *vars, char *str)
{
int len, index;
len = _strlen(str);
index = find_env_index(*vars, str);
chdir((vars->env[index]) + len + 1);
}