Thread: Splitting a space delimited string

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Splitting a space delimited string

    Hey forum,

    Having a little trouble here with a C program I'm writing.

    I want to take a string as input, eg:
    ./my_program -arg1 -arg2 -arg3 ... -arg 29

    and pass it into an execlp function (with the arguments). I Need it to be hable to handle any amount of arguments.

    What i'm doing so far is this:
    Code:
    fgets(buffer, 2048, input);
            if(feof(input)) {
                //printf("we got an EOF on first line of block\n"); //DEBUG
                exit(error(1,line,""));
            }
            if(!sscanf(buffer, "%s", command)) {
                exit(error(1,line,""));
            }
    and then I fork, and run execlp in the child.

    I know that to get a string with space delimited arguments I would simply read the string at buffer[(int)strlen(command)+1] (+1 for the space after the command and before the first argument), but how do I make it so that exec will dynamically pass through any number of arguments needed.

    Is it easier to do this using execvp?

    Also as a side note, I know the first argument has to be the program name. This is fine if command = tee (for example), because i could pass command as the first argument as well, but what if the command i'm running is "/some/file/path/program"? How can I pass "program" as the first parameter?

    Thanks,
    Luke

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your command line arguments are already split up word by word for you, and are stored in argv, including 'program':
    Code:
    #include<stdio.h>
    int main( int argc, char **argv )
    {
        size_t x = 0;
        for( x = 0; x < argc; x++ )
        {
            printf( "\'%s\'\n", argc[ x ] );
        }
    
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    No I don't think you understand.

    I start my program, then my program asks for input

    This input will be a string containing a program to be run, including parameters.

    I then take this string, fork, and the child process execs the program that the user inputted.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read it all however you want, split it with something like strtok, or just call something like sscanf in a loop, until you run out of input, storing them all however you want, say in an array of pointers to characters, since that's what the function expects you to pass it. I'm not sure what you can't figure out how to do. Let's assume you don't know how to store them.
    Code:
    char **args = NULL;
    int argcount = 0;
    
    while input
        pop a word off into a buffer
        char **temp;
        temp = realloc( args, 1 + argcount * sizeof *temp )
        if( temp )
        {
            args = temp;
            args[ argcount ] = malloc strlen buf + 1 
            if( args[ argcount ] )
                strcpy( args[ argcount ], buffer )
            argcount++;
        }
    realloc again, adding room for a NULL ptr, or do it at the start, before the loop
    Something like that should be fine.

    Quzah.
    Last edited by quzah; 05-15-2010 at 02:32 AM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    strtok is exactly what I was looking for, thanks!

    What about my problem with the initial argument: the name of the program?

    It seems like such a common thing to want to find out, there must be a simple way of doing it?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strrchr .. assuming you're always going to have a /, find the last / in the first "word", and snip off everything to the right of it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  5. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM