Thread: subtle issue that i cant find

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    subtle issue that i cant find

    I need to make an argument vector to pass to execv()... I'm creating it like shown below, I've tested it so I know all my strings are getting null termininated etc... but any programs I execute with execv() complain they aren't getting their arguments.

    Can anyone find the subtle thing wrong here?

    Code:
    	 
    	 char* argv[MAX_ARGS] = { 0 }; // need this to pass to execv()
             string argument; 
    	 stringstream ss;
    	 ss << input;
    	 ss >> command;
    	 int i = 0; 
    	 if ( command.size() != input.size() )  // were there arguments?
    	 {
    		 arguments = input.substr(command.size()+1);  // make an argument string.
    		 while ( (ss >> argument) && (i < (MAX_ARGS-1)) ) // create argument vector.
    		 { 
    			 argv[i] = new char[argument.size()+1];    // allocate memory
    			 memset(argv[i], '\0', argument.size()+1); // null it out
    			 argument.copy(argv[i], argument.size());  // copy argument to argv
    			 i++;
    		 }
    		 argv[i] = '\0'; // make last element null
    	 }
    	 /* child */
    	execv((path+command).c_str(), argv);
    Last edited by keira; 02-27-2008 at 08:02 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything obviously wrong - have you tried making a loop to print your "argv" array when you have parsed the arguments?

    Minor nits:
    You probably want:
    Code:
    		 argv[i] = NULL; // make last element null
    instead of:
    Code:
    		 argv[i] = '\0'; // make last element null
    Code:
    			 memset(argv[i], '\0', argument.size()+1); // null it out
    			 argument.copy(argv[i], argument.size());  // copy argument to argv
    Setting the entire array to zero, then overwriting all but one byte with a string is a bit excessive. Perhaps this would work better:
    Code:
    			 argument.copy(argv[i], argument.size());  // copy argument to argv
    			 argv[i][argument.size()+1] = '\0';
    --
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    argv[0] should be a program name, are you setting it ok? I do not think so - you skippint the command
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    argv[i][argument.size()+1] = '\0';
    Oh no! Remove +1 here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    argv[0] should be a program name, are you setting it ok? I do not think so - you skippint the command
    Good point, so it's probably not missing ALL the arguments, it's just getting one less than it expects, and failing due to that...

    --
    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.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Yes! I love you guys... thanks. That HAS to be it.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    Oh no! Remove +1 here
    Yes of course.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Proxy/Stub issue
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2008, 02:40 AM
  3. ld.exe: cannot find -l-lstdc++
    By Tonto in forum Tech Board
    Replies: 3
    Last Post: 04-10-2007, 11:20 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM