473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1590
/*
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********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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.helsinki.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.helsinki.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 "implementation-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***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #5
Je***********@physik.fu-berlin.de wrote:
Joona I Palaste <pa*****@cc.helsinki.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.helsinki.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 "implementation-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*********@sun.com> wrote:
Je***********@physik.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***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #8
On Fri, 10 Dec 2004 12:18:35 GMT
Bjørn Augestad <bo*@metasystems.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*@metasystems.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
"Casanova" <pr********@gmail.com> writes:
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


There is no portable way to do this in standard C.

Many implementations provide a way to do this. Consult the
documentation for your system (or, failing that, ask in an
implementation-specific newsgroup).

<OT>
Some systems may use
extern char **environ;
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11

In article <RX*******************@juliett.dax.net>, =?ISO-8859-1?Q?Bj=F8rn_Augestad?= <bo*@metasystems.no> writes:

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.


This is a dangerous expectation. The standard does not require a
diagnostic for this case. In fact, it doesn't require a diagnostic
for any specific case; what it requires is that, if a translation
unit has at least one constraint violation, the implementation must
emit at least one diagnostic (which might be "compilation finished",
say). But this isn't a constraint violation, so you don't even have
that guarantee.

The diagnostics provided by the implementation are purely a quality-
of-implementation issue, and many implementations are not of especially
high quality - including many of the most commonly used ones.

Moreover, there could be good reasons for an implementation to *not*
diagnose this. In particular, some implementations let you change the
parameters passed to main, and implement the parameterization of main
in a fashion sufficiently separate from the translator that the latter
does not know if you've changed it. (On some implementations, for
example, this would involve providing new "startup code".)

In short, it's better to know what the standard does and does not
require of an implementation, and what the implementation(s) you're
using documents as its implementation-defined behavior and extensions
to the language, than to hope that it will provide a diagnostic when
you violate the rules.

--
Michael Wojcik mi************@microfocus.com

[After the lynching of George "Big Nose" Parrot, Dr. John] Osborne
had the skin tanned and made into a pair of shoes and a medical bag.
Osborne, who became governor, frequently wore the shoes.
-- _Lincoln [Nebraska] Journal Star_
Nov 14 '05 #12
mw*****@newsguy.com (Michael Wojcik) writes:
In article <RX*******************@juliett.dax.net>,
=?ISO-8859-1?Q?Bj=F8rn_Augestad?= <bo*@metasystems.no> writes:
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.


This is a dangerous expectation. The standard does not require a
diagnostic for this case. In fact, it doesn't require a diagnostic
for any specific case; what it requires is that, if a translation
unit has at least one constraint violation, the implementation must
emit at least one diagnostic (which might be "compilation finished",
say). But this isn't a constraint violation, so you don't even have
that guarantee.

The diagnostics provided by the implementation are purely a quality-
of-implementation issue, and many implementations are not of especially
high quality - including many of the most commonly used ones.


(Irrelevant aside: the standard does require a specific diagnostic for
the "#error" directive.")

I don't think the situation is quite as dire as you imply. In my
experience, most implementations do make a good effort to produce
meaningful diagnostics for syntax errors and constraint violations,
including an attempt to indicate the line in the source file that
caused the problem. In the case of syntax errors, the parser is
likely to become confused about reporting any *further* errors (and a
misspelled typedef name is effectively a syntax error).

In the very worst case, a minimalist compiler could print a single
question mark as its only diagnostic, leaving the user to examine the
entire source file to figure out the problem. (For that matter, it
could print the question mark even if there is no error.) In the
worst realistic case, the first diagnostic emitted is likely to be
helpful in tracking down the problem; the usefulness of further
diagnostics depends on how badly the compiler was confused by the
first error.

But in the particular case of a non-standard declaration for main(),
you're right; the standard doesn't require a diagnostic, and many
implementations won't give you one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
In article <cp**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.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.


Bzzzzt. It is guaranteed to work on Unix (aka, POSIX).
This serves as a counterexample to the statement that there doesn't exist
a system under which it is guaranteed to work.

Nov 14 '05 #14
Kenny McCormack <ga*****@yin.interaccess.com> scribbled the following:
In article <cp**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.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.

Bzzzzt. It is guaranteed to work on Unix (aka, POSIX).
This serves as a counterexample to the statement that there doesn't exist
a system under which it is guaranteed to work.


When I say "guaranteed" I mean "guaranteed by the C standard", keeping
with the topic of this group. Implementations may guarantee what they
want, as long as it doesn't conflict with the C standard, but that's off
topic here.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am lying."
- Anon
Nov 14 '05 #15
In article <cp**********@yin.interaccess.com>,
Kenny McCormack <ga*****@interaccess.com> wrote:
In article <cp**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.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.


Bzzzzt. It is guaranteed to work on Unix (aka, POSIX).
This serves as a counterexample to the statement that there doesn't exist
a system under which it is guaranteed to work.


What he was trying to say was that it was not guaranteed to work
under Standard C. Of course some thing else may guarantee it.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 14 '05 #16

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

Similar topics

1
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...
1
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...
2
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...
4
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...
9
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
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"...
2
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 ...
9
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...
0
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...

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.