-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsh.cpp
More file actions
53 lines (41 loc) · 889 Bytes
/
tsh.cpp
File metadata and controls
53 lines (41 loc) · 889 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "tsh.h"
#include <sys/wait.h>
using namespace std;
void simple_shell::parse_command(char* cmd, char** cmdTokens) {
// TODO: tokenize the command string into arguments
int j = 0;
cmd[strlen(cmd) - 1] = '\0';
char *tok =strtok(cmd," ");
while (tok != NULL){
cout << tok;
cmdTokens[j]=tok;
j++;
tok = strtok(NULL," ");
}
cmdTokens[j]=NULL;
}
void simple_shell::exec_command(char **argv)
{
// TODO: fork a child process to execute the command.
// parent process should wait for the child process to complete and reap it
int rc =fork();
if (rc < 0) {
exit(1);
}
else if (rc == 0){ //child
execvp(argv[0],argv);
exit(1);
}
else{
int w = wait(NULL);
}
}
bool simple_shell::isQuit(char *cmd){
if(strcmp(cmd,"quit")==0){
return true;
}
else{
return false;
}
// TODO: check for the command "quit" that terminates the shell
}