We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,344 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Strtok()

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
1 Day
Discussion Span
8 Years Ago
Last Updated
10
Views
wal99d
Newbie Poster
12 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
char * cmd="ls -l";

This should be:

char cmd[] = "ls -l";
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Skill Endorsements: 38

Are there any good solution :?:

wal99d
Newbie Poster
12 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

THank U Dave and Narue

wal99d
Newbie Poster
12 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What about

execvp(spCmd[0],spCmd);


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

wal99d
Newbie Poster
12 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

http://www.rt.com/man/execvp.3.html

execvp(/* single char */spCmd[0],/*pointer to char */spCmd);
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Skill Endorsements: 38


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:

wal99d
Newbie Poster
12 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

><-- this is not executed why ??

$ man execvp

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

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

Thankx Narue

wal99d
Newbie Poster
12 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.2132 seconds using 2.63MB