-
Notifications
You must be signed in to change notification settings - Fork 0
/
built-in.c
205 lines (191 loc) · 3.45 KB
/
built-in.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
#include "shell.h"
/**
* built_in - check cmd structure and prints their system calls
* @argv: Array of arguments entered includes its name
* @cmd: pointer of structure
*/
void built_in(char **argv, s_global *cmd)
{
char buff[1024] = "/bin/";
int i = 0, j = 0, flag = 0;
int var_acc = 0;
while (*(cmd->token[0] + i))
{
if (*(cmd->token[0] + i) == buff[i])
j++;
if (j == 5)
break;
i++;
}
var_acc = access(cmd->token[0], X_OK);
if (var_acc == -1)
{
strcat(buff, cmd->token[0]);
var_acc = access(buff, X_OK);
if (var_acc != -1)
{
j = 5;
flag = 1;
}
}
else
{
flag = 1;
j = 0;
}
if (flag == 1)
function_fork(cmd, buff, j);
else
printf("%s: No such file or directory\n", argv[0]);
}
/**
* my_getline - Reads and store the input console
* @buff: saves a single line of the input
* @len: size of the total input
*
* Return: count:size of buffer, var_flag_0:if EOF or 1, EOF:CRTL+D
*/
int my_getline(char **buff, int *len)
{
char *tmp_buff = NULL;
static char *save = NULL, flag = '0';
static int count;
int var_flag_0 = 0, var_flag_1 = 0;
if (flag == '0')
{
save = malloc(sizeof(char) * 1024);
if (save == NULL)
return (EOF);
*len = read(STDIN_FILENO, save, 1024);
var_flag_0 = flag_0(save, len);
if (var_flag_0 == EOF || var_flag_0 == 1)
{
flag = '0';
count = 0;
return (var_flag_0);
}
count = var_flag_0;
*buff = malloc(sizeof(char) * ((*len) + 1));
if (*buff == NULL)
{
free(save);
return (EOF);
}
}
if (flag == '1')
{
if (save[count] == 32)
{
while (save[count] == 32)
count++;
}
}
flag = '1', tmp_buff = *buff;
var_flag_1 = flag_1(tmp_buff, save, count, len);
if (var_flag_1 == 0)
flag = '0';
count = var_flag_1;
return (count);
}
/**
* flag_1 - checks if there is more than 1 buffer
* @tmp_buff: pointer of the buffer
* @save: saves the entire input
* @count: static variable counter
* @len: size of the total input
*
* Return: count:size of buffer
*/
int flag_1(char *tmp_buff, char *save, int count, int *len)
{
while (save[count] != '\0')
{
*tmp_buff = save[count];
if (save[count] == '\n')
{
save[count] = '\0';
*tmp_buff = save[count];
tmp_buff++;
count++;
break;
}
tmp_buff++;
count++;
}
*tmp_buff = '\0';
if (count >= *len)
{
free(save);
count = 0;
}
return (count);
}
/**
* flag_0 - checks if there are errors on the first buffer
* @save: saves the entire input
* @len: size of the total input
*
* Return: count:size of buffer, 1:if len=1 or if there are spaces, EOF:CRTL+D
*/
int flag_0(char *save, int *len)
{
int count = 0;
if (*len <= 0)
{
free(save);
count = 0;
return (EOF);
}
if (*len == 1)
{
free(save);
count = 0;
return (1);
}
if (save[count] == 32)
{
while (save[count] == 32)
{
count++;
if (count + 1 == *len)
{
free(save);
count = 0;
*len = 1;
return (1);
}
}
}
return (count);
}
/**
* _strtok - Divides the buffer in tokens when it finds spaces
* @buff: saves a single line of the input
*
* Return: the token as string or NULL if the string is empty
*/
char *_strtok(char *buff)
{
int i = 0;
char *set = NULL, *string = NULL;
static char *save;
buff = (buff) ? buff : save;
set = buff;
string = set;
while (*string == ' ')
{
string++;
set++;
}
while (*set != ' ' && *set != '\0' && *set != '\n')
{
set++;
i++;
}
*set = '\0';
set++;
save = set;
if (*string == '\0')
return (NULL);
return (string);
}