-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokner.c
More file actions
48 lines (47 loc) · 793 Bytes
/
Copy pathtokner.c
File metadata and controls
48 lines (47 loc) · 793 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
#include "myshell.h"
/**
* tokener - Tokenizes dtring.
* @input: string to be tokenized.
* Return: arrays of strings.
*/
char **tokener(char *input)
{
char *delim = " \t\n", *token = NULL, *input_cpy;
int token_count = 0, n = 0;
char **cmd = NULL;
if (input == NULL)
return (NULL);
token_count = tokener1(input);
if (token_count == -1)
{
free(input);
input = NULL;
return (NULL);
}
input_cpy = stringdup(input);
if (input_cpy == NULL)
{
free(input);
input = NULL;
return (NULL);
}
cmd = malloc(sizeof(char *) * (token_count + 1));
if (cmd == NULL)
{
free(input);
return (NULL);
}
token = strtok(input_cpy, delim);
while (token)
{
cmd[n] = stringdup(token);
token = strtok(NULL, delim);
n++;
}
cmd[n] = NULL;
free(input);
input = NULL;
free(input_cpy);
input_cpy = NULL;
return (cmd);
}