-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_line.c
More file actions
52 lines (45 loc) · 881 Bytes
/
Copy pathparse_line.c
File metadata and controls
52 lines (45 loc) · 881 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
52
#include "shell.h"
/**
* parse_line - Splits a line into tokens (arguments)
* @line: Input line to parse
*
* Return: Array of strings (tokens), or NULL on failure
*/
char **parse_line(char *line)
{
char **tokens;
char *token;
int i = 0, bufsize = 64;
tokens = malloc(sizeof(char *) * bufsize);
if (tokens == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
token = strtok(line, " \t\r\n");
while (token != NULL)
{
tokens[i++] = token;
if (i >= bufsize)
{
bufsize += 64;
tokens = realloc(tokens, sizeof(char *) * bufsize);
if (tokens == NULL)
{
perror("realloc");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, " \t\r\n");
}
tokens[i] = NULL;
return (tokens);
}
/**
* free_tokens - Frees memory allocated for tokens array
* @tokens: Null-terminated array of strings
*/
void free_tokens(char **tokens)
{
free(tokens);
}