-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilders.c
More file actions
100 lines (97 loc) · 2.19 KB
/
builders.c
File metadata and controls
100 lines (97 loc) · 2.19 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
#include "holberton.h"
/**
*build_argv - Build argv to send myexec
*@lineptr: Line with value stored the information
*Return: Array to pointers with Arguments
*/
char **build_argv(char *lineptr)
{
char **argv = NULL;
char len = 1;
lineptr = strtok(lineptr, " \t\n");
while (lineptr != NULL)
{
argv = realloc_pointer(argv, len - 1, len);
argv[len - 1] = lineptr;
lineptr = strtok(NULL, " \t\n");
len++;
}
argv = realloc_pointer(argv, len - 1, len);
if (argv == NULL)
return (NULL);
argv[len - 1] = NULL;
return (argv);
}
/**
*build_path - fin path to execute
*@c: quantity of commands executed
*@full_path: comand to execute
*@argv_0: the arguments
*@envp: the enviroment
*Return: Pointer with the value of full path
*/
int build_path(int c, char **full_path, char *argv_0, char *envp[])
{
char *token = NULL, *s = ":", *path = NULL, *aux;
struct stat st;
if (argv_0[0] == '/' || argv_0[0] == '.')
{
if (stat(argv_0, &st) == 0)
{ *full_path = str_concat("", argv_0);
return (0);
}
else
{ *full_path = str_concat("", "");
not_found_command(c, argv_0);
exit(127); }
}
else
{ aux = str_concat(get_value_env(envp, "PATH"), "");
aux = strtok(aux, " ");
if (aux == NULL)
{ not_found_path(c, argv_0);
exit(127);
}
else
{ token = strtok(aux, s);
while (token != NULL)
{ path = str_concat(token, "/");
*full_path = str_concat(path, argv_0);
free(path);
if (*full_path == NULL)
{ free(aux);
return (-1); }
if (stat(*full_path, &st) == 0)
{ free(aux);
return (1); }
free(*full_path);
token = strtok(NULL, s); }
free(aux);
*full_path = str_concat("", "");
not_found_command(c, argv_0); }
exit(127); }
}
/**
* getfunction- selects the correct function to perform the operation
* @built: Operator given
* Return: a pointer to the function that corresponds to the operator given
*/
void (*getfunction(char *built))(char **, char **, char *, int)
{
int i_struct = 0;
op_t print[] = {
{"exit", exit_handler},
{"env", env_handler},
{"help", help_handler},
{NULL, NULL}
};
while (print[i_struct].op != NULL)
{
if (_str_cmp(built, print[i_struct].op))
{
return (print[i_struct].f);
}
i_struct++;
}
return (NULL);
}