argv shell execvp

This is a discussion on argv shell execvp within the C Programming forums, part of the General Programming Boards category; I'm coding a shell. . . I have a Code: char *newargv[MAX]; I am able to get the newargv array ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 02:38 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21