ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write a shell terminal in c and I have a simple problem here.
Assume I have an array of char like char array[500]="ls -l -Q";
I need to parse these commands to a pointer array like **args
So I need to know how many arguments there are in the array to malloc the args. First I thought that I may use the space character but the form may not always be like "ls -l -Q", there may be many space characters between them.
Is there a simple way of doing this? If not I will try to reform the array, adjusting the number of space characters to 1 between strings.
Granted, it's not directly a part of your original question, but the idea did occur to me: what happens when a command allows arguments to be lumped together? Example: tar zcvf archive.tgz directory/
taylor_venable makes a good point. I think, after seeing that the man page for strtok() says "never use this function", the definitive source for command line argument parsing woudl be the source code for getopt().
int getopt(int argc, char * const argv[],
const char *optstring);
I think getopt is for the application to process args after they have been nicely split up by the shell into the argv array.
Since the OP is writing a shell this function is not really applicable. strtok() could work if you only want to split based on spaces, as soon as you want the more complicated stuff like quotes, escaping backslashes and stuff it's completely useless.
I imagine this is being written for learning purposes, so a function that parses the line for you maybe isn't what's needed here (I'm sure you can find one on the 'net). I'd use a simple for loop and have a switch statement for the "interesting" characters (ie space, ', ", \, |, >, etc). I wonder whether it would be better to count the tokens first or resize the array as you go along?
this sort of thing I would bin scanf and use fgets to grab the whole line then
parse it after
do man strtok you'll find there are a whole load of string splitting functions
to use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.