-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
208 lines (197 loc) · 4.38 KB
/
search.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "main.h"
/**
* checkpoint - reads the array of strings and process it accordingly
* @arg: is the array of pointers composing the tokens
* @string: contains the raw user input
* @count: the number of argument which are processed until now
*
* Return: a char which is somehow the path if required
*/
char *checkpoint(char **arg, char *string, int count)
{
char *cmd = NULL;
int i, err_code = error_stat(-16);
if (_strcmp(arg[0], "exit") == 0)
exit_with_code(err_code, count, arg, string);
if (_strcmp(arg[0], "env") == 0)
{
error_stat(_env());
return (arg[0]);
}
if (_strcmp(arg[0], "setenv") == 0)
{
i = _setenv(arg, count), error_stat(i);
return (arg[0]);
}
if (_strcmp(arg[0], "unsetenv") == 0)
{
i = _unsetenv(arg, count), error_stat(i);
return (arg[0]);
}
if (_strcmp(arg[0], "cd") == 0)
{
error_stat(_cd(arg[1]));
return (arg[0]);
}
if (_strcmp(arg[0], "alias") == 0)
{
error_stat(_alias(arg, 0));
return (arg[0]);
} /* betty has an issue with number of lines */
if ((_strcmp(arg[0], "echo") == 0) && (arg[1][0] == '$'))
{
i = echo_dollar(arg), error_stat(i);
return (arg[0]);
}
cmd = _which(arg[0]);
return (cmd);
}
/**
* _which - looks for the requried path
* @arg: the argument which is the name of the executable
*
* Return: a string containing the fullpath, NULL on fsilure
*/
char *_which(char *arg)
{
const char *variableName = "PATH";
char *variableValue = NULL;
char *fullpath = NULL;
char *pathcopy = NULL, *token = NULL;
if (!(arg[0] == '/' || arg[0] == '.'))
{
fullpath = malloc(1024);
if (fullpath == NULL)
return (NULL);
fullpath[0] = '\0';
variableValue = getenv(variableName);
pathcopy = strdup(variableValue);
if (pathcopy == NULL)
return (NULL);
token = strtok(pathcopy, ":");
while (token != NULL)
{
fullpath = strcpy(fullpath, token);
fullpath = strcat(fullpath, "/");
fullpath = strcat(fullpath, arg);
if (access(fullpath, X_OK) == 0)
{
free(pathcopy);
return (fullpath);
}
token = strtok(NULL, ":");
}
free(fullpath);
free(pathcopy);
return (NULL);
}
else
{
if (access(arg, X_OK) == 0)
return (arg);
return (NULL);
}
}
/**
* is_builin - checks if a string matches one of the builin commands
* @cmd: is the name of the builtin command
*
* Return: Nothing.
*/
int is_builin(char *cmd)
{
if (_strcmp(cmd, "env") == 0)
return (0);
else if (_strcmp(cmd, "setenv") == 0)
return (0);
else if (_strcmp(cmd, "unsetenv") == 0)
return (0);
else if (_strcmp(cmd, "cd") == 0)
return (0);
else if (_strcmp(cmd, "alias") == 0)
return (0);
else if (_strcmp(cmd, "echo") == 0)
return (0);
else
return (-1);
}
/**
* semicolon_parser - prints a name as is
* @next_ptr: the moving piece across letters
*
* Return: Nothing.
*/
void semicolon_parser(char ***next_ptr)
{
int i, j; /*flag means that semicolon found*/
static char *next_token;
/*char *next_token;*/
if (next_ptr == NULL)
{
next_token = NULL;
return;
}
if (next_token)
**next_ptr = next_token;
for (i = 0; (*next_ptr)[i] != NULL; i++)
{
if (_strcmp((*next_ptr)[i], ";") == 0) /*semicolon found!*/
{
(*next_ptr)[i] = NULL;
*next_ptr = &((*next_ptr)[i + 1]);
return; /*returning a token, ready for the next*/
}
for (j = 0; (*next_ptr)[i][j] != '\0'; j++)
{
if ((*next_ptr)[i][j] == ';')
{
(*next_ptr)[i][j] = '\0';
next_token = &((*next_ptr)[i][j + 1]);
return;
}
}
}
*next_ptr = &((*next_ptr)[i]);
}
/**
* cmd_list - send the array of commands to be executed
* @array: is the raw array of tokens produced from input
*
* Return: the array only containing a one command with its arguments
*/
char **cmd_list(char **array)
{
static char **next_ptr, **org;
char **temp = NULL; /*a savepoint of where the cmd should start*/
if (array != org) /*a new input*/
{
if (_strcmp(array[0], ";") == 0) /*checking if correct syntax*/
{
perror("syntax error near unexpected token `;'");
return (NULL);
}
next_ptr = array;
org = array;
temp = array;
}
else
{
if (*next_ptr == NULL) /*no next command to be passed*/
{
org = NULL;
semicolon_parser(NULL);
return (NULL);
}
temp = next_ptr; /*preparing the next command*/
}
semicolon_parser(&next_ptr);
return (temp);
}