static int handle_redirection(const char *cmd, bool in, bool out, bool err) { if (!cmd || cmd[0] == '\0') return 0; if (cmd[0] == '!') { // redirection to a process return handle_redirection_proc (cmd + 1, in, out, err); } else { // redirection to a file int f, flag = 0, mode = 0; flag |= in ? O_RDONLY : 0; flag |= out ? O_WRONLY | O_CREAT : 0; flag |= err ? O_WRONLY | O_CREAT : 0; #ifdef __WINDOWS__ mode = _S_IREAD | _S_IWRITE; #else mode = S_IRUSR | S_IWUSR; #endif f = open (cmd, flag, mode); if (f < 0) { eprintf("Cannot open: %s\n", cmd); return 1; } if (in) dup2 (f, 0); if (out) dup2 (f, 1); if (err) dup2 (f, 2); close (f); return 0; } }
static int handle_redirection(const char *cmd, bool in, bool out, bool err) { if (!cmd || cmd[0] == '\0') return 0; if (cmd[0] == '"') { #if __UNIX__ if (in) { int pipes[2]; if (pipe (pipes) != -1) { write (pipes[1], cmd+1, strlen (cmd)-2); write (pipes[1], "\n", 1); close (0); dup2 (pipes[0], 0); } else { eprintf ("[ERROR] rarun2: Cannot create pipe\n"); } } #else #warning quoted string redirection handle not yet done #endif return 0; } else if (cmd[0] == '!') { // redirection to a process return handle_redirection_proc (cmd + 1, in, out, err); } else { // redirection to a file int f, flag = 0, mode = 0; flag |= in ? O_RDONLY : 0; flag |= out ? O_WRONLY | O_CREAT : 0; flag |= err ? O_WRONLY | O_CREAT : 0; #ifdef __WINDOWS__ mode = _S_IREAD | _S_IWRITE; #else mode = S_IRUSR | S_IWUSR; #endif f = open (cmd, flag, mode); if (f < 0) { eprintf ("[ERROR] rarun2: Cannot open: %s\n", cmd); return 1; } #define DUP(x) { close(x); dup2(f,x); } if (in) DUP(0); if (out) DUP(1); if (err) DUP(2); close (f); return 0; } }
static int handle_redirection(const char *cmd, bool in, bool out, bool err) { if (!cmd || cmd[0] == '\0') { return 0; } #if __APPLE__ && !__POWERPC__ //XXX handle this in other layer since things changes a little bit //this seems like a really good place to refactor stuff return 0; #endif if (cmd[0] == '"') { #if __UNIX__ if (in) { int pipes[2]; if (pipe (pipes) != -1) { size_t cmdl = strlen (cmd)-2; if (write (pipes[1], cmd + 1, cmdl) != cmdl) { eprintf ("[ERROR] rarun2: Cannot write to the pipe\n"); close (0); return 1; } if (write (pipes[1], "\n", 1) != 1) { eprintf ("[ERROR] rarun2: Cannot write to the pipe\n"); close (0); return 1; } close (0); dup2 (pipes[0], 0); } else { eprintf ("[ERROR] rarun2: Cannot create pipe\n"); } } #else #ifdef _MSC_VER #pragma message ("string redirection handle not yet done") #else #warning quoted string redirection handle not yet done #endif #endif return 0; } else if (cmd[0] == '!') { // redirection to a process return handle_redirection_proc (cmd + 1, in, out, err); } else { // redirection to a file int f, flag = 0, mode = 0; flag |= in ? O_RDONLY : 0; flag |= out ? O_WRONLY | O_CREAT : 0; flag |= err ? O_WRONLY | O_CREAT : 0; #ifdef __WINDOWS__ mode = _S_IREAD | _S_IWRITE; #else mode = S_IRUSR | S_IWUSR; #endif f = open (cmd, flag, mode); if (f < 0) { eprintf ("[ERROR] rarun2: Cannot open: %s\n", cmd); return 1; } #define DUP(x) { close(x); dup2(f,x); } if (in) { DUP(0); } if (out) { DUP(1); } if (err) { DUP(2); } close (f); return 0; } }