-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParseCommand.c
49 lines (45 loc) · 940 Bytes
/
ParseCommand.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
#include "shell.h"
/**
* ParseCommand - take and evaluate the arguments
* @command: take a command
* @separator: take a blank space of arguments
* Return: Return a params
**/
char **ParseCommand(char *command, char *separator)
{
unsigned int i = 0, Qword = 0, len = 0;
char **param;
char *s;
len = _strlen(command);
for (i = 0; i < len; i++)
{
if (command[i] == *separator)
Qword++;
}
Qword != 1 ? Qword++ : (Qword = 1);
if ((len - 1) == 0)
{
free(command);
return (NULL);
}
param = malloc((Qword + 2) * sizeof(char **));
if (param == NULL)
{
return (NULL);
}
i = 0;
s = strtok(command, separator);
len = _strlen(s);
s[len - 1] == '\n' ? s[len - 1] = '\0' : (s[len - 1] = s[len - 1]);
param[i] = s;
i++;
while ((s = strtok(NULL, separator)))
{
param[i] = s;
len = _strlen(s);
s[len - 1] == '\n' ? s[len - 1] = '\0' : (s[len - 1] = s[len - 1]);
i++;
}
param[i] = NULL;
return (param);
}