Web Design and Web Development Forum

  1. #1
    Join Date
    Apr 2005
    Posts
    8
    Rep Power
    0
  2. wal99d is on a distinguished road
  3. Wink Strtok()

    The following code does not work correctly:

    Code:
    	char * cmd="ls -l"; /* Unix command used for listing */
    	char * spCmd;	 
    	spCmd = strtok(cmd , " ");/* it divides the cmd in to tokens */
    	execvp(spCmd[0] , spCmd);
    output of this code to execute command that is written in cmd ?

    :?:
    Reply With Quote Reply With Quote
  4. #2
    niallj's Avatar
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Age
    22
    Posts
    612
    Rep Power
    8
  5. niallj is on a distinguished road
  6. Re: Strtok()

    Firstly, you're using the wrong function. The exec() family replace the current process with the one in the arguments, so it would replace your program with ls -l.

    Secondly, by passing the entire spCmd array as an argument to execvp(), you are actually telling it to run the command: ls ls -l. Just pass spCmd[1] as your second argument in execvp().
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Apr 2005
    Posts
    8
    Rep Power
    0
  8. wal99d is on a distinguished road
  9. Question Re: Strtok()

    Do you have any good code to show me this output DUDE. :?:
    Reply With Quote Reply With Quote
  10. #4
    niallj's Avatar
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Age
    22
    Posts
    612
    Rep Power
    8
  11. niallj is on a distinguished road
  12. Re: Strtok()

    I'm unclear about what you want to do. Do you want to write a program that will print the output of ls -l to the screen, or one that will give your program the output of ls -l?
    Reply With Quote Reply With Quote
  13. #5
    Nuticulus's Avatar
    Join Date
    Apr 2005
    Location
    England
    Age
    22
    Posts
    128
    Rep Power
    8
  14. Nuticulus is on a distinguished road
  15. Re: Strtok()

    I haven't used execvp() before, but after a little looking around, it seems that you supply several arguments to it in a list which will result as, when executed, argv[0], argv[1] etc. So, in other words, you are supposed to split up your string by the token " " into an array, then pass the first item to the first argument of execvp(), then the rest to the second. This array must be terminated with a null pointer.

    Code:
    char cmd[] = "/bin/ls /home/nuticulus/ -l";
    char **toks;
    int c=0;
    	
    printf("Running %s\n",cmd);
    toks = (char**) malloc(sizeof(char*));
    toks[0] = (char*)strtok(cmd," ");
    	
    while(toks[c]!=NULL)
    {
    	printf("#%d %s\n",c+1,toks[c]);
    	c++;
    	toks = (char**) realloc(toks, (c+1)*sizeof(char*));
    	toks[c] = (char*)strtok(NULL, " ");
    }
    	
    execvp(toks[0], toks);
    That would split the string cmd into an array of character pointers, delimited by a final NULL pointer.
    Reply With Quote Reply With Quote
  16. #6
    Join Date
    Apr 2005
    Posts
    8
    Rep Power
    0
  17. wal99d is on a distinguished road
  18. Question Re: Strtok()

    Code:
    	char *all;
    	char *res[80];
    	int i=0;
    	
    	all = "ls -l";
    	res[i]= strtok(all," ");
    		
    	while (res[i] != NULL){
    		res[++i]= strtok(NULL," ");
    	}
    	execvp(res[0],&res[0]);
     --->	   printf("Done\n");          <-- this is not executed why ??
    thankx gentelmen , but now my question is that why the last line cannot be
    executed ??

    :eek:
    Last edited by wal99d; 04-24-2005 at 10:25 AM.
    Reply With Quote Reply With Quote
  19. #7
    niallj's Avatar
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Age
    22
    Posts
    612
    Rep Power
    8
  20. niallj is on a distinguished road
  21. Re: Strtok()

    As I said in my first post, execvp() stops the current process, and replaces its code space in memory with that of the new process. In other words, your app dies and is replaced by ls -l.
    Reply With Quote Reply With Quote
  22. #8
    Join Date
    Apr 2005
    Posts
    8
    Rep Power
    0
  23. wal99d is on a distinguished road
  24. Re: Strtok()

    Thankx niallj
    Reply With Quote Reply With Quote