-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
42 lines (38 loc) · 1.01 KB
/
parse.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
#include "npipe.h"
/*
*
* Tue Dec 20 23:47:01 EST 2011
* Satish Kumar Eerpini
*/
/*
* Returned the indices of the words in the given sentence
* with space as the default separator
*/
int * break_by_spaces(char * arg, int * num_args){
if(strlen(arg) <= 0 || (strlen(arg) ==1 && *arg == ' ')){
return NULL;
}
int * retval = malloc((strlen(arg) -1)*sizeof(int));
int * temp = retval;
int counter = 0;
if(retval == NULL){
perror("malloc");
exit(EXIT_FAILURE);
}
memset(retval, 0, (strlen(arg) -1)*sizeof(int));
*temp = counter;
*num_args = *num_args + 1;
arg++;
counter++;
while(*arg){
if(*arg == ' '){
*(++temp) = counter+1;
*arg = 0;
*num_args = *num_args + 1;
}
arg++;
counter++;
}
*(++temp) = -1;
return retval;
}