![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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? |
|
|
|
|
|
#2 |
|
Newbie
|
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;
} |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Newbie
|
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;
} |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#6 |
|
Newbie
|
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?
|
|
|
|
|
|
#7 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unix commands compatible with Windows? | titaniumdecoy | Bash / Shell Scripting | 7 | Oct 5th, 2006 7:25 AM |
| Lions' Commentary on UNIX 6th Edition, with source code | Mad_guy | Book Reviews | 0 | Sep 24th, 2006 7:06 PM |
| Best Linux or Unix Desktop? | Prm753 | Coder's Corner Lounge | 23 | Apr 8th, 2006 10:17 AM |
| Importing from DLL's on Unix | Kaja Fumei | Existing Project Development | 5 | Jan 9th, 2006 6:35 PM |
| Setting Process Name | HackeZ | C++ | 11 | Dec 26th, 2005 11:25 AM |