-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.c
44 lines (40 loc) · 912 Bytes
/
tokenize.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
#include "main.h"
/**
* tokenize - return the command line after split it to argv
* @line: the command line taken from the user
* Return: pointer to string to cmd line
*/
char **tokenize(char *line)
{
char *token, **argv, *line_cp = NULL, *delim = " \n";
int token_num = 0, i;
line_cp = malloc(sizeof(char) * (_strlen(line) + 1));
if (line_cp == NULL)
return (NULL);
_strcpy(line_cp, line);
token = strtok(line_cp, delim);
while (token != NULL)
{
token_num++;
token = strtok(NULL, delim);
}
token_num++;
argv = malloc(sizeof(char *) * token_num);
_strcpy(line_cp, line);
token = strtok(line_cp, delim);
for (i = 0; token != NULL; i++)
{
argv[i] = malloc(sizeof(char) * _strlen(token) + 1);
if (argv == NULL)
{
free(line_cp);
free(argv);
return (NULL);
}
_strcpy(argv[i], token);
token = strtok(NULL, delim);
}
argv[i] = NULL;
free(line_cp);
return (argv);
}