Thread: getcwd() function messes up my environment variable

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    getcwd() function messes up my environment variable

    Hi, I'm creating a shell and when I call the getcwd() function, it makes any environment variables I defined go away. Here is an example:

    int main()
    {

    char dir[500];

    putenv("NEWDIR=/usr/bin");
    system("env"); //NEWDIR shows up
    getcwd(dir, 499);
    system("env"); //NEWDIR is gone

    return 0;

    }


    Any ideas?

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    maybe this should be in the linux programing forum

  3. #3
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Your code works fine for me.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Hmm, can you try this Nessarose?

    int main()
    {

    char *dirPtr;

    char *path;
    path = (char *)malloc(12);

    strcpy(path, "NEWDIR=/usr");

    putenv(path);

    free(path);

    std::cout << getenv("NEWDIR") << std::endl;
    dirPtr = (char *)malloc(5);
    std::cout << getenv("NEWDIR") << std::endl;

    free(dirPtr);
    return 0;

    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Hmm, can you try this Nessarose?

    what exactly are you trying to do there? by the way - don't forget to use code tags, please...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    I ran it and it prints out /usr twice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM