-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenviron.c
More file actions
169 lines (153 loc) · 3.06 KB
/
environ.c
File metadata and controls
169 lines (153 loc) · 3.06 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "shell.h"
/**
* _env - Print the environment
* @cmd: The command info
*
* Return: 0 on success, -1 on failure
*
*/
int _env(UCommand *cmd)
{
char *variable = NULL, *env_var = NULL, *env_val = NULL;
char *delim = "=", *token = NULL;
ST_strc *symtab = symtab_stack.global_symtab;
ST_entry *entry = symtab->first;
if (cmd->ac == 1)
while (entry)
{
print(entry->name, STDOUT_FILENO);
print("=", STDOUT_FILENO);
print(entry->val, STDOUT_FILENO);
print("\n", STDOUT_FILENO);
entry = entry->next;
}
else
{
variable = strdup(cmd->av[1]);
if (variable == NULL)
{
perror("could not get environment variable");
free(variable);
return (-1);
}
token = strtok(variable, delim);
env_var = strdup(token);
token = strtok(NULL, delim);
env_val = strdup(token);
entry = get_symtab_entry(env_var);
if (entry == NULL)
entry = add_to_symtab(env_var);
symtab_entry_setval(entry, env_val);
free(variable);
free(env_var);
free(env_val);
}
return (0);
}
/**
* interactive - Check if the shell is interactive
* @cmd: The command info
*
* Return: 1 if interactive, 0 if not
*/
int interactive(UCommand *cmd)
{
return ((isatty(STDIN_FILENO) && (cmd->fdd <= 2)));
}
/**
* set_env - Set a new environment variable or update an existing one
* @cmd: Command
*
* Return: 0 on success, -1 on failure
*/
int set_env(UCommand *cmd)
{
char *env_var = NULL;
char *env_val = NULL;
ST_entry *entry = NULL;
if (cmd->ac == 3)
{
env_var = strdup(cmd->av[1]);
env_val = strdup(cmd->av[2]);
if (env_var == NULL)
{
perror("could not get environment variable");
free(env_val);
free(env_var);
return (-1);
}
if (env_val == NULL)
{
perror("could not set environment variable");
free(env_var);
free(env_val);
return (-1);
}
entry = get_symtab_entry(env_var);
if (entry == NULL)
entry = add_to_symtab(env_var);
symtab_entry_setval(entry, env_val);
free(env_var);
free(env_val);
}
else
{
print("Incorrect number of arguements\n", STDERR_FILENO);
return (1);
}
return (0);
}
/**
* unset_env - Removes a variable from path
* @cmd: Command
*
* Return: 0 on success, -1 from path
*/
int unset_env(UCommand *cmd)
{
char *env_var = NULL;
ST_entry *entry = NULL;
int i;
if (cmd->ac < 2)
{
print("Incorrect number of arguements\n", STDERR_FILENO);
return (1);
}
for (i = 1; i != cmd->ac; i++)
{
env_var = strdup(cmd->av[i]);
entry = do_lookup(env_var, symtab_stack.global_symtab);
if (env_var == NULL)
{
perror("could not get environment variable");
free(env_var);
return (-1);
}
if (entry == NULL)
{
perror("could remove environment variable");
free(env_var);
return (-1);
}
rem_from_symtab(entry, symtab_stack.global_symtab);
free(env_var);
}
return (0);
}
/**
* get_symtab_path - Get the val from the symtab
*
* Return: NULL on error or the path variable
*/
char *get_symtab_path(void)
{
ST_entry *symtab;
char *path;
symtab = get_symtab_entry("PATH");
if (symtab != NULL)
{
path = strdup(symtab->val);
return (path);
}
return (NULL);
}