-
Notifications
You must be signed in to change notification settings - Fork 0
/
sesh.c
227 lines (182 loc) · 4.63 KB
/
sesh.c
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
/******************************************************************************
* sesh.c *
* *
* Segev Shell v1.0 - per demands from OSTEP book project *
* *
******************************************************************************/
#define _POSIX_C_SOURCE 200809L // strdup, getline
#include <stdio.h> // printf, getline
#include <stdlib.h> // free
#include <stddef.h> // typedef size_t
#include <unistd.h> // fork, execvp, chdir, getcwd
#include <sys/types.h> // fork, wait
#include <sys/wait.h> // wait
#include <string.h> // strtok, strdup, strerror
#include <errno.h>
#define MAX_ARGS 256
#define UNUSED(x) ((void)(x))
static void RunPrompt(void);
static void RunBatch(const char *batch_file);
static int ParseInput(char *input, char **parsed);
static void ExecuteCommand(int argc, char **argv);
static void FreeStringList(char **list);
static int WhichCommand(char *string);
static void ExitCommand(int argc, char **argv);
static void ChangeDirCommand(int argc, char **argv);
enum builtin_cmds
{
EXIT,
CD,
/* PATH, */
NUM_OF_BUILTIN_CMDS
};
enum bool
{
FALSE,
TRUE
};
#define FAILURE (-1)
typedef void (*builtin_cmd)(int argc, char **argv);
static int g_exit_flag = TRUE;
char *g_paths[MAX_ARGS] = {"/bin"};
char *builtin_cmds[NUM_OF_BUILTIN_CMDS] = {"exit", "cd", /* "path" */};
builtin_cmd commands[NUM_OF_BUILTIN_CMDS] =
{
ExitCommand,
ChangeDirCommand,
/* 0 */
};
int main(int argc, char const *argv[])
{
UNUSED(argv);
if (1 == argc)
{
RunPrompt();
}
else if (2 == argc)
{
RunBatch(argv[1]);
}
else
{
fprintf(stderr, "sesh: Too many arguments\n");
}
return 0;
}
static void RunPrompt(void)
{
char *line = NULL;
char *cwd = NULL;
size_t line_length = 0;
char *argv[MAX_ARGS] = {0};
int argc = 0;
while (g_exit_flag)
{
cwd = getcwd(NULL, 0);
printf("\033[1;32msesh:\033[1;34m%s\033[0m> ", cwd);
getline(&line, &line_length, stdin); // TODO - error handling
argc = ParseInput(line, argv);
ExecuteCommand(argc, argv);
FreeStringList(argv);
free(cwd);
cwd = NULL;
}
free(line);
line = NULL;
}
static void RunBatch(const char *batch_file)
{
FILE *fp = fopen(batch_file, "r");
if (NULL == fp)
{
fprintf(stderr, "sesh: batch-file error: %s\n", strerror(errno));
exit(FAILURE);
}
char *line = NULL;
size_t line_length = 0;
int argc = 0;
char *argv[MAX_ARGS] = {0};
while (FAILURE != getline(&line, &line_length, fp))
{
argc = ParseInput(line, argv);
ExecuteCommand(argc, argv);
FreeStringList(argv);
}
free(line);
line = NULL;
}
static int ParseInput(char *input, char **parsed)
{
int i = 1;
char *curr_arg = NULL;
parsed[0] = strdup(strtok(input, " \t\n"));
while ((curr_arg = strtok(NULL, " \t\n")))
{
parsed[i] = strdup(curr_arg);
++i;
}
return i;
}
static void ExecuteCommand(int argc, char **argv)
{
UNUSED(argc);
int command_number = WhichCommand(argv[0]);
if (-1 != command_number)
{
commands[command_number](argc, argv);
}
else /* TODO - check if binary exists in path directories */
{
pid_t pid = fork();
if (0 == pid)
{
execvp(argv[0], argv);
/* if reached here, exec failed - no such command exists */
fprintf(stderr, "%s: command not found\n", argv[0]);
exit(FAILURE);
}
}
wait(NULL);
}
static void FreeStringList(char **list)
{
unsigned int i = 0;
while (list[i])
{
free(list[i]);
list[i] = NULL;
++i;
}
}
static int WhichCommand(char *string)
{
int i = 0;
int command = -1;
while (i < NUM_OF_BUILTIN_CMDS && strcmp(string, builtin_cmds[i]))
{
++i;
}
if (i < NUM_OF_BUILTIN_CMDS)
{
command = i;
}
return command;
}
static void ExitCommand(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
g_exit_flag = FALSE;
}
static void ChangeDirCommand(int argc, char **argv)
{
if (2 < argc)
{
fprintf(stderr, "sesh: cd: too many arguments\n");
}
int status = chdir(argv[1]);
if (FAILURE == status)
{
fprintf(stderr, "sesh: cd: %s: %s\n", argv[1], strerror(errno));
}
}