Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 7th, 2006, 10:38 PM   #1
Ghosty
Newbie
 
Join Date: May 2005
Location: NJ
Posts: 18
Rep Power: 0 Ghosty is on a distinguished road
Send a message via AIM to Ghosty
setting env vars in unix

Please examine the following code:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){

  char * envVar;
  envVar = getenv ("PS1");

  cout << "PS1 value is currently: " << *envVar << endl;
  cout << "We will now use putenv to change the PS1 variable" << endl;

  putenv("PS1=<duck>");

  envVar = getenv ("PS1");
  cout << "PS1 value is currently: " << *envVar << endl;
  return 0;
}

My book says that I have to use malloc aquire more space for "<duck>" but I've never used the malloc function. Can anyone tell me how to use malloc or now to use, perhaps, the new call?
Ghosty is offline   Reply With Quote
Old Nov 7th, 2006, 11:58 PM   #2
Ghosty
Newbie
 
Join Date: May 2005
Location: NJ
Posts: 18
Rep Power: 0 Ghosty is on a distinguished road
Send a message via AIM to Ghosty
Updated. I'm using the malloc command successfully now, but I don't understand how to increase the size of my environment variable.

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main(){

  char * envVar = static_cast<char*>(malloc(7));
  envVar = getenv ("PS1");

  cout << "PS1 value is currently: " << *envVar << endl;
  cout << "We will now use putenv to change the PS1 variable" << endl;

  putenv("PS1=<duck>");

  envVar = getenv ("PS1");
  cout << "PS1 value is currently: " << *envVar << endl;

  free(envVar);
  return 0;
}
Ghosty is offline   Reply With Quote
Old Nov 8th, 2006, 1:00 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Right away you are setting envVar to an address of memory loaned to you by malloc. Right after that you are setting it to an address given to you by 'getenv'. You have lost the address given to you by malloc, thus you will not be able to free it without barfing in your own shirt pocket. I would suggest that you learn, first, about pointers, and secondly, about malloc/free and its requirements. Actually, since this is C++, you should shelve malloc/free in favor of new/delete.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 8th, 2006, 1:34 AM   #4
Ghosty
Newbie
 
Join Date: May 2005
Location: NJ
Posts: 18
Rep Power: 0 Ghosty is on a distinguished road
Send a message via AIM to Ghosty
Fixed. Thanks for the reply DaWei. That code is rubbish anyway. My completed assignment is 4 lines of code. I didn't need malloc/free or new/delete as my book was misleading. I do need to get a refresher on pointers though.

//This code works
#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

  char name []={"PS1=<duck>"};
  putenv(name);
  char * envVar = getenv ("PS1");
  cout << "PS1 value is currently: " << envVar << endl;

  return 0;
}
Ghosty is offline   Reply With Quote
Old Nov 8th, 2006, 3:53 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Note that this code will only work from within main. If you made a function using this code, the "name" array will go out of scope while the environment is still using it.
I suspect that it what your book was getting at, when you use putenv the pointer you pass in may be used by the runtime library until it the environment value is reset, so using a local variable is a problem. One way to get around this is to use malloc to allocate the space for the memory passed to putenv.
The Dark is offline   Reply With Quote
Old Nov 8th, 2006, 12:42 PM   #6
Ghosty
Newbie
 
Join Date: May 2005
Location: NJ
Posts: 18
Rep Power: 0 Ghosty is on a distinguished road
Send a message via AIM to Ghosty
That certainly seems like a better explanation The Dark. I don't really want to use malloc if I don't have to though. How would I use new to create space for the pointer that I'd be passing if I was making a function?
Ghosty is offline   Reply With Quote
Old Nov 8th, 2006, 4:00 PM   #7
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by The Dark View Post
Note that this code will only work from within main. If you made a function using this code, the "name" array will go out of scope while the environment is still using it.
The entry point of your program is the main function. In the main functions, you usually call a number of functions. Does this mean your main function gets out of scope? Well, no. When you call function x, your variables from the main function are still on the stack. How else could you return from function x and continue in your main function?
Your local variables in function main will get unavailable once you return from the main function. And what happens when you quit the main function? Your program is finished, and the variable is no longer used anyway.

I'm not sure how this applies when running multiple threads, it may be OS specific (running another thread while main thread has finished) - another member can probably tell you that. I usually block the main thread when it has ended and other threads have not.
Polyphemus_ is offline   Reply With Quote
Old Nov 8th, 2006, 9:02 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
When i said "If you made a function using this code" i meant making the code as shown into a function (e.g. by changing the name from main to something else), not making some of the code into a function, but somehow leaving the variables in the main function. Sorry if that wasn't clear.

Ghosty, you can use new in the same way as malloc, in this case you could use something like:
  const char *eName = "PS1";
  const char *eVal = "<duck>";
  char *name = new char [strlen(eName) + 1 + strlen(eVal) + 1];
  strcpy (name, eName);
  strcat (name, "=");
  strcat (name, eVal);
  putenv(name);

I am not sure when you could delete the memory - from the man page I read, it seemed you could only delete it when you set the PS1 variable to something else.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix commands compatible with Windows? titaniumdecoy Bash / Shell Scripting 7 Oct 5th, 2006 8:25 AM
Lions' Commentary on UNIX 6th Edition, with source code Mad_guy Book Reviews 0 Sep 24th, 2006 8:06 PM
Best Linux or Unix Desktop? Prm753 Coder's Corner Lounge 23 Apr 8th, 2006 11:17 AM
Importing from DLL's on Unix Kaja Fumei Existing Project Development 5 Jan 9th, 2006 7:35 PM
Setting Process Name HackeZ C++ 11 Dec 26th, 2005 12:25 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC