This commit is contained in:
2017-08-25 13:44:24 +02:00
commit fd44eaed3f
10 changed files with 163 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bin/

17
Makefile Normal file
View File

@@ -0,0 +1,17 @@
TARGET=s
BUILDDIR=bin/
PREFIX=/usr/local/bin/
SOURCES=$(wildcard src/*.c)
MAIN=main.c
override LDFLAGS+=-ledit
override CFLAGS+=-Werror -Wall -g -fPIC -O2 -DNDEBUG -ftrapv -Wfloat-equal -Wundef -Wwrite-strings -Wuninitialized -pedantic -std=c11
all: main.c
mkdir -p $(BUILDDIR)
$(CC) $(MAIN) $(SOURCES) -o $(BUILDDIR)$(TARGET) $(CFLAGS) $(LDFLAGS)
install: all
install $(BUILDDIR)$(TARGET) $(PREFIX)$(TARGET)
uninstall:
rm -rf $(PREFIX)$(TARGET)

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# s
A minimal shell.

7
main.c Normal file
View File

@@ -0,0 +1,7 @@
#include "src/shell.h"
int main(int argc, char** argv) {
s_loop();
return 0;
}

21
src/builtins.c Normal file
View File

@@ -0,0 +1,21 @@
#include "builtins.h"
const char* s_builtin_names[] = {"cd", "exit"};
s_builtin s_builtins[] = {&s_cd, &s_exit};
int s_cd(char** args) {
if (!args[1]) {
s_error("cd", "expected argument");
} else {
if (chdir(args[1])) perror("s");
}
return 1;
}
int s_exit(char** args) {
return 0;
}
int s_builtins_count() {
return sizeof(s_builtin_names) / sizeof(const char *);
}

12
src/builtins.h Normal file
View File

@@ -0,0 +1,12 @@
#include <unistd.h>
#include "util.h"
typedef int (*s_builtin)(char**);
int s_cd(char**);
int s_exit(char**);
int s_builtins_count();
extern const char* s_builtin_names[];
extern s_builtin s_builtins[];

84
src/shell.c Normal file
View File

@@ -0,0 +1,84 @@
#include "shell.h"
void s_die(const char* msg) {
fprintf(stderr, "s: %s\n", msg);
exit(1);
}
int s_do(char **args) {
pid_t pid, wpid;
int s;
pid = fork();
if (!pid) {
if (execvp(args[0], args) == -1) perror("lsh");
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("lsh");
} else {
do {
wpid = waitpid(pid, &s, WUNTRACED);
} while (!WIFEXITED(s) && !WIFSIGNALED(s));
}
return 1;
}
char** s_prompt() {
char* line = readline("> ");
int bs = 64;
int pos = 0;
char **tokens = malloc(bs * sizeof(char*));
char *token;
if (!line) return NULL;
add_history(line);
if (!tokens) s_die("allocation error");
token = strtok(line, " \t\r\n\a");
while (token) {
tokens[pos++] = token;
if (pos >= bs) {
bs += 64;
tokens = realloc(tokens, bs * sizeof(char*));
if (!tokens) s_die("allocation error");
}
token = strtok(NULL, " \t\r\n\a");
}
tokens[pos] = NULL;
free(line);
return tokens;
}
int s_exec(char** args) {
int i;
if (!args[0]) return 1;
for (i = 0; i < s_builtins_count(); i++) {
if (!strcmp(args[0], s_builtin_names[i])) return (*s_builtins[i])(args);
}
return s_do(args);
}
void s_loop() {
char** cmd;
int s;
do {
cmd = s_prompt();
if(!cmd) return;
s = s_exec(cmd);
free(cmd);
} while(s);
}

10
src/shell.h Normal file
View File

@@ -0,0 +1,10 @@
#include <stdlib.h>
#include <string.h>
#include <editline/readline.h>
#include "builtins.h"
void s_loop();
char** s_prompt();
int s_exec(char**);

5
src/util.c Normal file
View File

@@ -0,0 +1,5 @@
#include "util.h"
void s_error(const char* cmd, const char* msg) {
fprintf(stderr, "%s: %s\n", cmd, msg);
}

3
src/util.h Normal file
View File

@@ -0,0 +1,3 @@
#include <stdio.h>
void s_error(const char*, const char*);