LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-14-2009, 06:40 AM   #1
vtn
LQ Newbie
 
Registered: Aug 2009
Location: Alpharetta, GA, USA
Distribution: Ubuntu
Posts: 9

Rep: Reputation: 1
environ variable and command line arguments


Hello,

I have couple of questions:

1.) I am trying to see the environment variables in my system using global variable environ and the command line argument char * envp[].

Following is my code snippet:
-------------------------------------------------

extern char* environ[];

int main(int argc, char* argv[], char *envp[])
{
int i=0, j=0;
while(environ[i])
{
printf("%s\n", environ[i]);
i++;
}
while(envp[j])
{
printf("%s\n", envp[j]);
j++;
}
}
-------------------------------------------------

The first while loop does not give proper results.
But when I changed the declaration of global variable to "extern char ** environ"it gave proper results.

can some one clarify me this as why is this happening. As per my understanding char* environ[] and char** environ are same. And also the second loop worked fine though I declared envp as char * envp[].



2.) When I ran the above program with out passing any command line arguments, it still worked and gave proper results with loop 2, the envp argument. How come? I didnt pass this argument, how could it still pull in the environment variables to envp? And the main thing, if I would like to pass the command line arguments I would I do that?

Thanks a lot in advance.

Last edited by vtn; 08-16-2009 at 07:10 AM. Reason: corrected the code posted as it diverts the problem
 
Old 08-14-2009, 04:34 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
1. This part is tricky. IMO when you add extern char* environ[] you redefine it, in fact. On the other hand, if you use exactly the same extern as in unistd.h, it is just a repeat. In fact, I wouldn't redefine environ (any way), just compile with -D_GNU_SOURCE (if you do not have other reasons to leave it off).

If you want to learn more about this problem, read about weak symbols and then move to this discussion: http://sourceware.org/ml/binutils/2007-07/msg00304.html (but it's rather complicated).

2. This one is simple You don't reset i to 0 after the first loop.
 
Old 08-14-2009, 05:36 PM   #3
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285
Blog Entries: 1

Rep: Reputation: 37
i actually can't say about the problem...i'll figure it out and let you know....

the thing is whenever you write programs, let it be any language, follow proper indentation and proper commenting.
it helps oyhers to grasp your program quickly.







Enjoy programming...
 
Old 08-16-2009, 07:22 AM   #4
vtn
LQ Newbie
 
Registered: Aug 2009
Location: Alpharetta, GA, USA
Distribution: Ubuntu
Posts: 9

Original Poster
Rep: Reputation: 1
Hi Mara,

Thanks for your response and I understood what I was doing regarding first loop..thanks.

Regarding the second loop, my question was different...when I ran the code I was running one loop at a time and when I posted it I forgot to initialize i to 0... my bad . Anyways I edited the code I posted. But my question was, after compiling the code and if I run it with out passing the command line arguments like ./a.out at the prompt, envp is still available inside the code. I was assuming it to complain that envp unkown variable when I run it with out passing the arguments.

Also if I need to pass command line arguments when running a program, how would I do it?

Thanks.
 
Old 08-16-2009, 07:25 AM   #5
vtn
LQ Newbie
 
Registered: Aug 2009
Location: Alpharetta, GA, USA
Distribution: Ubuntu
Posts: 9

Original Poster
Rep: Reputation: 1
Hi

Thanks for the suggestion ..but I did maintain the indentation but when I post the code it is getting corrupted and all spaces are being removed. How do I do that.

Thanks

Quote:
Originally Posted by lemon09 View Post
i actually can't say about the problem...i'll figure it out and let you know....

the thing is whenever you write programs, let it be any language, follow proper indentation and proper commenting.
it helps oyhers to grasp your program quickly.







Enjoy programming...
 
Old 08-16-2009, 07:11 PM   #6
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
As you've used quote tags above, use the word 'code' in tags instead, in the exact same way.
BTW, envp is supplied as part of the compiler http://www.cs.ui.ac.id/WebKuliah/IKI...nt_Access.html
 
  


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
Command line arguments bioinformatics_guy Linux - Newbie 2 02-12-2009 07:26 AM
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 09:40 AM
command line arguments nickraj Programming 6 09-11-2006 01:01 PM
Command line arguments?? almagerenia Linux - Newbie 1 09-08-2006 04:05 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 08:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:04 PM.

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