0

Is the following code correct:

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 ?
:?:

3
Contributors
9
Replies
10
Views
11 Years
Discussion Span
Last Post by wal99d
0

>Are there any good solution
Yes, Dave's solution is a good one. Your code will fail because string literals can't be modified, and strtok modifies the string you pass to it. Changing cmd to an array fixes the problem.

Just in case you didn't catch it, remove the parts in red and add the parts in blue.

0

What about

execvp(spCmd[0],spCmd);


if I do so the output will be like :
Segmentation fault
any suggestions?plz
:cheesy:

0


The following code works fine ,but once it reaches the last line it will not be printed why ?? any suggestions are appreciated thankx dave

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

:eek:

0

><-- this is not executed why ??

$ man execvp

That should tell you pretty quickly what happens when execvp is called.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.