-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.c
More file actions
44 lines (32 loc) · 782 Bytes
/
tokenize.c
File metadata and controls
44 lines (32 loc) · 782 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
#include "headers.h"
char *trim_string(char *str)
{
char *end;
while (isspace((unsigned char)*str))
str++;
if (*str == 0)
return str;
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end))
end--;
end[1] = '\0';
return str;
}
int tokenize_commands(char *cmds, char *delimiter, char **tokenized_cmds)
{
char *token = strtok(cmds, delimiter);
int count = 0;
while (token != NULL)
{
if (count >= BUFFER_SIZE)
{
fprintf(stderr, "Command limit exceeded\n");
return -1;
}
token = trim_string(token);
strcpy(tokenized_cmds[count], token);
token = strtok(NULL, delimiter);
++count;
}
return count;
}