Make WvStreams compile with gcc 4.4.
[wvstreams.git] / utils / wvsystem.cc
blob6bb5136bb9e9c42c3e30f62e1e88bda577377dd2
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * A more convenient wrapper around WvSubProc. See wvsystem.h.
6 */
7 #include "wvsystem.h"
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
12 WvSystem::~WvSystem()
14 go();
18 void WvSystem::init(const char * const *argv)
20 started = false;
21 WvSubProc::preparev(argv[0], argv);
25 // open a given filename or device, making sure it has the given fd. If
26 // there is an open file on that fd already, it gets closed.
27 static void fd_open(int fd, WvStringParm file, int mode)
29 ::close(fd);
30 int nfd = ::open(file, mode, 0666);
31 if (nfd < 0)
32 return;
33 if (nfd != fd)
35 ::dup2(nfd, fd);
36 ::close(nfd);
41 // overrides WvSubProc::fork().
42 int WvSystem::fork(int *waitfd)
44 int pid = WvSubProc::fork(waitfd);
45 if (!pid) // child
47 if (!fdfiles[0].isnull())
48 fd_open(0, fdfiles[0], O_RDONLY);
49 if (!fdfiles[1].isnull())
50 fd_open(1, fdfiles[1], O_WRONLY|O_CREAT);
51 if (!fdfiles[2].isnull())
52 fd_open(2, fdfiles[2], O_WRONLY|O_CREAT);
55 return pid;
59 int WvSystem::go()
61 if (!started)
63 WvSubProc::start_again();
64 started = true;
66 WvSubProc::wait(-1, false);
67 return WvSubProc::estatus;
71 WvSystem &WvSystem::infile(WvStringParm filename)
73 fdfiles[0] = filename;
74 return *this;
78 WvSystem &WvSystem::outfile(WvStringParm filename)
80 fdfiles[1] = filename;
81 return *this;
85 WvSystem &WvSystem::errfile(WvStringParm filename)
87 fdfiles[2] = filename;
88 return *this;