-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsh.c
195 lines (166 loc) · 3.97 KB
/
nsh.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
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "nsh.h"
#define NSH_BUF_SIZE 1024
#define NSH_TOK_SIZE 64
#define NSH_TOK_DELIM " \t\n"
size_t buf_size;
size_t tok_size;
int main()
{
nsh_loop();
return 0;
}
char *nsh_builtin_strs[] = {
"help",
"cd",
"exit"
};
char *nsh_builtin_descs[] = {
"Usage: 'help' - displays information about the shell",
"Usage: 'cd [args]' - changes the present working directory to the path specified by args",
"Usage: 'exit' - exits the shell"
};
int (*nsh_builtin_funcs[])(char **) = {
&nsh_help,
&nsh_cd,
&nsh_exit
};
size_t nsh_num_builtins()
{
return (sizeof(nsh_builtin_strs) / sizeof(char *));
}
int nsh_cd(char **args)
{
if (!args[1]) fprintf(stderr, "nsh: expected argument to \"cd\"\n");
if (chdir(args[1])) perror("nsh");
return 1;
}
int nsh_help(char **args)
{
fprintf(stdout, "nsh: Shell developed by Nathan Abebe, 2024. Version 1.0.\n");
fprintf(stdout, "Inspired by lsh: https://github.com/brenns10/lsh\n\n");
fprintf(stdout, "The following builtin functions are supported:\n");
size_t num_builtins = nsh_num_builtins();
for (size_t i = 0; i < num_builtins; ++i)
{
fprintf(stdout, "%s%*s- %s.\n", nsh_builtin_strs[i],
(int)(7 - strlen(nsh_builtin_strs[i])), "", nsh_builtin_descs[i]);
}
return 1;
}
int nsh_exit(char **args)
{
return 0;
}
void nsh_loop()
{
int status;
char *line;
char **args;
do
{
printf("> ");
line = nsh_read(stdin);
args = nsh_tokenize(line);
status = nsh_exec(args);
free(line);
free(args);
} while (status);
}
char *nsh_read(FILE* in)
{
buf_size = NSH_BUF_SIZE;
char *buffer = malloc(buf_size * sizeof(char));
if (!buffer) nsh_alloc_error(NULL, NULL);
buffer[0] = '\0';
size_t position = 0;
char c;
while (((c = fgetc(in)) != EOF) && (c != '\n'))
{
if ((position + 1) == buf_size)
{
buf_size *= 2;
char *new_buffer = realloc(buffer, buf_size * sizeof(char));
if (!new_buffer) nsh_alloc_error(buffer, NULL);
buffer = new_buffer;
}
buffer[position++] = c;
buffer[position] = '\0';
}
return buffer;
}
char **nsh_tokenize(char *line)
{
tok_size = NSH_TOK_SIZE;
char **tokens = malloc(tok_size * sizeof(char *));
if (!tokens) nsh_alloc_error(line, NULL);
char *line_copy = line;
char *delim = NSH_TOK_DELIM;
tokens[0] = strtok(line_copy, delim);
size_t counter = 0;
while (tokens[counter] != NULL)
{
if (counter == tok_size)
{
tok_size *= 2;
char **new_tokens = realloc(tokens, tok_size * sizeof(char *));
if (!new_tokens) nsh_alloc_error(line, tokens);
tokens = new_tokens;
}
tokens[++counter] = strtok(NULL, delim);
}
return tokens;
}
int nsh_exec(char **args)
{
if (args[0] == NULL) return 1;
size_t num_builtins = nsh_num_builtins();
for (size_t i = 0; i < num_builtins; ++i)
{
if (!strcmp(args[0], nsh_builtin_strs[i]))
{
return (*nsh_builtin_funcs[i])(args);
}
}
return nsh_launch(args);
}
int nsh_launch(char **args)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0)
// child process
{
if (execvp(args[0], args) == -1)
{
perror("nsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
// error forking
{
perror("nsh");
}
else
// parent process
{
do
{
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
void nsh_alloc_error(void *ptr1, void *ptr2)
{
free(ptr1);
free(ptr2);
fprintf(stderr, "nsh: allocation error\n");
exit(EXIT_FAILURE);
}