-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_env2.c
More file actions
68 lines (56 loc) · 1.15 KB
/
builtin_env2.c
File metadata and controls
68 lines (56 loc) · 1.15 KB
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
#include "main.h"
/**
* _getenv - Searches env string pointed to by arg
* @cmd_info: shell data structure
* @path_prefix: The arg of the env
* Return: Pointer to the env arg
* NULL if env does not exist
*/
char *_getenv(CommandInfo *cmd_info, char *path_prefix)
{
char *env_value;
int len = _strlen(path_prefix);
char **env = cmd_info->env;
while (env != NULL)
{
if (_strncmp(path_prefix, *env, len) == 0)
{
env_value = *env + len;
return (env_value + 1);
}
env++;
}
return (NULL);
}
/**
* copy_info - copies info to create
* a new env or alias
* @name: name (env or alias)
* @value: value (env or alias)
* @new_var_len: allocation size
*
* Return: new env or alias.
*/
char *copy_info(char *name, char *value, size_t new_var_len)
{
char *new;
new = malloc(sizeof(char) * new_var_len);
if (!new)
return (NULL);
_strcpy(new, name);
_strcat(new, "=");
_strcat(new, value);
return (new);
}
/**
* free_env - Frees the the environment copy.
* @cmd_info: argument vector
* Return: void
*/
void free_env(CommandInfo *cmd_info)
{
int i;
for (i = 0; cmd_info->env[i]; i++)
free(cmd_info->env[i]);
free(cmd_info->env);
}