C split array of strings to subarrays -
i doing shell (in c) school assignment , have problem: have read input , have array of words. (like this: {"/bin/ls", "-l", ">", "file"}) , want have subarrays words between special symbols '<', '>', '|'. if input is
/bin/ls -l > f.txt > /usr/bin/wc
i want have:
{{"bin/ls", "-l"}, {"f.txt"}, {"usr/bin/wc"}}
so can call execv right arguments.
currently have char***, hardly allocate 3 cycles , copy char** char*** not simple algorithms.
is there simple way of splitting array of string array of arrays of strings? (to me looks trivial task split array substrings, c makes pretty hard me)
also, know split input @ these special symbols , arrays between them, i'm kind of interested how can done splitting array.
in c, array nothing more contiguous chunk of memory. have handle bounds checks yourself, might that. in other words, if have char **words
words[0] points /bin/ls
, words[1] points -l
, words[2] points >
, etc., have want. in real code, use char ***commands
, dynamically resize depending on number of commands, simple case can do:
char **first_command, **second_command, **third_command; first_command = words; second_command = words + 3; third_command = words + 5;
now first_command
points first element of array {"/bin/ls", "-l"}
(and hence points array), second_command
points first element of array {"f.txt"}
, etc. note in setup first_command[2]
out-of-array reference, need keep track of bounds. in other words, don't go copying words around, keep track of are.
Comments
Post a Comment