-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/**************************************************** | ||
author: gsc2001 | ||
brief: redirection handling | ||
*****************************************************/ | ||
#include "globals.h" | ||
#ifndef __REDIRECT_H | ||
#define __REDIRECT_H | ||
int stdinSave, stdoutSave, rfd, wfd; | ||
void redirectBegin(Command c); | ||
void redirectRestore(); | ||
#endif // __REDIRECT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "redirecting.h" | ||
#include "errorHandler.h" | ||
|
||
void redirectBegin(Command c) | ||
{ | ||
stdinSave = -1; | ||
stdoutSave = -1; | ||
|
||
if (c.inp) | ||
{ | ||
rfd = handleSyscallint(open(c.inp, O_RDONLY), "Error opening inp file"); | ||
if (rfd >= 0) | ||
{ | ||
stdinSave = dup(STDIN_FILENO); | ||
dup2(rfd, STDIN_FILENO); | ||
} | ||
} | ||
if (c.out) | ||
{ | ||
if (c.append) | ||
wfd = handleSyscallint(open(c.out, O_CREAT | O_WRONLY | O_APPEND, 0644), "Error opening output file"); | ||
else | ||
wfd = handleSyscallint(open(c.out, O_CREAT | O_WRONLY | O_TRUNC, 0644), "Error opening output file"); | ||
|
||
if (wfd >= 0) | ||
{ | ||
stdoutSave = dup(STDOUT_FILENO); | ||
dup2(wfd, STDOUT_FILENO); | ||
} | ||
} | ||
} | ||
|
||
void redirectRestore() | ||
{ | ||
if (stdinSave >= 0) | ||
{ | ||
dup2(stdinSave, STDIN_FILENO); | ||
close(rfd); | ||
close(stdinSave); | ||
} | ||
if (stdoutSave >= 0) | ||
{ | ||
dup2(stdoutSave, STDOUT_FILENO); | ||
close(wfd); | ||
close(stdoutSave); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters