Browse Source

merge errors

master
alistair 5 years ago
parent
commit
6d8390a760
  1. 4
      Makefile
  2. 7
      README.md
  3. BIN
      chicken
  4. 11
      error.h
  5. 3
      main.c
  6. 37
      run.c
  7. 18
      run.h
  8. 7
      util.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.

11
error.h

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
#ifndef ERRORH
#define ERRORH
enum ExitCodes {
ER_SUCCESS,
ER_FAILURE,
ER_FAIL_TO_FORK
};
#endif

3
main.c

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdbool.h>
#include "error.h"
#define INITIALLINEBUFFER 80
char *readline(FILE* in) {
@ -46,4 +48,5 @@ int main(int argc, char** argv) { @@ -46,4 +48,5 @@ int main(int argc, char** argv) {
exit(0);
return ER_SUCCESS;
}

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.
*/
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;
}

18
run.h

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

7
util.h

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
#ifndef UTILH
#define UTILH
#endif
Loading…
Cancel
Save