-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.c
More file actions
45 lines (40 loc) · 857 Bytes
/
split.c
File metadata and controls
45 lines (40 loc) · 857 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
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<linux/limits.h>
#include<signal.h>
#include<errno.h>
#include "mylib.h"
char** split(char *commandInput, char *delims)
{
const char *delim = delims;
int numOfTokens = 64, position = 0, size = 64;
char **tokens = malloc(numOfTokens*sizeof(char*));
char *token;
if(!tokens)
{
fprintf(stderr, "bash: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(commandInput, delim);
while(token!=NULL)
{
tokens[position] = token;
position++;
if(position>=numOfTokens)
{
numOfTokens += size;
tokens = realloc(tokens, numOfTokens*sizeof(char*));
if(!tokens)
{
fprintf(stderr, "bash: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, delim);
}
tokens[position] = NULL;
return tokens;
}