00001
00002
00003
00004
00005
00006
00007 #include "wvsystem.h"
00008 #include <unistd.h>
00009 #include <fcntl.h>
00010 #include <sys/types.h>
00011
00012 WvSystem::~WvSystem()
00013 {
00014 go();
00015 }
00016
00017
00018 void WvSystem::init(const char * const *argv)
00019 {
00020 started = false;
00021 WvSubProc::preparev(argv[0], argv);
00022 }
00023
00024
00025
00026
00027 static void fd_open(int fd, WvStringParm file, int mode)
00028 {
00029 ::close(fd);
00030 int nfd = ::open(file, mode, 0666);
00031 if (nfd < 0)
00032 return;
00033 if (nfd != fd)
00034 {
00035 ::dup2(nfd, fd);
00036 ::close(nfd);
00037 }
00038 }
00039
00040
00041
00042 int WvSystem::fork(int *waitfd)
00043 {
00044 int pid = WvSubProc::fork(waitfd);
00045 if (!pid)
00046 {
00047 if (!fdfiles[0].isnull())
00048 fd_open(0, fdfiles[0], O_RDONLY);
00049 if (!fdfiles[1].isnull())
00050 fd_open(1, fdfiles[1], O_WRONLY|O_CREAT);
00051 if (!fdfiles[2].isnull())
00052 fd_open(2, fdfiles[2], O_WRONLY|O_CREAT);
00053 }
00054
00055 return pid;
00056 }
00057
00058
00059 int WvSystem::go()
00060 {
00061 if (!started)
00062 {
00063 WvSubProc::start_again();
00064 started = true;
00065 }
00066 WvSubProc::wait(-1, false);
00067 return WvSubProc::estatus;
00068 }
00069
00070
00071 WvSystem &WvSystem::infile(WvStringParm filename)
00072 {
00073 fdfiles[0] = filename;
00074 return *this;
00075 }
00076
00077
00078 WvSystem &WvSystem::outfile(WvStringParm filename)
00079 {
00080 fdfiles[1] = filename;
00081 return *this;
00082 }
00083
00084
00085 WvSystem &WvSystem::errfile(WvStringParm filename)
00086 {
00087 fdfiles[2] = filename;
00088 return *this;
00089 }
00090
00091