-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomshell.c
More file actions
executable file
·386 lines (334 loc) · 8.42 KB
/
customshell.c
File metadata and controls
executable file
·386 lines (334 loc) · 8.42 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h> // Used for cmd_date
#include <grp.h> // Used to retrieve group related info
#include <sys/types.h> // Used for gid, uid
#include <limits.h>
#define NUM_GROUPS_MAX 10 // Maximum number of groups to be display for cmd_userinfo()
/*
Function Declarations for builtin shell commands:
*/
int cmd_cd(char **args);
int cmd_help(char **args);
int cmd_exit(char **args);
int cmd_pwd(char **args);
int cmd_date(char **args);
int cmd_ifconfig(char **args);
int cmd_userinfo(char **args);
void cshell_intro();
/*
List of builtin commands, followed by their corresponding functions.
*/
char *builtin_str[] = {
"cd",
"help",
"exit",
"pw",
"dt",
"ifc",
"ud"
};
int (*builtin_func[]) (char **) = {
&cmd_cd,
&cmd_help,
&cmd_exit,
&cmd_pwd,
&cmd_date,
&cmd_ifconfig,
&cmd_userinfo
};
int cmd_num_builtins() {
return sizeof(builtin_str) / sizeof(char *);
}
/*
Builtin function implementations.
*/
/**
@brief - Introduction to CustomShell
@param - n/a
@return Alawys returns 1, to continue executing
*/
void cshell_intro()
{
printf("======= CUSTOM SHELL INITITATED =======\n");
printf("Type 'help' to get a list of allowed commands\n");
printf("======= CUSTOM SHELL INITITATED =======\n");
}
/**
@brief - Allows user to use the 'pwd' command
@param - checks if additional parameters are entered and negates them
@return Alawys returns 1, to continue executing
*/
int cmd_pwd(char **args)
{
if (args[1] == NULL) { // Check if command tail provided
// run pwd command
system("pwd");
}
else {
fprintf(stderr, "chsell: unexpected argument to \"pw\"\n");
}
return 1;
}
/**
@brief - Displays current date in the format YYYY-MM-DD-HH-MM-SS
@param checks if additional parameters are entered and negates them
@return Alawys returns 1, to continue executing
*/
int cmd_date(char **args)
{
// Check to see a command tail is provided
if (args[1] == NULL) {
time_t cur_time;
struct tm *tmp;
char buffer[64];
time(&cur_time);
tmp = localtime(&cur_time);
strftime(buffer, 64,"Time and Date(YYYY-MM-DD-HH-MM-SS): %Y%m%d%H%M%S", tmp);
printf("%s\n", buffer);
} else {
fprintf(stderr, "cshell: unexpected argument to \"pw\"\n");
}
return 1;
}
/**
@brief - Displays the ip configuration for Eth0 by default, accepts other Eth ports
@param - Accepts ports but displays eth0 by default
@return Alawys returns 1, to continue executing
*/
int cmd_ifconfig(char **args)
{
if (args[1] == NULL)
system("ifconfig eth0"); // Run command 'ifconfig eth0'
else {
if (args[1] !=NULL) { // Checks if command tail is provided
char ifc_ethx[100]; // New var to hold specific ifc ethX selection
sprintf(ifc_ethx, "ifconfig %s",args[1]);
system(ifc_ethx); // Run command if additional argument added
}
}
return 1;
}
/**
@brief - Displays UserID, GroupID, username, groupname and Inode of User home directory
@param
@return Alawys returns 1, to continue executing
*/
int cmd_userinfo(char **args)
{
if (args[1] == NULL) {
struct group *gr;
int num_groups, i;
gid_t groups[NUM_GROUPS_MAX];
num_groups = NUM_GROUPS_MAX;
if( getgrouplist( getlogin(), getegid(), groups, &num_groups) == -1)
printf("Group array is too small: %d\n", num_groups); // Error check for group size
// Display UserId and GroupID
printf("%d, %s," , geteuid(), getenv("LOGNAME"));
// Display all groups registered to this User
for(i=0; i< num_groups; i++) {
gr = getgrgid(groups[i]);
printf(" %s,", gr->gr_name);
}
// Display inode of home director
system("ls -id");
}
else {
fprintf(stderr, "chsell: unexpected argument to \"uid\"\n");
}
}
/**
@brief Bultin command: change directory.
@param args List of args. args[0] is "cd". args[1] is the directory.
@return Always returns 1, to continue executing.
*/
int cmd_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "cshell: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
fprintf(stderr, "cshell: unexpected directory parameter used.\n");
}
}
return 1;
}
/**
@brief Builtin command: print help.
@param args List of args. Not examined.
@return Always returns 1, to continue executing.
*/
int cmd_help(char **args)
{
int i;
printf("\n======= HELP SECTION =======\n");
printf("These are the following commands that can be used by this CustomShell\n");
for (i = 0; i < cmd_num_builtins(); i++) {
printf(" %s\n", builtin_str[i]);
}
printf("Use the man command for information on other programs.");
printf("\n======= HELP SECTION =======\n");
return 1;
}
/**
@brief Builtin command: exit.
@param args List of args. Not examined.
@return Always returns 0, to terminate execution.
*/
int cmd_exit(char **args)
{
return 0;
}
/**
@brief Launch a program and wait for it to terminate.
@param args Null terminated list of arguments (including program).
@return Always returns 1, to continue execution.
*/
int cshell_launch(char **args)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) {
perror("cshell");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("cshell");
} else {
// Parent process
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
/**
@brief Execute shell built-in or launch program.
@param args Null terminated list of arguments.
@return 1 if the shell should continue running, 0 if it should terminate
*/
int cshell_execute(char **args)
{
int i;
if (args[0] == NULL) {
// An empty command was entered.
return 1;
}
for (i = 0; i < cmd_num_builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
return (*builtin_func[i])(args);
}
}
return cshell_launch(args);
}
#define CSHELL_RL_BUFSIZE 1024
/**
@brief Read a line of input from stdin.
@return The line from stdin.
*/
char *cshell_read_line(void)
{
int bufsize = CSHELL_RL_BUFSIZE;
int position = 0;
char *buffer = malloc(sizeof(char) * bufsize);
int c;
// Error check for memory allocation
if (!buffer) {
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += CSHELL_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}
#define CSHELL_TOK_BUFSIZE 64
#define CSHELL_TOK_DELIM " \t\r\n\a"
/**
@brief Split a line into tokens (very naively).
@param line The line.
@return Null-terminated array of tokens.
*/
char **cshell_split_line(char *line)
{
int bufsize = CSHELL_TOK_BUFSIZE, position = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;
// Error check for token allocation
if (!tokens) {
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, CSHELL_TOK_DELIM);
while (token != NULL) {
tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += CSHELL_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens) {
fprintf(stderr, "cshell: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, CSHELL_TOK_DELIM);
}
tokens[position] = NULL;
return tokens;
}
/**
@brief Loop getting input and executing it.
*/
void cshell_loop(void)
{
char *line;
char **args;
int status;
do {
printf("CustomShell> ");
line = cshell_read_line();
args = cshell_split_line(line);
status = cshell_execute(args);
free(line);
free(args);
} while (status);
}
/**
@brief Main entry point.
@param argc Argument count.
@param argv Argument vector.
@return status code
*/
int main(int argc, char **argv)
{
// Run command loop.
system("clear");
cshell_intro();
cshell_loop();
// Perform any shutdown/cleanup.
return EXIT_SUCCESS;
}