-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilt-in.c
More file actions
169 lines (129 loc) · 3.69 KB
/
built-in.c
File metadata and controls
169 lines (129 loc) · 3.69 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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stddef.h>
#include<unistd.h>
#include<ctype.h>
#include<errno.h>
#include<token.h>
#include<built-in.h>
#include<history.h>
#include<constants.h>
//TODO: Improve error handling
enum BUILT_IN { CD, HELP, EXIT, PWD, HISTORY, PATH };
extern HISTORY_STORE *history_store;
const size_t built_in = 6;
extern char *PATH_FILE;
const char* built_ins[] = {
"cd",
"help",
"exit",
"pwd",
"history",
"path"
};
int get_builtin(char *cmd) {
int code = -1;
for(size_t i = 0; i < built_in; i++) {
if(strcmp(cmd, built_ins[i]) == 0) {
code = i;
break;
}
}
return code;
}
void builtin_handler(int code, TOKEN *token_list, size_t token_list_length) {
switch(code) {
case HELP:
help(token_list, token_list_length);
break;
case EXIT:
exit_shell(token_list, token_list_length);
break;
case HISTORY:
history_cmd(token_list, token_list_length);
break;
case PATH:
path(token_list, token_list_length);
break;
case CD:
change_dir(token_list, token_list_length);
break;
case PWD:
pwd(token_list, token_list_length);
break;
}
return;
}
void help(TOKEN *token_list, size_t token_list_length) {
//TODO: Print help text
return;
}
void history_cmd(TOKEN *token_list, size_t token_list_length) {
size_t start = 0;
if(token_list_length == 2) {
start = atoi(token_list[1].val);
if(start == 0 && (strlen(token_list[0].val) == 0 || !isdigit(token_list[0].val[0]))) {
fprintf(stdout, "A numeric argument greater than 0 is required\n");
return;
}
start = ((long long int)history_store->curr - (long long int)start >= 0) ? history_store->curr - start : 0;
}
for(size_t i = start; i < history_store->curr; i++)
fprintf(stdout, "%lu %s\n", i + 1, (history_store->store)[i]);
return;
}
void exit_shell(TOKEN *token_list, size_t token_list_length) {
if(token_list_length > 1)
fprintf(stderr, "No arguments are allowed for exit command\n");
else
exit(0);
return;
}
void path(TOKEN *token_list, size_t token_list_length) {
FILE* file_ptr = NULL;
if((file_ptr = fopen(PATH_FILE, "w")) == NULL) {
fprintf(stderr, error_msg);
return;
}
for(int i = 1; i < token_list_length; i++)
fprintf(file_ptr, "%s\n", token_list[i].val);
fclose(file_ptr);
return;
}
void change_dir(TOKEN *token_list, size_t token_list_length) {
if(token_list_length == 1)
fprintf(stderr, "Please specify a path\n");
else if(token_list_length > 2)
fprintf(stderr, "Too many arguments\n");
else if(chdir(token_list[1].val) == -1)
fprintf(stderr, "%s\n", strerror(errno));
return;
}
void pwd(TOKEN *token_list, size_t token_list_length) {
if(token_list_length > 1 || token_list[0].is_background) {
fprintf(stderr, "No arguments are allowed for exit command\n");
return;
}
//Determine the maximum PATH SIZE possible using
//pathconf()
size_t PATH_MAX = pathconf(".", _PC_PATH_MAX);
if(PATH_MAX == -1) {
fprintf(stderr, error_msg);
return;
}
char *buffer = (char *)malloc(sizeof(char)*PATH_MAX);
if(buffer == NULL) {
fprintf(stderr, error_msg);
return;
}
buffer = getcwd(buffer, PATH_MAX);
if(buffer == NULL) {
fprintf(stderr, error_msg);
return;
}
else
fprintf(stdout, "%s\n", buffer);
free(buffer);
return;
}