LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2009, 10:45 AM   #1
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Rep: Reputation: 0
i have the problem about:execv.


recently i am learning exec family.

in my class we always use execl, but our lecturer throw a question to us
: he want us to write a c program that can prompt the user to key in the executable file's path name,file name,arguments.

a example for execl:
execl("/bin/ls","ls","-l",0)

but, obviously, execl cannot fullfil the question. after a long time for me to search to solution, i found execv is a good method.

for example:
char *av[]={"ls","-1",0};
execl("/bin/ls",av);

from the example above i found a idea for my problem:

----------------------------------------------------------------
char pathName[20];//for path name string
scanf("%s",pathName);

int argumentNumber;
scanf("%d",&argumentNumber);// ask the user for number of arguments

char av[argumentNumber][20];//create a 2 dimensional array to store
//the arguments
int i;
for(i=0;i<argumentNumber;i++)//use for loop to store the arguments
scanf("%s",av[i]);

execv(pathName,av);
------------------------------------------------------------------
please forgive me to ignore the prompting message.

unfortunely, this program is not working.the system call function execv keep failed. this is really disappointed me.

i thought the problem is the NULL character in the execv function, so i add the '\0' into the last position:

*av[argumentNumber]='\0';


but execv still failed to execute.
moreover i tried to display the av array by using for loop, and the string inside is perfectly exist.

really hope someone can help me solve this problem.
really appreciated.

regard by sam.
 
Old 07-24-2009, 10:49 AM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
unfortunately the forum rules state that we can not help with your homework.

after you have found a solution then we might be able to add different ways of doing the same thing.
good luck with your homework.

Google/linux is a good tool

http://www.google.com/linux
 
Old 07-24-2009, 10:13 PM   #3
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 0
i admitted this is my homework. but i spend a lot of time still cannot figure out where is my problem.
Moreover i feel that i almost overcome the problem but still has a small things stuck me. The only way i can get
help from is internet, but i search quite a lot of website, they only introdue the usage of exec family simply but not deeply.
You know why i dont want to ask my lecturer? very funny is he also does not know where is my problem.

execv(pathName,av);

the argument pathName work corrently, but only that two dimesional array av work incorrectly.

this is what i found from internet: int execv(const char *path, char *const argv[]);

my program is supposed to work.......
if someone cannot give me the answer directly, please give me a hint or a direction to let me can continue my problem.
thanks again.
 
Old 07-25-2009, 07:58 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I suggest you use the Report button to ask the mods to get this moved to the Programming forum for faster help.
 
Old 07-26-2009, 02:43 AM   #5
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
I suggest you use the Report button to ask the mods to get this moved to the Programming forum for faster help.
oh my god, i am supposed to open the tread in programming part??
i though this is related to linux....moreover i am really a newbie...
 
Old 07-26-2009, 07:40 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by sam1201 View Post
oh my god, i am supposed to open the tread in programming part??
i though this is related to linux....moreover i am really a newbie...
No problem---you will not be punished!!!

Moved to programming
 
Old 07-26-2009, 10:46 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
You know why i dont want to ask my lecturer? very funny is he also does not know where is my problem.
Really? Seems like your lecturer doesn't know the material very well, you have (at least) 4 problems.

Quote:
Originally Posted by sam1201 View Post

i thought the problem is the NULL character in the execv function, so i add the '\0' into the last position:

*av[argumentNumber]='\0';
  • This statement is writing past the end of the array.
  • The execv function doesn't need a NULL character in the last position, it needs a NULL pointer.
  • You didn't include the header for execv (unistd.h)
  • If you had included that header the compiler would have warned you have a type mismatch, a 2D array of char's is not the same as an array of char pointers.
 
Old 07-27-2009, 09:53 AM   #8
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 0
really thanks for your reply. that is really helpful.

haha, so sorry i wrote wrongly the
*av[argumentNumber-1]='\0'; to *av[argumentNumber]='\0';
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
finally i think my problem is my 2D dimensional array:
char array[10][10]
only can pass to function(array), when the function is : void function(char [][column]), but execv is int execv(const char *path, char *const argv[]);

i thought the only way for let user to input string is two dimenional array:
char array[10][10];
because it specify the size, instead of declare like char *array[10], no doubt this will give run time error because unspecified size.

but now i found a way
char * array[10];
char str[10];

so i can let user key in the string and store in: scanf("%s",str);
after that i do this: array[0]=str; hahaha, the effect is same with two dimensional array. the size is also 10x10, but i can pass to execv as a pointer.

i think this time it is supposed to work
unfortunely, my ubuntu dont know why cannot compile c program, so i need to go to school lab to try my program, and the free lab cannot connect to my samba!!! i need to retype everytime. god bless me tomorow my program will be working. haha

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

in addition, why i did't include <unistd.h>, but no error for my execv?

and i pass the argument wrongly also no compile error, like this execv(pathName,argumentList) //argumentList is two dimenional array
but once i include <unistd.h>, my error appear xD ..

Last edited by sam1201; 07-27-2009 at 09:55 AM.
 
Old 07-27-2009, 10:18 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by sam1201 View Post

i think this time it is supposed to work
unfortunely, my ubuntu dont know why cannot compile c program
You didn't install gcc? apt-get install build-essential should fix that.


Quote:
in addition, why i did't include <unistd.h>, but no error for my execv?

and i pass the argument wrongly also no compile error, like this execv(pathName,argumentList) //argumentList is two dimenional array
but once i include <unistd.h>, my error appear xD ..
That's just the way C is. If you compile with the -Wall option then gcc will give you a warning about missing prototypes.
 
Old 07-27-2009, 11:32 AM   #10
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 0
ahhhhh, the concept above got a problem.

array[0]=str;

if a loop happen, all the array[i] will point to str and cause all array[i] same value because all point to same place = =. i forgot '=' in string is not copy of value......

luckily i just learn malloc() from internet, that is really a good function

argumentList[i]=malloc( sizeof(char)*20 );

so i don't need str anymore, i can directly use scanf("%s",argumentList[i]) in a for loop. but i need use malloc() everytime before scanf

this can solve the problem above. hopefully....

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

who know how to pass a two dimensional array to a pointer-to-array type of parameter?
for example:
char array[10][10];
function(???); // function(array) sure is not correct, i tried before.

void function(char *const av[]);

Last edited by sam1201; 07-27-2009 at 11:46 AM.
 
Old 07-27-2009, 11:42 AM   #11
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 0
[QUOTE=ntubski;3621770]You didn't install gcc? apt-get install build-essential should fix that.



yaya i tried that before, but i cannot connect to internet...
actually i do have wireless network in my school, but i am not sure how to use ubuntu link to wireless network. i follow the way that the internet teach to detect the wireless signal but i failed.

is that like this?
system->administration->network

i need connection because of this command:
sudo apt-get update
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing groupadd to execv() returned error msg: "file already locked." walwali Programming 9 11-22-2010 06:36 AM
suid application fails to call execv Ron_09 Linux - Newbie 0 05-12-2009 10:16 AM
Starting a Java application from c++ using execv JaseyJaseJase Programming 1 09-17-2008 07:17 PM
Question about execv() oddabe83 Programming 7 03-06-2008 12:35 PM
Unrecoverable Error: execv() error attempting to run powah Red Hat 0 09-06-2006 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration