473,809 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

environmental variables

Hello!!

how can i get all the details of the environmental variables. If i use
getenv, i will have to specify the name of the variable. for intsance i
will have to give getenv("PATH")

How can i get the information of all the variables, with the variable
names and its values

Pls Help

Casanova

Nov 14 '05 #1
15 1630
/*
This program should print all the environment variables.
*/
#include <stdio.h>
int main( int argc, char *argv[], char *envp[] )
{
for( int i = 0; envp[i] != NULL; ++i )
{
printf("%s\n", envp[i]);
}

return 0;
}
"Casanova" <pr********@gma il.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hello!!

how can i get all the details of the environmental variables. If i use
getenv, i will have to specify the name of the variable. for intsance i
will have to give getenv("PATH")

How can i get the information of all the variables, with the variable
names and its values

Pls Help

Casanova

Nov 14 '05 #2
Satya Das <sa************ **@yahoo.com> scribbled the following:
/*
This program should print all the environment variables.
*/
Only on some implementations .
#include <stdio.h>
int main( int argc, char *argv[], char *envp[] )
This form of main() is non-standard and causes undefined behaviour.
It might work for some implementations but is not guaranteed to work
on any.
{
for( int i = 0; envp[i] != NULL; ++i )
{
printf("%s\n", envp[i]);
} return 0;
}


--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson
Nov 14 '05 #3
Joona I Palaste wrote:
Satya Das <sa************ **@yahoo.com> scribbled the following:
/*
This program should print all the environment variables.
*/

Only on some implementations .

#include <stdio.h>
int main( int argc, char *argv[], char *envp[] )

This form of main() is non-standard and causes undefined behaviour.


Hmm, the C99 standard says in 5.1.2.2.1 Program startup, that main() is
defined
a) "... with no parameters"
b) "... or with two parameters"
c) "..., or in some other implementation-defined manner".

That's not undefined and should not cause nasal daemons or anything
else, should it? ;-)

Bjørn
[snip]
Nov 14 '05 #4
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Satya Das <sa************ **@yahoo.com> scribbled the following:
/*
This program should print all the environment variables.
*/
Only on some implementations . #include <stdio.h>
int main( int argc, char *argv[], char *envp[] )

This form of main() is non-standard and causes undefined behaviour.
It might work for some implementations but is not guaranteed to work
on any.


Just taking a closer look I found in the standard (C89):

In a hosted environment, the main function receives a third
argument, char *envp[], that points to a null-terminated array of
pointers to char, each of which points to a string that provides
information about the environment for this execution of the process

but which seems to contradict what's written further up about the
arguments of main() for a hosted environment:

The function called at program startup is named main. The
implementation declares no prototype for this function. It can be
defined with no parameters:

int main(void) { /*...*/ }

or with two parameters (referred to here as argc and argv, though any
names may be used, as they are local to the function in which they are
declared):

int main(int argc, char *argv[]) { /*...*/ }

Can someone shed some light on this? Is that why in C99 the extra
clause "or in some other implementation-defined manner" got added
(but they kept the requirement for the third, *envp argument, not
declaring that as also "implementa tion-defined")? Or does main()
receive a third argument but you're not allowed to put it into its
definition (but which would seem to be silly)?

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #5
Je***********@p hysik.fu-berlin.de wrote:
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Satya Das <sa************ **@yahoo.com> scribbled the following:
/*
This program should print all the environment variables.
*/
Only on some implementations .

#include <stdio.h>
int main( int argc, char *argv[], char *envp[] )

This form of main() is non-standard and causes undefined behaviour.
It might work for some implementations but is not guaranteed to work
on any.


Just taking a closer look I found in the standard (C89):

In a hosted environment, the main function receives a third
argument, char *envp[], that points to a null-terminated array of
pointers to char, each of which points to a string that provides
information about the environment for this execution of the process
[...]


The quoted text is from an appendix titled "Common
extensions." The paragraph immediately before the quote
begins

The following extensions are widely used in many
systems, but are not portable to all implementations .

Other nearby paragraphs mention such things as dollar
signs in identifiers, modifiable string literals, casting
data pointers to function pointers, the `asm' keyword, and
so on. Like the `envp' argument, all are non-Standard.

--
Er*********@sun .com

Nov 14 '05 #6
On Fri, 10 Dec 2004 12:33:52 +0000, Jens.Toerring wrote:
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Satya Das <sa************ **@yahoo.com> scribbled the following:
/*
This program should print all the environment variables.
*/
Only on some implementations .

And it is mostly deprecated even on those now. For reference the POSIX way
is to use an extern char **environ variable.
#include <stdio.h>
int main( int argc, char *argv[], char *envp[] )

This form of main() is non-standard and causes undefined behaviour.
It might work for some implementations but is not guaranteed to work
on any.


Just taking a closer look I found in the standard (C89):

In a hosted environment, the main function receives a third
argument, char *envp[], that points to a null-terminated array of
pointers to char, each of which points to a string that provides
information about the environment for this execution of the process


I believe you found this in an non-normative annex under a section called
"Common Extensions". I.e. this text is NOT part of the standard definition
of the C language.
but which seems to contradict what's written further up about the
arguments of main() for a hosted environment:

The function called at program startup is named main. The
implementation declares no prototype for this function. It can be
defined with no parameters:

int main(void) { /*...*/ }

or with two parameters (referred to here as argc and argv, though any
names may be used, as they are local to the function in which they are
declared):

int main(int argc, char *argv[]) { /*...*/ }

Can someone shed some light on this? Is that why in C99 the extra
clause "or in some other implementation-defined manner" got added
(but they kept the requirement for the third, *envp argument, not
declaring that as also "implementa tion-defined")? Or does main()
receive a third argument but you're not allowed to put it into its
definition (but which would seem to be silly)?


There is NO three argument form of main() in standard C. It is simply
recognised in the standard document as a common extension. C99 allows
implementation-defined extensions. All this means is that if an
implementation supports any forms of main() other than the 2 above (and
those compatible with them) it must document them. There is no requirement
that a particular implementation supports specific other forms such as the
envp one, and code that uses them results in undefined behvaiour as far as
the standard is concerned.

Lawrence

Nov 14 '05 #7
Eric Sosman <er*********@su n.com> wrote:
Je***********@p hysik.fu-berlin.de wrote:
Just taking a closer look I found in the standard (C89):

In a hosted environment, the main function receives a third
argument, char *envp[], that points to a null-terminated array of
pointers to char, each of which points to a string that provides
information about the environment for this execution of the process
[...]
The quoted text is from an appendix titled "Common
extensions." The paragraph immediately before the quote
begins The following extensions are widely used in many
systems, but are not portable to all implementations .


I see, thanks. Should have scrolled up instead of just down, not
really caring for anything that didn't contain the string "env"...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #8
On Fri, 10 Dec 2004 12:18:35 GMT
Bjørn Augestad <bo*@metasystem s.no> wrote:
Joona I Palaste wrote:
Satya Das <sa************ **@yahoo.com> scribbled the following:
/*
This program should print all the environment variables.
*/



Only on some implementations .

#include <stdio.h>
int main( int argc, char *argv[], char *envp[] )



This form of main() is non-standard and causes undefined behaviour.


Hmm, the C99 standard says in 5.1.2.2.1 Program startup, that main()
is defined
a) "... with no parameters"
b) "... or with two parameters"
c) "..., or in some other implementation-defined manner".

That's not undefined and should not cause nasal daemons or anything
else, should it? ;-)


Surely that means that if the implementation defines another prototype
it is implementation defined but if you do something neither explicitly
defined by the C standard nor the implementation then it is undefined
behaviour. So on a Posix system it may be defined (by the
implementation) but on Windows it might invoke undefined behaviour.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #9
Flash Gordon wrote:
On Fri, 10 Dec 2004 12:18:35 GMT
Bjørn Augestad <bo*@metasystem s.no> wrote:

Joona I Palaste wrote:

Satya Das <sa************ **@yahoo.com> scribbled the following:
/*
This program should print all the environment variables.
*/
Only on some implementations .

