-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* get_environ - returns the string array copy of our environ | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
char **get_environ(info_t *info) | ||
{ | ||
if (!info->environ || info->env_changed) | ||
{ | ||
info->environ = list_to_strings(info->env); | ||
info->env_changed = 0; | ||
} | ||
|
||
return (info->environ); | ||
} | ||
|
||
/** | ||
* _unsetenv - Remove an environment variable | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: 1 on delete, 0 otherwise | ||
* @var: the string env var property | ||
*/ | ||
int _unsetenv(info_t *info, char *var) | ||
{ | ||
list_t *node = info->env; | ||
size_t i = 0; | ||
char *p; | ||
|
||
if (!node || !var) | ||
return (0); | ||
|
||
while (node) | ||
{ | ||
p = starts_with(node->str, var); | ||
if (p && *p == '=') | ||
{ | ||
info->env_changed = delete_node_at_index(&(info->env), i); | ||
i = 0; | ||
node = info->env; | ||
continue; | ||
} | ||
node = node->next; | ||
i++; | ||
} | ||
return (info->env_changed); | ||
} | ||
|
||
/** | ||
* _setenv - Initialize a new environment variable, | ||
* or modify an existing one | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* @var: the string env var property | ||
* @value: the string env var value | ||
* Return: Always 0 | ||
*/ | ||
int _setenv(info_t *info, char *var, char *value) | ||
{ | ||
char *buf = NULL; | ||
list_t *node; | ||
char *p; | ||
|
||
if (!var || !value) | ||
return (0); | ||
|
||
buf = malloc(_strlen(var) + _strlen(value) + 2); | ||
if (!buf) | ||
return (1); | ||
_strcpy(buf, var); | ||
_strcat(buf, "="); | ||
_strcat(buf, value); | ||
node = info->env; | ||
while (node) | ||
{ | ||
p = starts_with(node->str, var); | ||
if (p && *p == '=') | ||
{ | ||
free(node->str); | ||
node->str = buf; | ||
info->env_changed = 1; | ||
return (0); | ||
} | ||
node = node->next; | ||
} | ||
add_node_end(&(info->env), buf, 0); | ||
free(buf); | ||
info->env_changed = 1; | ||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* clear_info - initializes info_t struct | ||
* @info: struct address | ||
*/ | ||
void clear_info(info_t *info) | ||
{ | ||
info->arg = NULL; | ||
info->argv = NULL; | ||
info->path = NULL; | ||
info->argc = 0; | ||
} | ||
|
||
/** | ||
* set_info - initializes info_t struct | ||
* @info: struct address | ||
* @av: argument vector | ||
*/ | ||
void set_info(info_t *info, char **av) | ||
{ | ||
int i = 0; | ||
|
||
info->fname = av[0]; | ||
if (info->arg) | ||
{ | ||
info->argv = strtow(info->arg, " \t"); | ||
if (!info->argv) | ||
{ | ||
|
||
info->argv = malloc(sizeof(char *) * 2); | ||
if (info->argv) | ||
{ | ||
info->argv[0] = _strdup(info->arg); | ||
info->argv[1] = NULL; | ||
} | ||
} | ||
for (i = 0; info->argv && info->argv[i]; i++) | ||
; | ||
info->argc = i; | ||
|
||
replace_alias(info); | ||
replace_vars(info); | ||
} | ||
} | ||
|
||
/** | ||
* free_info - frees info_t struct fields | ||
* @info: struct address | ||
* @all: true if freeing all fields | ||
*/ | ||
void free_info(info_t *info, int all) | ||
{ | ||
ffree(info->argv); | ||
info->argv = NULL; | ||
info->path = NULL; | ||
if (all) | ||
{ | ||
if (!info->cmd_buf) | ||
free(info->arg); | ||
if (info->env) | ||
free_list(&(info->env)); | ||
if (info->history) | ||
free_list(&(info->history)); | ||
if (info->alias) | ||
free_list(&(info->alias)); | ||
ffree(info->environ); | ||
info->environ = NULL; | ||
bfree((void **)info->cmd_buf); | ||
if (info->readfd > 2) | ||
close(info->readfd); | ||
_putchar(BUF_FLUSH); | ||
} | ||
|
||
} |