alistair
5 years ago
8 changed files with 86 additions and 1 deletions
@ -1 +1,6 @@
@@ -1 +1,6 @@
|
||||
# chickenshell |
||||
# 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) |
||||
|
Binary file not shown.
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
|
||||
#ifndef ERRORH |
||||
#define ERRORH |
||||
|
||||
enum ExitCodes { |
||||
ER_SUCCESS, |
||||
ER_FAILURE, |
||||
ER_FAIL_TO_FORK |
||||
}; |
||||
|
||||
#endif |
@ -0,0 +1,37 @@
@@ -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; |
||||
} |
||||
|
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
#include <stdlib.h> |
||||
#include <stdbool.h> |
||||
#include <stdio.h> |
||||
#include <unistd.h> |
||||
|
||||
#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 |
Loading…
Reference in new issue