-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.c
42 lines (38 loc) · 819 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
#include "shell.h"
/**
* parse_command - A function to tokenize user inputs.
* @user_line: pointer to command-line arguments.
* Return: tokens.
*/
char **parse_command(char *user_line)
{
int bufsize = BUFFER_SIZE, i = 0;
char **tokens = malloc(bufsize * sizeof(char *));
char *token;
if (!tokens)
{
free(tokens);
write(STDERR_FILENO, "hsh: allocation error\n", 22);
exit(EXIT_FAILURE);
}
token = str_tok(user_line, DELIMITER);
while (token != NULL)
{
tokens[i] = token;
i++;
if (i >= bufsize)
{
bufsize += BUFFER_SIZE;
tokens = realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
{
free_all(tokens);
write(STDERR_FILENO, "hsh: allocation error\n", 22);
exit(EXIT_FAILURE);
}
}
token = str_tok(NULL, DELIMITER);
}
tokens[i] = NULL;
return (tokens);
}