472,986 Members | 2,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4525
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Xavier | last post by:
Greetings, While messing around with the "dl" module I ran into a segfault. *DO NOTE THAT THE FOLLOWING OCCURED ON 2 OF MY LINUX WORKSTATIONS* ------ # python -c 'import dl;...
2
by: Anand Subramanian | last post by:
Hi, Can someone explain the differences(setup, pre-main() setup/initialization) between global variables in a C++ and a C program? The global variables I used are uninitialized. I have a test.o...
10
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But...
5
by: Chad Paquette | last post by:
Hi, We have a legacy cgi app that's written in C. We are encountering an error when we try to retrieve a cgi environment variable. The variable we are getting contains a Base64 encoded...
10
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack...
10
by: Protoman | last post by:
What does getenv() do and why would you use it?
17
by: Protoman | last post by:
Why does getenv work backwards? When I'm testing an env variable, I have to test if its NOT equal to, rather than equal to, to get the desired result. As evidenced by this simple piece of code: ...
5
by: silrandir | last post by:
when attempting to call getenv() in global memory (c/c++), it constantly returns null, but when called in main, it properly reads the environment. is this expected behavior? sample code:...
4
by: Yogi Watcher | last post by:
Hi, Recently I have observed some odd behavior of getenv and putenv function. I am developing some code that integrates with several other libraries. This program is not using MFC. It is plain C...
8
by: Michael B Allen | last post by:
Is the string returned by getenv guaranteed to be the same string supplied to putenv plus the offset of the variable name and equals sign? Because of API constraints I do not want to save a...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.