-
Notifications
You must be signed in to change notification settings - Fork 1
/
getSemiColon.c
45 lines (42 loc) · 945 Bytes
/
getSemiColon.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
#include "shell.h"
/**
* getSemiColon - a function to check if the input is empty or not.
* @strRead: the input string we read.
* Return: NULL if not success, array if success.
*/
char **getSemiColon(char *strRead)
{
char **argv = NULL, *token, *tmp, *tmp_token;
int size = 0, i;
if (strRead[0] == ' ' && strRead[_strlen(strRead)] == ' ')
exit(0);
if (strRead == NULL)
return (NULL);
tmp = _strdup(strRead);
if (tmp == NULL)
return (NULL);
tmp_token = strtokMod(tmp, ";&");
while (tmp_token != NULL)
{
size++;
tmp_token = strtokMod(NULL, ";&");
}
free(tmp), size++;
argv = (char **) malloc(sizeof(char *) * size);
if (argv == NULL)
return (NULL);
for (i = 0; i < size - 1; i++)
{
token = strtokMod((i ? NULL : strRead), ";&");
argv[i] = _strdup(token);
if (argv[i] == NULL)
{
for (i--; i >= 0; i--)
free(argv[i]);
free(argv);
return (NULL);
}
}
argv[size - 1] = NULL;
return (argv);
}