Closed scohe001 closed 9 years ago
According to the POSIX standard chapter 8, the PWD
environment variable always contains a valid absolute path to the present working directory. This means that for most purposes, you can read from this environment variable instead of calling getcwd
. For situations where PWD
and getcwd
will behave differently, see this stackoverflow post.
+3 points extra credit for a good question
From the standard you linked:
PWD This variable shall represent an absolute pathname of the current working directory. It shall not contain any filename components of dot or dot-dot. _The value is set by the cd utility._
After a quick test with this code:
char path[256];
getcwd(path, 256);
cout << path << endl;
cout << getenv("PWD") << endl;
cout << GREEN << uname << "@" << YELLOW << hostname << RESET << "$ ";
I get this output:
/Users/aricohen/Desktop/Dev/CS100/rshell
/Users/aricohen/Desktop/Dev/CS100/rshell
aricohen@Ace.local$ cd bin
/Users/aricohen/Desktop/Dev/CS100/rshell/bin
/Users/aricohen/Desktop/Dev/CS100/rshell
Suggesting that if we do end up using the PWD
environment variable we'll have to be updating it in our cd
function since the system will only help us on initialization. On the other hand, it seems getcwd
requires no help from us.
If we do use getcwd
, should we still update PWD
for the assignment?
Whoops! I misinterpreted the documentation I linked you. I thought it was saying that a compliant version of chdir
would automatically update PWD
, but it was actually saying that a compliant shell must update PWD
. So if you want your shell to be POSIX compatible (not required for the assignment) then you have to update PWD
. Either way you do it, you must be printing out the value of whatever getcwd
would give. But it's fine to never explicitly call getcwd
and only update the value of PWD
.
Cool, thank you so much!
PWD doesn't reflect any chdir calls you may make
Thanks,
Emil Nevarez On Mar 7, 2015 1:19 PM, "scohe001" notifications@github.com wrote:
It seems to me that to find the current working directory for the prompt we could either either call getcwd or use getenv to fetch the PWD environment variable storing the same information...is there any obvious reason to use one over the other?
I'm tempted to use getenv only because the memory for the string has already been allocated and we don't have to worry about the deallocation, whereas with getcwd to ensure we can handle any path length, we'll have to ask getcwed to allocate new memory for us.
Am I missing anything?
— Reply to this email directly or view it on GitHub https://github.com/mikeizbicki/ucr-cs100/issues/1088.
It seems to me that to find the current working directory for the prompt we could either call
getcwd
or usegetenv
to fetch thePWD
environment variable storing the same information...is there any obvious reason to use one over the other?I'm tempted to use
getenv
only because the memory for the string has already been allocated and we don't have to worry about the deallocation, whereas withgetcwd
to ensure we can handle any path length, we'll have to askgetcwd
to allocate new memory for us.Am I missing anything?