-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_line.c
More file actions
41 lines (37 loc) · 704 Bytes
/
split_line.c
File metadata and controls
41 lines (37 loc) · 704 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
#include "shell.h"
char **split_line(char *line)
{
int bufsize = 64;
int i = 0;
char **tokens = malloc(bufsize * sizeof(char *));
char *token;
if (!tokens)
{
fprintf(stderr, "allocation error in split_line: tokens\n");
exit(EXIT_FAILURE);
}
token = strtok(line, TOK_DELIM);
while (token != NULL)
{
// handle comments
if (token[0] == '#')
{
break;
}
tokens[i] = token;
i++;
if (i >= bufsize)
{
bufsize += bufsize;
tokens = realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
{
fprintf(stderr, "reallocation error in split_line: tokens");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, TOK_DELIM);
}
tokens[i] = NULL;
return (tokens);
}