-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.cpp
More file actions
49 lines (42 loc) · 1.06 KB
/
builtin.cpp
File metadata and controls
49 lines (42 loc) · 1.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
#include <iostream>
#include <unistd.h>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include "tinyshell.h"
#include "job.h"
using namespace std;
bool handle_builtin(const vector<string>& args) {
if (args.empty()) return false;
if (args[0] == "jobs") {
list_jobs();
return true;
}
if (args[0] == "fg" && args.size() == 2) {
bring_to_foreground(std::stoi(args[1]));
return true;
}
if (args[0] == "bg" && args.size() == 2) {
bring_to_background(std::stoi(args[1]));
return true;
}
if (args[0] == "exit") {
exit(0);
}
if (args[0] == "pwd") {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != nullptr)
cout << cwd << endl;
else
perror("pwd failed");
return true;
}
if (args[0] == "cd") {
const char* path = args.size() > 1 ? args[1].c_str() : getenv("HOME");
if (chdir(path) != 0)
perror("cd failed");
return true;
}
return false; // Not a built-in
}