urgent help needed with exec function

really, just a small thing taking killer time

Page 1 of 1

2 Replies - 479 Views - Last Post: 31 October 2009 - 02:13 PM Rate Topic: -----

#1 random92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-October 09

urgent help needed with exec function

Post icon  Posted 31 October 2009 - 10:48 AM

hi everyone

i have an array of arguments (such as ls, wc, etc) stored in here
char * args[10][8];



so the first block(10) represents the different commands in the command family(8)
so args[0][0] is ls and args[1][0] is -l for the command family ls -l and so on

i can use execv command easily with a predefined char* args like so
char *argsx[] = {"ls", "-l", (char *) 0 };



but when i try to create on the fly from the argument array at top like this
while(1)
		{
		args1[c] = args[c][0];
		if(args[c][0] == NULL)
		break;
		c++;
		}

execv(str1, args1);



it won't work although it will for execv(str1, argsx), the predefined one. when i print the contents of args1 and argsx they are the same. what gives?

i reallllllly need help on this and would appreciate it
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: urgent help needed with exec function

#2 random92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-October 09

Re: urgent help needed with exec function

Posted 31 October 2009 - 02:02 PM

wow,
took forever to find out why...

the initial string that is used to hold the command terminates with an enter key?

for instance
i use fgets to get a string from the user like "ls -l -r"

and when i print that character as in

printf("command %sx\n", command);

what gets printed is

ls -l -r
x

where the heck does that enter key get into the string? how can you remove that with strtok so it prints

ls -l -rx
?

thjanks!
Was This Post Helpful? 0
  • +
  • -

#3 hackterr  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 293
  • Joined: 13-August 09

Re: urgent help needed with exec function

Posted 31 October 2009 - 02:13 PM

when you use fgets() function the trailing newline that you type at the end of the command is also included into the string. You will have to replace it with a null or '\0' character after taking input with some code like
 
		for (i = 0; i < strlen(commandstring); i++)
		{
			if ( commandstring[i] == '\n' || commandstring[i] == '\r' )
				commandstring[i] = '\0';
		}


here commandstring is the string for storing command taken from the user

This post has been edited by hackterr: 31 October 2009 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1