4

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.

twsmale
  • 139
  • 1
  • 5
  • 14
  • Changing the current directory of a process is done with the `chdir()` system call. Changing the environment variable doesn't do anything else other than change the environment variable. What problem did you have with `chdir()`? – Greg Hewgill May 29 '12 at 01:51
  • wow, good idea to change $PWD though. ^_^, chdir() , and spend sometime on OS system API basics – zinking May 29 '12 at 01:53
  • How specifically was `chdir()` not working? – HighCommander4 May 29 '12 at 02:36
  • @zinking I can't tell if that's sarcasm or not, but I'm learning by trial and error, and I thought that may help... – twsmale May 30 '12 at 02:27
  • @twsmale it's not sarcasm, I would think it's kind make sense to think of changing $PWD, to be honest, I think in that way at first as well. while agree that trail and error works for you to learn things, but it's not that efficient, learning things systematically if you want to dive deeper. that's my opinion. – zinking May 31 '12 at 01:48

2 Answers2

8

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.

#include <iostream>
#include <string>
#include <sys/param.h>
#include <unistd.h>

...

if (command == "curr") {
    char buffer[MAXPATHLEN];
    char *path = getcwd(buffer, MAXPATHLEN);
    if (!path) {
        // TODO: handle error. use errno to determine problem
    } else {
        string CurrentPath;
        CurrentPath = path;
        cout << CurrentPath << endl;
    }
} else if (command == "cd") {
    string newDir;
    cin >> newDir;
    int rc = chdir(newDir.c_str());
    if (rc < 0) {
        // TODO: handle error. use errno to determine problem
    }
}

There are three versions of getcwd():

char *getcwd(char *buf, size_t size);
char *getwd(char *buf);
char *get_current_dir_name(void);

please consult the man page in unix for details of usage.

Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Here's what I've edited: else if (command == "curr") { size_t size; string CurrentPath; char * path=new char[1024]; path = getcwd(path, size); CurrentPath = path; cout << CurrentPath << endl; } else if (command == "cd") { string newDir; cin >> newDir; chdir(newDir.c_str()); } Seg Faults, thoughts? – twsmale May 30 '12 at 02:26
  • @ThomasSmale: You're not initialising `size` in the `curr` case. You are expected to pass the size of the `path` buffer into `getcwd()`. Also, don't forget to `delete path`, or just allocate it as a local variable `char path[1024]` since the buffer doesn't need to exist longer than the end of the scope. – Greg Hewgill May 30 '12 at 02:57
  • Updated my answer to include an example. – Jeffery Thomas May 30 '12 at 12:19
  • Got it! I had to initialize MAXPATHLEN, then everything worked like a charm. Thanks for the help! – twsmale May 30 '12 at 22:02
  • Just to remember to change back to current directory (getcwd() will give you this) after you do chdir() in any program that carry out multiple tasks. MAXPATHLEN is defined in sys/param.h – Kemin Zhou Jan 04 '17 at 19:45
1

You'll always want to use system calls in your code and not "mock" up what the system may be doing such as changing PWD. If you are on a superior UNIX system you should use chdir or if on a Windows Box, the SetCurrentDirectory call. I'm not a Windows developer, whew, but I found this link. http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806%28v=vs.85%29.aspx

jiveturkey
  • 2,484
  • 1
  • 23
  • 41