Browse Source

execute

master
alistair 5 years ago
parent
commit
4338d357eb
  1. 4
      Makefile
  2. 7
      README.md
  3. BIN
      chicken
  4. 5
      error.h
  5. 2
      main.c
  6. 37
      run.c
  7. 13
      run.h

4
Makefile

@ -17,3 +17,7 @@ run: run.c run.h @@ -17,3 +17,7 @@ run: run.c run.h
util: util.c util.h
$(GCC) -c util.c
clean:
rm chicken
rm *.o

7
README.md

@ -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)

BIN
chicken

Binary file not shown.

5
error.h

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
enum ExitCodes {
FAIL_TO_FORK
};

2
main.c

@ -1,6 +1,4 @@ @@ -1,6 +1,4 @@
int main(int argc, char** argv) {
return 0;
}

37
run.c

@ -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.
*/
bool execute(FILE *in, FILE *out, char *args[]) {
int in_FD = fileno(in);
int out_FD = fileno(out);
int err = fork();
if (err == -1) {
return true;
}
if (err) { // parent
return false;
} else { // child
dup2(in_FD, 0);
dup2(out_FD, 1);
execvp(args[0], args);
}
return false;
}
int test_exec(void) {
char *arg[] = {"ls", "lah", 0};
execute(stdin, stdout, arg);
return 0;
}

13
run.h

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "error.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.
*/
bool execute(FILE* in, FILE* out, char *args[]);
Loading…
Cancel
Save