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
    Posts
    5,439
    >> 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:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 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, 06:18 AM

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