Browse Source

error coding

master
alistair 5 years ago
parent
commit
2801214296
  1. 8
      error.h
  2. 6
      main.c
  3. 10
      run.c
  4. 7
      run.h
  5. 7
      util.h

8
error.h

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

6
main.c

@ -1,4 +1,8 @@ @@ -1,4 +1,8 @@
#include "error.h"
int main(int argc, char** argv) {
return 0;
return ER_SUCCESS;
}

10
run.c

@ -5,26 +5,26 @@ @@ -5,26 +5,26 @@
*
* Returns true on success or 1 on failure.
*/
bool execute(FILE *in, FILE *out, char *args[]) {
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 true;
return ER_FAIL_TO_FORK;
}
if (err) { // parent
return false;
return ER_SUCCESS;
} else { // child
dup2(in_FD, 0);
dup2(out_FD, 1);
execvp(args[0], args);
err = execvp(args[0], args);
}
return false;
return ER_SUCCESS;
}
int test_exec(void) {

7
run.h

@ -5,9 +5,14 @@ @@ -5,9 +5,14 @@
#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.
*/
bool execute(FILE* in, FILE* out, char *args[]);
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