-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
executable file
·238 lines (206 loc) · 5.31 KB
/
shell.c
File metadata and controls
executable file
·238 lines (206 loc) · 5.31 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdbool.h>
#include <fcntl.h>
//static char *line_read = (char *)NULL;
#define COLOR_RED "\x1b[35m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_RESET "\x1b[0m"
int main(int argc, char* argv[], char* envp[])
{
printf(COLOR_BLUE"\n //----\\ || || || ||===== //====== || || ||==== || ||\n");
printf(" || || || || || || || || || || || ||\n");
printf(" ||---- || || || ||===== \\\\----\\\\ ||====|| ||==== || ||\n");
printf(" || || || || || || || || || || || ||\n");
printf(" ||____/ ||===== \\\\---// ||===== ======// || || ||==== ||==== ||====\n");
printf(COLOR_RESET);
char* c;
char* parameters[128];
char *cptr;
int parameterCount = 0;
//char piping = '|', redirection = '>', wildcard ='*';
//Little code for having the time displayed as well as the shell name
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime(&rawtime);
printf("\nLog-in time: %s",asctime(timeinfo));
char* pegpoint = NULL;
while(1)
{
int z;
char* path = "/bin/";
char* envList[] = {"HOME=/root", "PATH=/bin:/sbin", NULL};
bool chngdir = false;
char *change = NULL;
//Loop that resets arrays to null
for (z=0; z<= 128; z++)
{
parameters[z] = NULL;
}
parameterCount = 0;
//Display and read the message
int reg;
//Current working directory display
char cwd[1024];
getcwd(cwd,sizeof(cwd));
printf(COLOR_BLUE"\n(%s)"COLOR_RESET,cwd);
if(pegpoint!=NULL)
{
printf(COLOR_RED"\t\t\t Peg:(%s)"COLOR_RESET,pegpoint);
}
//prompt display
c = readline(COLOR_BLUE"\n[BLUE_SHELL ]:- "COLOR_RESET);
reg = strcmp(c,"");
while (reg == 0)
{
printf(COLOR_BLUE"\n(%s)\n"COLOR_RESET,cwd);
c = readline(COLOR_BLUE"[BLUE_SHELL ]:- "COLOR_RESET);
reg = strcmp(c,"");
if (reg != 0)
break;
}
add_history(c);
//Store every word as an array of strings to parse later
cptr = strtok(c, " ");
while (cptr != NULL)
{
parameters[parameterCount] = cptr;
parameterCount++;
cptr = strtok(NULL, " ");
}
//Exiting out of the shell
int ret;
ret = strcmp(parameters[0],"exit");
if(ret == 0)
{
printf("\nLog-out time: %s\n",asctime(timeinfo));
exit(0);
}
//debugging.. set to 1 to see all the arguments on each seperate line
if(0)
{
int s = 0;
while(s != parameterCount)
{
printf("%d: %s\n",s,parameters[s]);
s++;
}
}
//Special feature: Pegging
int pegVal;
pegVal = strcmp(parameters[0],"peg");
if(pegVal == 0)
{
char dir[1024];
pegpoint = getcwd(dir,sizeof(dir));
continue;
}
pegVal = strcmp(parameters[0],"rmpeg");
if(pegVal == 0)
{
if(pegpoint == NULL)
{
printf("No peg in place to remove. Type \"peg\" to place holder.\n");
continue;
}
pegpoint = NULL;
continue;
}
pegVal = strcmp(parameters[0],"mv2peg");
if(pegVal ==0)
{
//printf("%s\n",pegpoint);
if(pegpoint == NULL)
{
printf("No peg set in place to move to. Type \"peg\" to place holder.\n");
continue;
}
else
{
//char* destination = pegpoint;
//const char* source =
//printf("%s - %s\n",parameters[1],destination);
//execve("/bin/mv",¶meters[1],destination);
continue;
}
}
//Fork
int rc = fork();
//if Fork fails
if(rc < 0)
{
printf("Oops! Something went wrong!");
exit(0);
}
//Child Code
else if(rc == 0)
{
//This will be the final path to the program that we will pass to execv
char prog[1024];
//First we copy a /bin/ to prog
strcpy(prog, path);
// Allowing for detection of piping, redirection
int i;
for (i = 0; i < parameterCount; i++)
{
//Execute the program from bash, or by itself
//Then we concancate the program name to /bin/
//If the program name is ls, then it'll be /bin/ls
//Special Case for cd
int cd = strcmp(parameters[i],"cd");
if(cd == 0)
{
change = parameters[1];
cd = chdir(change);
if(cd == 0)
chngdir = true;
else
printf("%s:No such file or directory found!\n",parameters[1]);
return 0;
}
//For other commands follow the path
strcat(prog, parameters[i]);
int retc = execve(prog,parameters,envList);
if (retc == -1)
{
strcpy(prog,"");
strcat(prog,parameters[i]);
retc = execve(prog,parameters,envList);
if (retc == -1)
{
strcpy(prog,"/usr/bin/");
//printf("Checked /usr/bin\n");
strcat(prog,parameters[i]);
retc = execv(prog,parameters);
if (retc == -1)
{
strcpy(prog,"/sbin/");
strcat(prog,parameters[i]);
retc = execv(prog,parameters);
if(retc == -1)
{
printf("The process %s could not be recognized.\n",c);
}
}
}
}
}
}
//Parent Code
else if(rc > 0)
{
wait(NULL);
//printf("Parent finished waiting\n");
change = parameters[1];
if(chngdir == true);
chdir(change);
}
}
}