-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDCommand.cpp
More file actions
44 lines (40 loc) · 1.07 KB
/
CDCommand.cpp
File metadata and controls
44 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Author: Jon Shidal
Purpose: CSE 332 lab 5
*/
#include "CDCommand.h"
#include<iostream>
using namespace std;
CDCommand::CDCommand(AbstractFileSystem * fileSys) : fs(fileSys) {}
int CDCommand::execute(std::string& CWD, std::string options) {
if (options == "..") { // move to parent directory
AbstractFile* f = fs->openFile(CWD);
if (f == nullptr) {
cout << "current directory doesn't exist" << endl;
return CWDdoesnotexist;
}
// update cwd to go back one directory
CWD = CWD.substr(0, CWD.find_last_of('/'));
fs->closeFile(f);
return success;
}
else {
if (options.find('.') != string::npos) {
cout << "not a directory" << endl;
return notadirectory;
}
AbstractFile * f = fs->openFile(CWD + "/" + options);
if (f == nullptr) {
cout << "directory does not exist" << endl;
return directorydoesnotexist;
}
CWD += '/';
CWD += options;
fs->closeFile(f);
return success;
}
}
void CDCommand::displayInfo() {
cout << "changes directories" << endl;
cout << "Usage: cd <directory name> or cd .." << endl;
}