-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd_echo_pwd.cpp
More file actions
107 lines (99 loc) · 3.06 KB
/
Copy pathcd_echo_pwd.cpp
File metadata and controls
107 lines (99 loc) · 3.06 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
// Function to split a string into tokens
std::vector<std::string> split(const std::string& input, char delimiter) {
std::vector<std::string> tokens;
size_t start = 0;
size_t end = input.find(delimiter);
while (end != std::string::npos) {
tokens.push_back(input.substr(start, end - start));
start = end + 1;
end = input.find(delimiter, start);
}
tokens.push_back(input.substr(start, end));
return tokens;
}
// Function to implement the built-in "cd" command
void cd(const std::string& arg) {
if (arg.empty()) {
// If no argument provided, change to the home directory
const char* home = getenv("HOME");
if (home == nullptr) {
std::cerr << "HOME environment variable is not set." << std::endl;
return;
}
if (chdir(home) != 0) {
perror("chdir");
}
} else if (arg == "-") {
// If argument is "-", change to the previous directory
const char* oldpwd = getenv("OLDPWD");
if (oldpwd == nullptr) {
std::cerr << "OLDPWD environment variable is not set." << std::endl;
return;
}
if (chdir(oldpwd) != 0) {
perror("chdir");
}
} else if (arg == "..") {
// If argument is "..", move up one directory
if (chdir("..") != 0) {
perror("chdir");
}
} else if (arg == "~") {
// If argument is "~", change to the home directory
const char* home = getenv("HOME");
if (home == nullptr) {
std::cerr << "HOME environment variable is not set." << std::endl;
return;
}
if (chdir(home) != 0) {
perror("chdir");
}
} else {
// Otherwise, change to the specified directory
if (chdir(arg.c_str()) != 0) {
perror("chdir");
}
}
}
// Function to implement the built-in "echo" command
void echo(const std::string& arg) {
std::cout << arg << std::endl;
}
// Function to implement the built-in "pwd" command
void pwd() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != nullptr) {
std::cout << cwd << std::endl;
} else {
perror("getcwd");
}
}
// int main() {
// while (true) {
// std::string input;
// std::cout << "> ";
// std::getline(std::cin, input);
// if (input == "exit") {
// break;
// } else if (input == "pwd") {
// pwd();
// } else {
// // Split the input into command and argument
// std::vector<std::string> parts = split(input, ' ');
// std::string cmd = parts[0];
// std::string arg = (parts.size() > 1) ? parts[1] : "";
// if (cmd == "cd") {
// cd(arg);
// } else if (cmd == "echo") {
// echo(arg);
// } else {
// std::cerr << "Command not found: " << cmd << std::endl;
// }
// }
// }
// return 0;
// }