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
|
/* nosh - noshell
* ~100+ lines of pure POSIX and standard C code for thy eyes!
*
* von czjstmax, <jstmaxlol at disroot dot org>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <wordexp.h>
#include <sys/wait.h>
void handlecc(int sig);
char *line = NULL;
size_t len = 0;
wordexp_t p;
int main(void)
{
signal(SIGINT, handlecc);
while (true) {
printf("$ ");
getline(&line, &len, stdin);
line[strcspn(line, "\n")] = '\0';
wordexp(line, &p, 0);
// debug
// for (size_t i = 0; i < p.we_wordc; i++) {
// printf("argv[%zu] = %s\n", i, p.we_wordv[i]);
// }
if (strlen(line) == 0) {
wordfree(&p);
continue; //otherwise.. SIGSEGV
}
if (strcmp(p.we_wordv[0], ":q") == 0 || strcmp(p.we_wordv[0], "exit") == 0) {
wordfree(&p);
free(line);
return 0;
}
else if (strcmp(p.we_wordv[0], "echo") == 0) {
for (size_t i = 1; i < p.we_wordc; i++)
printf("%s ", p.we_wordv[i]);
if (p.we_wordc >= 2) {
if (strcmp(p.we_wordv[1], "-c") != 0)
printf("\n");
else continue;
}
}
else if (strcmp(p.we_wordv[0], "cd") == 0) {
if (p.we_wordc <= 1) {
printf("nsh! cd: no arguments were given.\n");
wordfree(&p);
continue;
}
if (chdir(p.we_wordv[1]))
perror("nsh! ERROR: failed to change directory");
}
else if (strcmp(p.we_wordv[0], "clear") == 0) {
printf("\033[2J\033[H\033[3J");
fflush(stdout);
}
else if (strcmp(p.we_wordv[0], "help") == 0) {
printf(
"nosh - noshell\na minimal POSIX C shell for *NIX dudes.\n\nbuilt-ins:\n"
"echo -> outputs a string of text, doesn't need \" enclosure.\n"
" supports '-c' for output without '\\n'"
"clear -> clears screen (and scrollback)\n"
"cd -> change working directory\n"
"help -> prints this screen\n"
"\ngithub repo: github.com/jstmaxlol/nosh\n"
);
}
else {
pid_t pid = fork();
char **argv = p.we_wordv; // pray with me now
if (pid == 0) {
execvp(p.we_wordv[0], argv);
perror("nsh! ERROR: failed to execvp()");
_exit(1);
}
else if (pid > 0) {
int status;
waitpid(pid, &status, 0);
}
else {
printf("nsh! couldn't open %s", p.we_wordv[0]);
}
}
}
}
void handlecc(int sig)
{
printf("\nnsh! %d caught.\nfreeing stuff before quitting.\n", sig);
exit(0);
}
|