-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseperate_input.c
46 lines (42 loc) · 995 Bytes
/
seperate_input.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
#include "main.h"
/**
* seperate_input - Separate a command string
* into an array of tokens.
* @command: The input command string to tokenize.
*
* Return: An array of tokens (strings) if
* successful, or NULL on failure.
*/
char **seperate_input(char *command) {
char **tokens = NULL;
char *token;
int i = 0, j;
int token_count = 5;
token = strtok(command, " ");
tokens = malloc(token_count * sizeof(char *));
if (tokens == NULL) {
free(command);
command = NULL;
exit(EXIT_FAILURE);
}
while (token != NULL) {
if (i >= token_count - 1) {
token_count *= 2;
tokens = realloc(tokens, token_count * sizeof(char *));
if (tokens == NULL)
exit(EXIT_FAILURE);
}
tokens[i] = token;
if (tokens[i] == NULL) {
for (j = 0; j < 30; j++)
free(tokens[i]);
free(tokens);
tokens = NULL;
exit(EXIT_FAILURE);
}
i++;
token = strtok(NULL, " ");
}
tokens[i] = NULL;
return (tokens);
}