Possible Duplicate:
Split string with delimiters in C
I am writing a program in Linux to simulate Shell. The basic idea is first use fgets to get one line the user input, which is supposed to be something like "ls -l"
, then I parse this string into "ls" and "-l" then construct a two dimensional array like char *array[] = {"ls","-l"},
then use the execvpe
which takes three arguments: the first is also from the input, that is "ls" in the example given above and the second would be array, the third would be a pointer like array in which I specify some directories.
The problem would be to parse string like "ls -l" into the two dimensional array. I intend to use malloc
create the array. Parsing string in C seems quite unhandy.
Is there any good way to parse string like "asd dg rg grhr"
into a array like {"asd","dg","rg","grhr"}
.
Also any advice concerning simulating shell in another way is appreciated.