Thread: argv shell execvp

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Cool argv shell execvp

    I'm coding a shell. . .
    I have a
    Code:
    char *newargv[MAX];
    I am able to get the newargv array to point to words when inputted in the program. For instance a user inputting "echo something 2 3" would result in newargv[0] pointing to the e of the string, newargv[1] pointing to s of the string, etc. . .

    This is a problem since when I fork and then use execvp
    Code:
    execvp(newargv[0], newargv];
    execvp is not just getting the word echo but the whole string "echo something 2 3" for example. . . any ways to get just the first word? something simple and clean also I'll probably need to get the other words separately too later on but not that far ahead to really know.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could post your real code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Quote Originally Posted by Salem View Post
    Well you could post your real code.
    I'd rather not post the exact code but I will describe it.

    Code:
    char stream[STORAGE]; 
    
    for(;;) {                                   /* repeat until done         */
    	(void) printf("p2: ");                  /* display a prompt          */
    	parse(stream);                          /* parse the line            */
    	
    	if(fork() == 0) {
    	    
    	    execvp(newargv[0], newargv);
    	    perror("exec");
    	}
    
    	break;
        }
    the parse() function will delimites words and single metacharacters with a null. . . "hello there" will turn into "hellow\0there" and point each newargv accordingly so newargv[0] points to the h in this example. . . i know this part works but i keep getting an error on execvp "no file found . . . " even when the first word in the stream is a legit command like "echo something something"
    Last edited by yacek; 02-25-2013 at 03:38 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe you should do this until you get it right.
    Code:
    char stream[STORAGE];
     
    for(;;) {                                   /* repeat until done         */
        (void) printf("p2: ");                  /* display a prompt          */
        parse(stream);                          /* parse the line            */
      
        for ( int i = 0 ; newargv[i] != NULL ; i++ ) {
            printf("Arg %d = >>%s<<\n", i, newargv[i] );
        }
    //    if(fork() == 0) {
    //         
    //        execvp(newargv[0], newargv);
    //        perror("exec");
    //    }
     
        break;
        }
    We can't tell you how your parser is broken without you posting it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execvp help
    By hambergler in forum C Programming
    Replies: 16
    Last Post: 06-02-2010, 09:06 PM
  2. Using execvp
    By kotoko in forum C Programming
    Replies: 21
    Last Post: 01-06-2009, 02:05 PM
  3. some help regarding execvp
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 04-27-2008, 09:49 PM
  4. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 AM
  5. execvp not working right
    By Nessarose in forum C Programming
    Replies: 5
    Last Post: 07-18-2005, 05:51 AM