forked from denistillwaters/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalize.c
More file actions
107 lines (91 loc) · 1.7 KB
/
analize.c
File metadata and controls
107 lines (91 loc) · 1.7 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
#include "shell.h"
/**
* analyze - This function runs thorough checks on
* command input on the buffer
* @buf: repent the buffer
* @count: count inputs
* @av: argument vector
*/
void analyze(char *buf, int count, char **av)
{
char *args[MAX];
char *token;
int i = 0;
token = strtok(buf, " ");
while (token != NULL && i < MAX - 1)
{
args[i] = token;
token = strtok(NULL, " ");
++i;
}
args[i] = NULL;
if (_strcmp(EXXIT, args[0]) == 0)
{
exxit(buf, args, count, av);
}
else if (access(args[0], X_OK) == 0)
{
execute(args[0], args);
}
else
getpath(args, count, av);
}
/**
* getpath - This function searches if a command exist
* and gets the path
* @args: arguments to check
* @count: count commands
* @av: argument vector
*/
void getpath(char **args, int count, char **av)
{
char *path = _getenv("PATH"), *token;
char *paths;
int found = 0;
if (path == NULL)
{
exit(EXIT_FAILURE);
}
removechar(path, '\n');
token = strtok(path, ":");
while (token != NULL)
{
paths = path_concat(token, args[0]);
if (access(paths, X_OK) == 0)
{
found = 1;
execute(paths, args);
free(paths);
break;
}
free(paths);
token = strtok(NULL, ":");
}
free(path);
if (found == 0)
{
error_message(args, count, av);
}
}
/**
* path_concat - This function concats token and a path
* @token: path
* @arg: argumeent passed
* Return: pointer to str
*/
char *path_concat(char *token, char *arg)
{
char *ptr;
int len1, len2;
if (!token || !arg)
return (NULL);
len1 = _strlen(token);
len2 = _strlen(arg);
ptr = malloc(sizeof(char) * len1 + 2 + len2);
if (!ptr)
return (NULL);
_strcpy(ptr, token);
_strcat(ptr, "/");
_strcat(ptr, arg);
return (ptr);
}