-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleshell.c
More file actions
291 lines (271 loc) · 6.14 KB
/
simpleshell.c
File metadata and controls
291 lines (271 loc) · 6.14 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/************************************************************************************
*
* Synopsis:
* This program is a simple C shell for Linux
*
* File: simpleshell.c
*
* Authors: Paul McGurk, Jordan Melville.
*
* All code, unless specified, is of our own creation.
*
* ***********************************************************************************/
#define VERSION "simpleshell v1.0. Last update: 13/04/2014\n\n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#define terminate 0
#define maxHistSize 20
#define MAX_INPUT 512
char *history[maxHistSize];
int historySize = 0;
char *aliases[20][2];
int aliasesSize = 0;
char *tokens[50];
char *path;
void setup() {
/* attempt to open file */
FILE *rf = fopen("history.txt", "rb");
/* if there is no file, return */
if (rf) {
fread(history, sizeof(char), historySize, rf);
}
}
void addHist() {
int i;
/* If historySize if not at the limit of History Array */
if (strcmp(tokens[0], "history") != 0) {
if (historySize < maxHistSize) {
history[historySize++] = strdup(*tokens);
}
/* else move all elements up and add */
else {
for (i = 1; i < maxHistSize; i++) {
history[i - 1] = history[i];
}
history[maxHistSize - 1] = strdup(*tokens);
}
}
}
void invokeHist() {
int num = historySize - 1, i , hist;
/* check if is a digit and is within historySize */
if ((tokens[0][1] - '0') > historySize) {
printf("History does not go back that far.\n");
}
else {
/* call last command */
if (!strncmp(tokens[0], "!!", 2)) {
num = historySize - 1;
}
else if (!isdigit(tokens[0][1])) {
printf("Please enter a digit!\n");
}
/* call command x in history */
else if (isdigit(tokens[0][2])) {
hist = (tokens[0][2] - '0') + ((tokens[0][1] - '0') * 10);
num = historySize - 1 - hist;
} else {
hist = (tokens[0][1] - '0');
num = historySize - 1 - hist;
}
tokens[0] = strdup(history[num]);
}
}
printHist() {
int i = 0;
/* Print function */
for (i = 0; i < historySize; i++) {
if (historySize - i - 1 != 0) {
printf("!%d for %s\n", historySize - i - 1, history[i]);
}
else {
printf("!! for %s\n", history[i]);
}
}
}
/* Handles the input if it is to do with aliases */
void aliasesControl(char *cmd[]) {
int i;
if (cmd[1] == NULL) {
if (aliasesSize == 0) {
printf("Aliases are empty.\n");
}
else {
for (i = 0; i < aliasesSize; i++) {
printf("%s is aliase for %s\n", aliases[i][0], aliases[i][1]);
}
}
}
else {
addAliases();
}
}
void addAliases() {
aliases[aliasesSize][0] = strdup(tokens[1]);
aliases[aliasesSize][1] = strdup(tokens[2]);
printf("%s assigned to %s.\n", aliases[aliasesSize][0], aliases[aliasesSize][1]);
aliasesSize++;
}
/* invoke aliases */
void invokeAlias(char *cmd[]) {
int i = 0;
/************************************************
* go through the array of aliases and *
* change the token[0] (the command) to the *
* alias command, were applicable. *
* *
************************************************/
for (i = 0; i < aliasesSize; i++) {
if (!strcmp(cmd[0], aliases[i][0])) {
tokens[0] = strdup(aliases[i][1]);
}
}
}
/* tokenize input */
int tokenizedInput(char input[], char *tokens[]) {
int i = 0, j;
char *token;
int len;
if (fgets(input, 512, stdin) != 0) {
len = strlen(input);
}
else {
exit(EXIT_SUCCESS);
}
token = strtok(input, " <>|\t\n");
while (token != NULL)
{
tokens[i++] = token;
token = strtok(NULL, " <>|\t\n");
}
tokens[i] = NULL;
return i;
}
/* print working directory */
int pwd() {
char buffer[MAX_INPUT];
if (tokens[1] != NULL) {
printf("pwd does not take in any variables.\n");
}
else {
getcwd(buffer, MAX_INPUT);
printf("%s\n", buffer);
}
return 0;
}
/* change directory */
int cd() {
char *temp;
if (tokens[1] == NULL) {
printf("HOME\n");
temp = getenv("HOME");
chdir(temp);
}
else if (chdir(tokens[1]) == -1) {
perror("cd");
}
else if (tokens[2] != NULL) {
printf("The command cd should only take 1 or 0 variables.\n");
}
}
void setpath() {
if (tokens[1] != NULL && tokens[2] == NULL) {
setenv("PATH", tokens[1], 1);
printf("The path has been set to %s\n", getenv("PATH"));
}
else if (tokens[2] != NULL) {
printf("The path was not set as too many variables were entered.\n");
}
else {
printf("The path was not set as no path was specified.\n");
}
}
void getpath() {
if (tokens[1] != NULL) {
printf("Cannot get path, as too many variables were entered.\n");
}
else {
printf("The path is %s\n", getenv("PATH"));
}
}
void exterCmd() {
// forks for the new execvp command
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "The fork has failed");
exit(-1);
}
else if (pid == 0) {
if (execvp(tokens[0], tokens) == -1) {
printf("Command \"%s\" not found.\n", tokens[0]);
exit(0);
}
}
else {
wait(NULL);
printf("Child process complete\n");
}
}
void exitCmd() {
// Opens the history file
FILE *f = fopen("history.txt", "wb");
// Writes the contents of the array
fwrite(history, sizeof(char), historySize, f);
fclose(f);
// Sets the Path to the home path
setenv("PATH", path, 1);
printf("The path is %s\n", getenv("PATH"));
exit(EXIT_SUCCESS);
}
void handleInput(char *tokens[]) {
if (tokens[0] != NULL) {
invokeAlias(tokens);
if (strncmp(tokens[0], "!", 1) == 0) {
invokeHist(tokens);
}
else {
addHist();
}
if (strcmp(tokens[0], "history") == 0) {
printHist();
}
else if (strcmp(tokens[0], "alias") == 0) {
aliasesControl(tokens);
}
else if (strcmp(tokens[0], "exit") == 0) {
exitCmd();
}
else if (strcmp(tokens[0], "pwd") == 0) {
pwd();
}
else if (strcmp(tokens[0], "cd") == 0) {
cd();
}
else if (strcmp(tokens[0], "setpath") == 0) {
setpath();
}
else if (strcmp(tokens[0], "getpath") == 0) {
getpath();
}
else {
exterCmd();
}
}
}
int main() {
char input[512];
path = getenv("PATH");
char *homedir = getenv("HOME");
int tokenCounter = 0;
printf(VERSION);
setup();
while (terminate == 0) {
printf("%s>$ ", homedir);
// tokenises the input and assigns it to tokens, returns size
tokenCounter = tokenizedInput(input, tokens);
// Handles the tokenized input
handleInput(tokens);
}
}