diff --git a/Makefile b/Makefile index f068311..90062b1 100644 --- a/Makefile +++ b/Makefile @@ -17,3 +17,7 @@ run: run.c run.h util: util.c util.h $(GCC) -c util.c + +clean: + rm chicken + rm *.o diff --git a/README.md b/README.md index b124d99..7f11ffd 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -# chickenshell \ No newline at end of file +# chickenshell + + +## Reference + +[https://github.com/danistefanovic/build-your-own-x#build-your-own-shell](https://github.com/danistefanovic/build-your-own-x#build-your-own-shell) diff --git a/chicken b/chicken deleted file mode 100755 index 3bb43d9..0000000 Binary files a/chicken and /dev/null differ diff --git a/error.h b/error.h new file mode 100644 index 0000000..e1ef4ec --- /dev/null +++ b/error.h @@ -0,0 +1,11 @@ + +#ifndef ERRORH +#define ERRORH + +enum ExitCodes { + ER_SUCCESS, + ER_FAILURE, + ER_FAIL_TO_FORK +}; + +#endif diff --git a/main.c b/main.c index 3da2818..b72276b 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,8 @@ #include #include +#include "error.h" + #define INITIALLINEBUFFER 80 char *readline(FILE* in) { @@ -46,4 +48,5 @@ int main(int argc, char** argv) { exit(0); + return ER_SUCCESS; } diff --git a/run.c b/run.c index e69de29..9b47d2f 100644 --- a/run.c +++ b/run.c @@ -0,0 +1,37 @@ +#include "run.h" + +/* Fork and exec a command given in args taking input from in and sending + * output to out. The arguments array must end in a NULL. + * + * Returns true on success or 1 on failure. + */ +int execute(FILE *in, FILE *out, char *args[]) { + int in_FD = fileno(in); + int out_FD = fileno(out); + + int err = fork(); + + if (err == -1) { + return ER_FAIL_TO_FORK; + } + + if (err) { // parent + return ER_SUCCESS; + } else { // child + dup2(in_FD, 0); + dup2(out_FD, 1); + + err = execvp(args[0], args); + } + + return ER_SUCCESS; +} + +int test_exec(void) { + char *arg[] = {"ls", "lah", 0}; + + execute(stdin, stdout, arg); + + return 0; +} + diff --git a/run.h b/run.h index e69de29..4e3f57e 100644 --- a/run.h +++ b/run.h @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +#include "error.h" + +#ifndef RUNH +#define RUNH + +/* Fork and exec a command given in args taking input from in and sending + * output to out. The arguments array must end in a NULL. + * + * Returns true on success or 1 on failure. + */ +int execute(FILE* in, FILE* out, char *args[]); + +#endif diff --git a/util.h b/util.h index e69de29..2e38fc4 100644 --- a/util.h +++ b/util.h @@ -0,0 +1,7 @@ + +#ifndef UTILH +#define UTILH + + + +#endif