-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.cpp
More file actions
29 lines (23 loc) · 648 Bytes
/
Copy pathcd.cpp
File metadata and controls
29 lines (23 loc) · 648 Bytes
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
#include "cd.h"
#include <filesystem>
#include <string>
namespace fs = std::filesystem;
std::string changeDir(const std::string& givenPath) {
fs::path newPath;
if (givenPath == ".") {
return "";
} else if (givenPath == "..") {
newPath = fs::current_path().parent_path();
} else {
newPath = fs::absolute(givenPath);
}
if (newPath.empty() || !fs::exists(newPath)) {
return "cd: No such file or directory\n";
}
try {
fs::current_path(newPath);
} catch (fs::filesystem_error& e) {
return std::string("cd: ") + e.what() + "\n";
}
return ""; // success
}