-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseline.c
53 lines (33 loc) · 893 Bytes
/
parseline.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
47
48
49
50
51
52
53
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "parseline.h"
#define BUF_SIZE 64
#define MAX_ARG_SIZE 1024
#define STR_DELIM " \t\r\n\a"
char **parse_line(char *line){
int pos = 0;
char **tokens = malloc(BUF_SIZE * sizeof(char *));
char *token = NULL;
if(!tokens){
perror("Error, alloncating memory");
exit(EXIT_FAILURE);
}
token = strtok(line, STR_DELIM);
while(token != NULL){
tokens[pos] = token;
pos += 1;
if(pos >= BUF_SIZE){
pos += BUF_SIZE;
tokens = realloc(tokens, BUF_SIZE * sizeof(char *));
if(!tokens){
perror("Error reallocating memory");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, STR_DELIM);
}
tokens[pos] = NULL;
return tokens;
}