470,848 Members | 1,829 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

getenv() in global space?

When calling getenv() globally, the function returns NULL, yet when
called from main, returns an appropriate value.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

//using namespace std;
#define MW_DEBUG "TEST"

char* debugstr = getenv(MW_DEBUG);
int debug = debugstr?1:0;

int main()
{
if(debug)
write(2, "\nDEBUG Enabled\n", 15);
else
write(2, "\nDEBUG DISABLED\n", 16);
debugstr = getenv(MW_DEBUG);
if(debugstr)
write(2, "\nDebug Enabled\n", 15);
else
write(2, "\nDebug Disabled\n", 16);
}

Feb 23 '06 #1
2 4362
si*******@gmail.com wrote:
When calling getenv() globally, the function returns NULL, yet when
called from main, returns an appropriate value.
So? The behaviour of 'getenv' is implementation-defined. You need to
read the documentation for your application, perhaps they say that any
call to 'getenv' is not going to succeed before the 'main' function has
been called. The platform is probably setting the "environment" just
before calling your 'main' function.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
<unistd.h> is not a standard header (regardless of the fact that it has
'std' in its name).

//using namespace std;
#define MW_DEBUG "TEST"

char* debugstr = getenv(MW_DEBUG);
int debug = debugstr?1:0;

int main()
{
if(debug)
write(2, "\nDEBUG Enabled\n", 15);
else
write(2, "\nDEBUG DISABLED\n", 16);
debugstr = getenv(MW_DEBUG);
if(debugstr)
write(2, "\nDebug Enabled\n", 15);
else
write(2, "\nDebug Disabled\n", 16);
}


V
--
Please remove capital As from my address when replying by mail
Feb 24 '06 #2
si*******@gmail.com wrote:
When calling getenv() globally, the function returns NULL, yet when
called from main, returns an appropriate value.

No it doesn't, what system are you using?

--
Ian Collins.
Feb 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Anand Subramanian | last post: by
10 posts views Thread by Shuo Xiang | last post: by
5 posts views Thread by Chad Paquette | last post: by
10 posts views Thread by Kleenex | last post: by
10 posts views Thread by Protoman | last post: by
17 posts views Thread by Protoman | last post: by
5 posts views Thread by silrandir | last post: by
4 posts views Thread by Yogi Watcher | last post: by
8 posts views Thread by Michael B Allen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.