-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell_function_2.c
More file actions
149 lines (133 loc) · 2.87 KB
/
shell_function_2.c
File metadata and controls
149 lines (133 loc) · 2.87 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
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "main.h"
/**
* tokenize - Takes the input and tokenize command and arguments.
*
* @input: Input of the getline (with a command and parameters).
*
* Return: A structure with the tokens and a holder to free later.
*/
response *tokenize(char *input)
{
char **tokens = NULL, *token = NULL, *holder = NULL;
int spaces = total_malloc(input), i = 0;
response *res = NULL;
res = malloc(sizeof(response));
if (!res)
return (NULL);
tokens = malloc(sizeof(char *) * (spaces));
if (!tokens)
return (NULL);
holder = strdup(input);
token = strtok(holder, " \n\t");
free(input);
while (token != NULL)
{
tokens[i] = strdup(token);
token = strtok(NULL, " \n\t");
i++;
}
tokens[i] = NULL;
res->toks = tokens;
res->hold = holder;
return (res);
}
/**
* first_validations - Validations after the getline (all spaces or errors).
*
* @command: Command (input) of the getline.
*
* @bytes_read: Lenght of the command.
*
* @while_st: Pointer to the while status.
*
* Return: 0 if all good, 1 otherwise.
*/
int first_validations(char *command, int bytes_read, int *while_st)
{
if (bytes_read == EOF)
{
free(command);
return (-1);
}
if (all_spaces(command, bytes_read))
{
*while_st = 1;
free(command);
return (1);
}
return (0);
}
/**
* validate_last_access - Validate if the token[0] is a correct route or not
*
* @r: Pointer to an structure (tokens and holder).
*
* @file: Pointer to the name of the executable.
*
* @err: Pointer to the counter of errors ocurred on the shell.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: 0.
*/
void validate_last_access(response *r, char *file, int *err, int *exit_status)
{
if (r->hold == NULL)
{
fprintf(stderr, "%s: %d: %s :not found\n", file, *err,
clean_spaces(r->toks[0]));
free_tokens(r->toks);
free(r);
*err += 1;
*exit_status = 127;
}
if (access(r->hold, F_OK) != 0)
{
fprintf(stderr, "%s: %d: %s :not found\n", file, *err,
clean_spaces(r->hold));
*err += 1;
*exit_status = 127;
}
else
{
do_the_command(r, exit_status);
}
}
/**
* route_works - Validate if a route works or not.
*
* @obj: Pointer to an structure (tokens, holder).
*
* @while_status: Status of the infinite while.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: 0 if the route fails, 1 otherwise.
*/
int route_works(response *obj, int *while_status, int *exit_status)
{
if (access(obj->toks[0], F_OK) == 0)
{
do_the_command(obj, exit_status);
free_all(obj, while_status);
*while_status = 1;
return (1);
}
return (0);
}
/**
* free_all - Function that free all the malloc of the program.
*
* @obj: Pointer to a structure (tokens and holder)
*
* @while_status: Status of the infinite while..
*
* Return: Void.
*/
void free_all(response *obj, int *while_status)
{
free_tokens(obj->toks);
free(obj->hold);
free(obj);
*while_status = 1;
}