I'm designing a mock shell program, and I can't quite mimic the "cd" command. I've tried chdir(), but that wasn't working, so I moved on to trying to change the environmental variable "PWD="
Here's what I have, and I think this may be close. (Please, please, correct me if I'm wrong or was closer with chdir())
else if (command == "cd")
{
string pathEnv = "PWD=";
string newDir;
cin >> newDir;
pathEnv+=newDir;
cout << pathEnv << endl;
putenv(pathEnv.c_str());
}
Hopefully the command would be 'cd /user/username/folder' and my pathEnv variable would be "PWD=/user/username/folder" which would maybe change the directory?
Any insight is greatly appreciated.
chdir() should be the command you are looking for. Did you use getcwd() to fetch the current working directory after you set it?
Here is the code which worked for me.
...
There are three versions of getcwd():
please consult the man page in unix for details of usage.