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);
} 2 4586 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 "environmen t" 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 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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; a=dl.open("libc.so.6"); print a.call("getenv")'
or while in the interpreter:
|
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 which declares a global int " int xxx;". Now I link
test.o to a FreeBSD kernel module which then tries to access xxx. If
test.o was compiled from C...
|
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 what about the global space? Is that the same
as the heap space or is that still a form of special stack? Because once
I've seen someone declare a 1...
|
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 distinguished name.
If the distinguished is greater than 57 characters in length, a
carriage return is appended to the 58th character. This of course...
|
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 space, I
tried to only use parameters and stack space for things which are truely
temporary. Instead of passing a pointer to a data structure which...
| |
by: Protoman |
last post by:
What does getenv() do and why would you use it?
|
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:
#include <iostream>
#include <cstdlib>
using namespace std;
const char* cpu=getenv("PROCESSOR_ARCHITECTURE");
|
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:
#include <cstdlib>
#include <stdio.h>
#include <unistd.h>
using namespace std;
|
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 and C++ code.
Some login and initialization function from one dll is setting an
environment variable “MANUAL_LOGIN” to value “TRUE”. I...
|
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 pointer to the string
passed to putenv but I need to be able to free it later or I will have
a memory leak.
The following code demponstrates that with...
|
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, well explore What is ONU, What Is Router, ONU & Routers main...
| |
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. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |