Skip to content

Commit 4d7f6b0

Browse files
committed
fix ~ support
1 parent d0efc08 commit 4d7f6b0

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/cd_command.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include <unistd.h>
44

55
void CdCommand::execute(const std::string& args) {
6-
int first_slash = args.find_first_of('/');
7-
std::string first_dir = args.substr(0, first_slash);
8-
if (first_dir == "~") {
9-
chdir((getenv("HOME")+"/"+args.substr(first_slash)).c_str());
6+
std::string path = args;
7+
if (args[0] == '~') {
8+
const char* home = getenv("HOME");
9+
if(home) {
10+
path = std::string(home) + args.substr(1);
11+
}
1012
}
11-
else if (chdir(args.c_str()) != 0) {
13+
if (chdir(path.c_str()) != 0) {
1214
std::cerr << "cd: " << args << ": No such file or directory" << std::endl;
1315
}
1416
}

0 commit comments

Comments
 (0)