Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
127 lines (112 loc) · 3.43 KB
/
Copy pathmain.c
File metadata and controls
127 lines (112 loc) · 3.43 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
#include "commands.h"
char** parse_input(char *input){
size_t buffer_size=MAX_INPUT;
char **tokens = malloc(buffer_size*sizeof(char*));
char* token=NULL;
size_t position=0;
size_t token_length=0;
if(!tokens){
perror("Malloc");
exit(1);
}
for(size_t i=0; input[i]; i++){
token=&input[i];
while(input[i] == ' ' || input[i] == '\n' || input[i] == '\t' || input[i] == '\r' || input[i] == '\a'){
i++;
}
if(input[i] == '\0') break;
token = &input[i];
while (input[i] != '\0' && input[i] != ' ' && input[i] != '\n' && input[i] != '\t' && input[i] != '\r' && input[i] != '\a'){
token_length++;
i++;
}
tokens[position]=malloc((token_length+1)*sizeof(char));
if(!tokens[position]){
perror("Malloc");
exit(1);
}
for (size_t j=0; j<token_length; j++){
tokens[position][j]=token[j];
}
tokens[position][token_length]='\0';
position++;
token_length=0;
}
tokens[position] = NULL;
return tokens;
}
void free_objects(char** tokens){
if (tokens)
return;
for (size_t i = 0; tokens[i]; i++){
free(tokens[i]);
}
free(tokens);
}
void display_all_commands(){
printf("cd <directory> - Change the current directory.\n");
printf("pwd - Print the current working directory.\n");
printf("echo <text> - Print the given data.\n");
printf("env - Display all env variables.\n");
printf("setenv VAR=value - Set an env var.\n");
printf("unsetenv <variable> - Remove an env var.\n");
printf("which <command> - Locate an exec in the system's PATH.\n");
printf("help - Display this help message.\n");
printf("exit or quit - Exit shell.\n");
}
int shell_built_in(char **args, char **env, char *initial_dir){
(void) env;
(void) initial_dir;
if (strcmp(args[0], "cd") == 0) {
return command_cd(args, initial_dir);
} else if (strcmp(args[0], "pwd") == 0) {
command_pwd();
} else if (strcmp(args[0], "echo") == 0) {
command_echo(args, env);
} else if (strcmp(args[0], "env") == 0) {
command_env(env);
} else if (strcmp(args[0], "which") == 0) {
command_which(args, env);
} else if (strcmp(args[0], "exit") == 0 || strcmp(args[0], "quit") == 0) {
exit(EXIT_SUCCESS);
}
else if(strcmp(args[0],"help")==0){
display_all_commands();
}
else {
return execute(args,env);
}
return 0;
}
void call_shell_commands(char ** env){
char* input=NULL;
size_t input_size=0;
char * initial_dir=getcwd(NULL,0);
char **args;
while(1){
printf("$");
if(getline(&input, &input_size, stdin)==-1){
perror("getline");
break;
}
args=parse_input(input);
if(args[0]!=NULL && strcmp(args[0], "setenv") == 0){
env = command_setenv(args, env);
}
else if(args[0]!=NULL && strcmp(args[0], "unsetenv") == 0){
env = command_unsetenv(args, env);
}
else if(args[0]!=NULL){
shell_built_in(args,env,initial_dir);
}
else{
return;
}
}
free_objects(args);
}
int main(int argc, char** argv, char **ev){
(void)argc;
(void)argv;
call_shell_commands(ev);
}