getenv() in global space?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • silrandir@gmail.com

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

  • Victor Bazarov

    #2
    Re: getenv() in global space?

    silrandir@gmail .com wrote:[color=blue]
    > When calling getenv() globally, the function returns NULL, yet when
    > called from main, returns an appropriate value.[/color]

    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.
    [color=blue]
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <unistd.h>[/color]

    <unistd.h> is not a standard header (regardless of the fact that it has
    'std' in its name).
    [color=blue]
    >
    > //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);
    > }
    >[/color]

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • Ian Collins

      #3
      Re: getenv() in global space?

      silrandir@gmail .com wrote:[color=blue]
      > When calling getenv() globally, the function returns NULL, yet when
      > called from main, returns an appropriate value.
      >[/color]
      No it doesn't, what system are you using?

      --
      Ian Collins.

      Comment

      Working...