getcwd() function messes up my environment variable

This is a discussion on getcwd() function messes up my environment variable within the C Programming forums, part of the General Programming Boards category; Hi, I'm creating a shell and when I call the getcwd() function, it makes any environment variables I defined go ...

  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,659
    >> 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...



    ITSA
    Socket Library!

  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, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21