Thread: parsing command line strings

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    oh, so than that's probably what's wrong. Well as far as what's going wrong, it just doesn't execute the command ls -l properly. It will execute ls by itself though. I mean since it compiles and runs fine there's really no errors or anything generated. Does the arguments array I pass have to be of the EXACT size of the number of arguments? Or can it have blank entries as long as its ended by a NULL value?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can have:
    Code:
    char *argv[100000] = { "ls" , "-l", NULL };
    As long as it's big enough for the actual number of arguments you are using, it doesn't matter. It is passed as the address of the "vector" to execvp(), and execvp() will read the vector to find how many arguments there really are.

    So do I get it right that you get "ls" to work, but if you do "ls -l" it does exactly the same as "ls"? In that case, my supposition is that argv[1] is not "-l", but NULL, and argv[0] contains the "-l" - since ls itself skips argv[0], it sees no arguments and just does normal "ls" instead of the long version you expect from "-l".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    Ok, after trying a couple things out I know what's wrong. If I make the array holding the arguments some generic size, say 20 (to account for any number of possible arguments) I run into problems, Even though I do null terminate it. But when I make the array a set size, say 3, and than test out writing "ls -l" it works. But the problem is now it no longer works for just "ls" or a command longer with more options. Is it supposed to matter if I pass the exec() command an array with empty values as long as it's null terminated (which doesn't seem to work)?
    Last edited by John_L; 05-27-2008 at 02:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  2. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  3. Parsing Strings
    By SubLogic in forum C++ Programming
    Replies: 15
    Last Post: 01-07-2003, 11:11 AM
  4. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 10:18 AM
  5. parsing delimited strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 12:57 PM