#include #include // getcwd, chdir #include // perror #include #include //dirname #include using namespace std; const int MAX_SIZE = 256; int main() { char path[MAX_SIZE]; string command, append; char * env; char * tmp; string full, base, dir; while (command != "EXIT") { cout << endl; // show the behavior of the environment variable env = getenv("PWD"); if (NULL != env) { cout << "PWD: " << env << endl; } cout << endl; if (NULL == getcwd(path,MAX_SIZE)) { perror("getcwd"); return -1; } full = path; tmp = strdup(path); if (NULL == tmp){ perror("Strdup"); return -1; } dir = dirname(tmp); free(tmp); base = basename(path); cout << "basename: " << base << endl; cout << "directory: " << dir << endl; cout << "full: " << full << endl; cout << endl; cout << base <<" : "; cin >> command; if (command == "UP") { if(-1 == chdir(dir.c_str())) { perror("Up failed "); } } else if (command == "GO") { cin >> append; if(-1 == chdir(append.c_str())) { perror("chdir"); cout << "Could not change to " << append << endl; } } else if (command != "EXIT") { cout << "Unknown command " << command << endl; } } return 0; }