-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmnd_handler.c
More file actions
136 lines (114 loc) · 2.67 KB
/
cmnd_handler.c
File metadata and controls
136 lines (114 loc) · 2.67 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
#include "shell.h"
/**
* input_parser - Builds an array of strings as the arguments
* @str_input: Command input given by the user
* @delimiter: String od chars indicating the delimiters
*
* Return: Array of strings
*/
char **input_parser(char *str_input, char *delimiter)
{
int i, args_count = 0;
char **args;
char *token, *tkn_ptr;
char *str_copy;
if (str_input == NULL)
dispatch_error("Error while parsing the command\n");
args_count = args_counter(str_input, delimiter);
args = allc_memry(sizeof(char *) * (args_count + 1));
str_copy = str_dup(str_input);
tkn_ptr = str_copy;
for (i = 0; i < args_count; i++)
{
token = strtok(tkn_ptr, delimiter);
if (token == NULL)
break;
tkn_ptr = NULL;
args[i] = str_dup(token);
}
args[i] = NULL;
free(str_copy);
return (args);
}
/**
* args_counter - Counts the number of arguments in a command string
* @str_input: Command as a string
* @delimiter: used to separate the arguments
*
* Return: Number of arguments present in str_input
*/
int args_counter(char *str_input, char *delimiter)
{
char *tkn, *tkn_ptr;
int count = 0;
char *str_copy = str_dup(str_input);
tkn_ptr = str_copy;
while ((tkn = strtok(tkn_ptr, delimiter)) != NULL)
{
count++;
tkn_ptr = NULL;
}
free(str_copy);
return (count);
}
/**
* PATH_Handler - Checks if the command to execute could
* be found in PATH's dirs
* @commands: Array of strings containing the command and args
* Return: flag 0 if is succes else -1 if error
*/
int PATH_Handler(char **commands)
{
char *path_dirs, *path;
char *tkn, *tkn_ptr;
char *str_copy;
int flag = 127;
if (
commands == NULL || commands[0] == NULL ||
commands[0][0] == '\0' || commands[0][0] == '/'
)
return (127);
if (access(commands[0], F_OK) == 0)
return (0);
path_dirs = env_get("PATH");
if (path_dirs == NULL)
return (127);
str_copy = str_dup(path_dirs);
tkn_ptr = str_copy;
while (1)
{
tkn = strtok(tkn_ptr, ":");
if (tkn == NULL)
break;
tkn_ptr = NULL;
path = path_finder(tkn, commands[0]);
if (access(path, F_OK) != -1)
{
free(commands[0]);
commands[0] = path;
flag = 0;
break;
}
free(path);
}
free(str_copy);
return (flag);
}
/**
* path_finder - Creates a string representing a full path to file
* @dir: String representing the directory path
* @filename: Name of the file we are looking for
*
* Return: String as a full path to "filename"
*/
char *path_finder(char *dir, char *filename)
{
int dir_len = strlen(dir);
int filename_len = strlen(filename);
char *path;
path = allc_memry(sizeof(char *) * (dir_len + filename_len + 2));
strcpy(path, dir);
strcat(path, "/");
strncat(path, filename, filename_len + 1);
return (path);
}