-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
43 lines (37 loc) · 846 Bytes
/
parse.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
#include "main.h"
/**
* parse_line - Divise la ligne de commande en tokens.
* @line: La ligne de commande entrée par l'utilisateur.
* Return: Un tableau de chaînes de caractères,
* chaque élément étant un token.
*/
char **parse_line(char *line)
{
int bufsize = 64, position = 0;
char **tokens = malloc(bufsize * sizeof(char *));
char *token;
if (!tokens)
{
fprintf(stderr, "hsh: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, " \t\r\n\a");
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= bufsize)
{
bufsize += 64;
tokens = realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
{
fprintf(stderr, "hsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, " \t\r\n\a");
}
tokens[position] = NULL;
return (tokens);
}