-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.c
More file actions
189 lines (173 loc) · 3.33 KB
/
Copy pathprocess.c
File metadata and controls
189 lines (173 loc) · 3.33 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "main.h"
static char **words;
static int wordCount;
/**
* exe_cmd - function that execute command
* @args: array of arguments for the command
* Return: 0 on sucess, -1 on failure
*/
int exe_cmd(char **args)
{
int rtVal = 0;
if (_strcmp(args[0], "unsetenv") == 0 && args[1] != NULL)
{
rtVal = _unsetenv(args[1]);
}
else if (_strcmp(args[0], "cd") == 0)
{
chgdir(args[1]);
}
else if (_strcmp(args[0], "exit") == 0)
{
exit_status(args[1]);
}
else if (_strcmp(args[0], "setenv") == 0 && args[1] != NULL
&& args[2] != NULL)
{
rtVal = _setenv(args[1], args[2]);
} else
{
args[0] = get_path(args[0]);
rtVal = exe_bin(args);
}
return (rtVal);
}
/**
* exit_status - Exit function with exit status
* @input: exit status code/number
*/
void exit_status(const char *input)
{
if (input != NULL)
{
int status = _atoi(input);
clear_env();
if (words != NULL)
freeWords(&words, wordCount);
if (status == 0)
{
errno = 2;
perror("./hsh");
exit(2);
}
exit(status);
} else
{
clear_env();
if (words != NULL)
freeWords(&words, wordCount);
exit(0);
}
}
/**
* main - function that prompts the user for input
* Return: 0 on success and -1 on failure
*/
int main(void)
{
char *line = NULL, *delim = " \n";
size_t line_size = 0;
int rtVal = -1, getstatus = -1;
int test = isatty(STDIN_FILENO);
if (test == 1)
output("($) ");
while (getline(&line, &line_size, stdin) != -1)
{
getstatus = 0;
if (is_empty(line) != 0)
{
seperate_word(line, &words, delim);
free(line);
line = NULL;
rtVal = exe_cmd(words);
if (words != NULL)
freeWords(&words, wordCount);
} else if (line != NULL && is_empty(line))
{
output("\n");
free(line);
}
if (test == 1)
output("($) ");
getstatus = -1;
}
if (line)
free(line);
if (getstatus == -1)
{
write(STDIN_FILENO, "\n", 2);
exit(0);
}
return (rtVal);
}
/**
* seperate_word - Function that seperates the words
* in a string into a NULL terminated array of words
* @line: The line of string to be operated
* @words: Array to store the words
* @delim: Delimiter to be used for tokenization
* Return: On success number of words tokenized, -1 on failure
*/
int seperate_word(char *line,
char ***words, char *delim)
{
int i = 0;
char *token = NULL;
if (line != NULL)
{
wordCount = count_words(line);
*words = (char **)malloc(sizeof(char *) * (wordCount + 1));
if (*words != NULL)
{
token = _strtok(line, delim);
i = 0;
while (token != NULL)
{
(*words)[i] = _strdup(token);
token = _strtok(NULL, delim);
if (token != NULL)
{
if (token[0] == '#')
token = NULL;
}
i++;
}
(*words)[i] = NULL;
}
}
return (wordCount);
}
/**
* _unsetenv - Function that unsets an enviroment variable
* @name: name of enviroment variable
* Return: 0 on success, -1 on failure
*/
int _unsetenv(const char *name)
{
char *env, *token;
int count = 0, test = -1;
while (environ[count])
{
env = _strdup(environ[count]);
token = _strtok(env, "=");
if (_strcmp(token, name) == 0)
{
free_node(name);
while (environ[count] != NULL)
{
environ[count] = environ[count + 1];
count++;
}
environ[count] = NULL;
free(env);
test = 0;
break;
}
count++;
free(env);
test = -1;
}
if (test == -1)
write(STDERR_FILENO, "Unsetenv failed\n", 17);
return (count);
}