-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.c
210 lines (185 loc) · 3.17 KB
/
shell.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "seashell.h"
/**
* get_filepath - Get file_path
*
* @command: Input in command line
*
* Return: Null
*/
char *get_filepath(char *command)
{
/*char *tmp = NULL;*/
dir_list_t *list = NULL;
dir_list_t *current = NULL;
char *a, *b, *PATH;
list = make_path_list();
current = list;
PATH = _getenv("PATH");
if (PATH)
{
a = _strstr(PATH, "::");
b = _strstr(PATH, ":/bin");
if (PATH[0] == ':' || PATH[_strlen(PATH) - 1] == ':' ||
(a && (a < b)))
{
if (!access(command, F_OK))
return (command);
}
}
while (current)
{
_strcpy(tmp, current->dir_path);
_strcat(tmp, "/");
_strcat(tmp, command);
if (!access(tmp, F_OK))
{
free_list(&list);
return (tmp);
}
current = current->next;
}
free_list(&list);
return (NULL);
}
/**
* word_count - Function to get the length of string
*
* @string: Input string being counted
* Return: Count of string
*/
int word_count(char *string)
{
int count = 0;
char *find_delim = string;
while (*find_delim)
{
if (*find_delim == ' ')
count++;
find_delim++;
}
count += 2;
return (count);
}
/**
* make_argv - Function that shows args using strtok
*
* @string: arguments displayed
* Return: Array of strings
*/
char **make_argv(char *string)
{
char *token = NULL;
int i, count = 0;
count = word_count(string);
array = malloc(sizeof(char *) * (count + 1));
array[count] = NULL;
i = 0;
token = strtok(string, " \t");
if (!token)
{
free(array);
return (NULL);
}
array[i++] = token;
while (token)
{
token = strtok(NULL, " \t");
array[i] = token;
i++;
}
token = array[0];
if (!is_a_path(token))
{
if (!_strcmp(token, "exit"))
{
free(array);
free(string);
exit(0);
}
array[0] = get_filepath(token);
if (!array[0])
{
free(array);
return (NULL);
}
}
return (array);
}
/**
* exec_command - Function that executes coomand
*
* @usr_input: Input string to be executed
* @shell_name: Name of executable
* Return: void
*/
void exec_command(char *usr_input, char *shell_name)
{
char **argv = NULL;
pid_t is_parent, j;
argv = make_argv(usr_input);
if (!argv)
{
_printf("Something is going terribly wrong.\n");
free(usr_input);
return;
}
is_parent = fork();
if (is_parent == 0)
{
j = execve(argv[0], argv, NULL);
if (j == -1)
{
_printf("%s: No such file or directory\n", shell_name);
free(argv);
free(usr_input);
return;
}
}
wait(NULL);
free(argv);
free(usr_input);
}
/**
* main - Function main
* @ac: Number of command line args
* @av: Array of command line args
*
* Return: void
*/
int main(__attribute__((unused)) int ac, char **av)
{
char *newline = NULL;
size_t bufsize = 1024;
ssize_t p;
while (1)
{
if (isatty(STDIN_FILENO))
_printf("$ ");
usr_input = malloc(1024);
if (!usr_input)
return (-1);
p = getline(&usr_input, &bufsize, stdin);
if (p == -1)
{
free(usr_input);
exit(0);
}
newline = _strchr(usr_input, '\n');
if (newline)
*newline = '\0';
if (is_empty(usr_input))
{
free(usr_input);
continue;
}
if (!_strcmp(usr_input, "env"))
{
env_builtin();
free(usr_input);
continue;
}
exec_command(usr_input, av[0]);
}
free(usr_input);
return (0);
}