-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.c
More file actions
51 lines (44 loc) · 889 Bytes
/
tokenize.c
File metadata and controls
51 lines (44 loc) · 889 Bytes
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
#include "shell.h"
/**
* tokenize - split the command and arguments
* into tokens and stores in a vector
* @command: string to tokenize
*
* Return: tokenized array of strings
*/
char **tokenize(char *command)
{
char *token = NULL, *temp = NULL,
*delim = " \t\n\a";
char **args = NULL;
int i = 0, counter = 0;
if (command == NULL)
return (NULL);
if (_strcmp(command, "\n") == 0)
{
free(command);
return (NULL);
}
temp = _strdup(command);
token = strtok(command, delim);
while (token)
{
token = strtok(NULL, delim);
counter++;
}
args = malloc(sizeof(char *) * (counter + 1));
if (args == NULL)
return (NULL);
token = strtok(temp, delim);
while (token)
{
args[i] = malloc(sizeof(char) * (_strlen(token) + 1));
_strcpy(args[i], token);
token = strtok(NULL, delim);
i++;
}
args[i] = NULL;
free(temp);
free(command);
return (args);
}