-
Notifications
You must be signed in to change notification settings - Fork 2
/
env2.c
135 lines (125 loc) · 2.75 KB
/
env2.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
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
#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);
}
/**
* print_prompt - prints PS1 if exists
* @info: info struct
* Return: void
*/
void print_prompt(info_t *info)
{
char hostname[WRITE_BUF_SIZE + 1];
char *_PS1 = NULL, *prompt = NULL;
prompt = _getenv(info, "PS1=");
if (!prompt)
_puts("$ ");
else
{
if (_strcmp(prompt, "\\!") == 0)
_PS1 = convert_number(info->histcount, 10, 0);
else if (_strcmp(prompt, "\\a") == 0)
_PS1 = "\a";
else if (_strcmp(prompt, "\\d") == 0)
_PS1 = create_date();
else if (_strcmp(prompt, "\\H") == 0)
{
if (gethostname(hostname, WRITE_BUF_SIZE) == 0)
_PS1 = hostname;
else
perror("gethostname");
}
else if (_strcmp(prompt, "\\n") == 0)
_PS1 = "\n";
else if (_strcmp(prompt, "\\s") == 0)
_PS1 = "-hsh";
else if (_strcmp(prompt, "\\u") == 0)
_PS1 = _getenv(info, "USER=");
else if (_strcmp(prompt, "\\w") == 0)
_PS1 = _getenv(info, "PWD=");
else
_puts(prompt);
_puts(_PS1);
}
}