-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
133 lines (95 loc) · 1.92 KB
/
Copy pathshell.cpp
File metadata and controls
133 lines (95 loc) · 1.92 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<readline/readline.h>
#include<readline/history.h>
#define maxLetters 1000
#define maxCommands 10
#define clear() printf("\033[H\033[J")
int takeInput(char* commandLine)
{
char* buff;
buff = readline("\n ~$ ");
if(strlen(buff) !=0)
{
add_history(buff);
strcpy(commandLine, buff);
return 0;
}
else return 1;
}
void printCwd()
{
char buffer[2048];
getcwd(buffer, 2048);
printf("Current Directory: %s", buffer);
}
void parseWords(char* commandLine, char** parsed)
{
int i;
for(i=0 ; i<maxCommands ; i++)
{
parsed[i] = strsep(&commandLine, " ");
if(parsed[i] == NULL)
break;
if(strlen(parsed[i]) == 0)
i--;
}
}
void execCommand(char** parsed)
{
pid_t pid, wpid;
int status;
pid = fork();
if(pid == -1)
{
printf("Forking just failed!");
return;
}
else if(pid == 0)
{
if(execvp(parsed[0], parsed) <0) printf("Could not execute your command!");
exit(0);
}
else
{
do{
wpid = waitpid(pid, &status, WUNTRACED);
}while (!WIFEXITED(status) && !WIFSIGNALED(status));
return;
}
}
int changeDir (char** parsed)
{
if(strcmp(parsed[0], "cd") == 0)
{
chdir(parsed[1]);
return 1;
}
return 0;
}
int processStr(char* commandLine, char** parsed)
{
parseWords(commandLine, parsed);
if(changeDir(parsed))
return 0;
else
return 1;
}
int main()
{
char input[maxLetters], *parsedArgs[maxCommands];
int execFlag;
while(1)
{
printCwd();
if(takeInput(input)) continue;
execFlag = processStr(input, parsedArgs);
if(execFlag == 1)
execCommand(parsedArgs);
}
return 0;
}