getenv() in global space?

S

silrandir

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);
}
 
V

Victor Bazarov

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>


//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
 
I

Ian Collins

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top