#include <stdio.h>
int main( int argc, char *argv[], char *envp[] )
This form of main() is non-standard and causes undefined behaviour.


Hmm, the C99 standard says in 5.1.2.2.1 Program startup, that main()
is defined
a) "... with no parameters"
b) "... or with two parameters"
c) "..., or in some other implementation-defined manner".

That's not undefined and should not cause nasal daemons or anything
else, should it? ;-)

Surely that means that if the implementation defines another prototype
it is implementation defined but if you do something neither explicitly
defined by the C standard nor the implementation then it is undefined
behaviour. So on a Posix system it may be defined (by the
implementation) but on Windows it might invoke undefined behaviour.


If a prototype for main() is neither according to the standard nor
supported by the implementation, I would expect a diagnostic message to
be printed instead of just silently allowing it and possibly invoke
undefined behaviour.

Now that I have tried it, I am not so sure anymore. gcc -std=c99 is
silent on the foo parameter unless -Wall is used, even if it reports
that main() doesn't return int. MSVC happily accepts the code, even when
/Za /W4 is used. :-|

Bjørn

$ cat m2.c
#include <stdio.h>

float main(int ac, char*av[], char**envp, char**foo)
{
while(*envp)
printf("%s\n", *envp++);

return 0;
}

boa@wintendo /tmp
$ gcc -std=c99 m2.c
m2.c: In function `main':
m2.c:4: warning: return type of `main' is not `int'

boa@wintendo /tmp
$ gcc -std=c99 -Wall m2.c
m2.c:4: warning: return type of `main' is not `int'
m2.c:4: warning: `main' takes only zero or two arguments

boa@wintendo /tmp
$

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2910
by: Bruce Lehmann | last post by:
Hi, I've installed Apache (WAMP install) so I can edit and test my web site on my home computer. In general php code seems to work, but I can't access Apache variables such as PATH or SCRIPT_NAME. The code below works perfectly when I upload it to my webhost server. However, at home, everything processes corectly except the environmental variables are blank.
1
6844
by: Ric | last post by:
thx for the help. im having problems with java and jsp. i think im not settup up the enviornmental variables right in w2k advanced tab. im using a wrox book, but the wrox book references a servlet in the classpath environmental variable: CLASSPATH %CATALINA_HOME%\common\lib\servlet.jar i dont see a servlet.jar file in that directory. i do see a servlet-api.jar file. are they right?
2
3087
by: Steven | last post by:
I have an asp.net application written in VB.net. I want to access a client side environment variable. For instance, we set an environment variable for the UserName. So, I write the code for retrieving the UserName. Instead of getting my name, I get the name under which the IIS service must be running.... ASPNET. Is there a way to get the client side environmental variable? Thanks,
4
6437
by: Shiraz | last post by:
Hi I'm using Visual Studio Installer to make my installer, and have not as yet figured out a straightforward way to use it to set environmental variables. Amongst the various things I tried, I'm thinking the following might help. I would appreciate if someone could comment on this idea and possibly suggest a better one: The environement variable in question is 'Path' in the HKCU registry folder's Environment key. I want to add some...
9
10820
by: Sathyaish | last post by:
In which physical file are the python environmental variables located? I know I can access them using the: os.environ.get('PYTHONSTARTUP') or os.environ.get('PYTHONPATH')
2
2212
by: Kourosh | last post by:
I'm just wondering, is there a way I could use an environmental variable in an XML file to specify the path of its XSL file? something like <?xml-stylesheet type="text/xsl" href="%test%\test.xsl" ?>
2
2547
Curtis Rutland
by: Curtis Rutland | last post by:
I want to get the actual physical path for some of the Windows Environmental Variables. If I typed %TEMP% into the Run prompt on Vista, I'd go to the folder: C:\Users\username\AppData\Local\Temp I tried putting %TEMP% as the path for a DirectoryInfo object, but that didn't work. Does anyone know how to do this?
9
2063
by: iavian | last post by:
Hi all , I want to set a env variable in a C program? . Not through shell env variables. Any help? Thanks Vijay
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.