diff options
Diffstat (limited to '')
-rw-r--r-- | src/process.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/process.cpp b/src/process.cpp new file mode 100644 index 0000000..8fe98f3 --- /dev/null +++ b/src/process.cpp | |||
@@ -0,0 +1,146 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "process.h" | ||
9 | #include <sys/types.h> | ||
10 | #include <unistd.h> | ||
11 | #include <stdarg.h> | ||
12 | |||
13 | Bu::Process::Process( const char *sName, char *const argv[] ) | ||
14 | { | ||
15 | gexec( sName, argv ); | ||
16 | } | ||
17 | |||
18 | Bu::Process::Process( const char *sName, const char *argv, ...) | ||
19 | { | ||
20 | int iCnt = 0; | ||
21 | va_list ap; | ||
22 | va_start( ap, argv ); | ||
23 | for(; va_arg( ap, const char *); iCnt++ ); | ||
24 | va_end( ap ); | ||
25 | |||
26 | char const **list = new char const *[iCnt+2]; | ||
27 | va_start( ap, argv ); | ||
28 | list[0] = argv; | ||
29 | for( int j = 1; j < iCnt; j++ ) | ||
30 | list[j] = va_arg( ap, const char *); | ||
31 | list[iCnt+1] = NULL; | ||
32 | va_end( ap ); | ||
33 | |||
34 | gexec( sName, (char *const *)list ); | ||
35 | delete[] list; | ||
36 | } | ||
37 | |||
38 | Bu::Process::~Process() | ||
39 | { | ||
40 | } | ||
41 | |||
42 | void Bu::Process::gexec( const char *sName, char *const argv[] ) | ||
43 | { | ||
44 | int iaStdIn[2]; | ||
45 | int iaStdOut[2]; | ||
46 | int iaStdErr[2]; | ||
47 | pipe( iaStdIn ); | ||
48 | pipe( iaStdOut ); | ||
49 | pipe( iaStdErr ); | ||
50 | |||
51 | iStdIn = iaStdIn[1]; | ||
52 | iStdOut = iaStdOut[0]; | ||
53 | iStdErr = iaStdErr[0]; | ||
54 | |||
55 | iPid = fork(); | ||
56 | if( iPid == 0 ) | ||
57 | { | ||
58 | ::close( iaStdIn[1] ); | ||
59 | ::close( iaStdOut[0] ); | ||
60 | ::close( iaStdErr[0] ); | ||
61 | dup2( iaStdIn[0], 0 ); | ||
62 | dup2( iaStdOut[1], 1 ); | ||
63 | dup2( iaStdErr[1], 2 ); | ||
64 | execvp( sName, argv ); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | void Bu::Process::close() | ||
69 | { | ||
70 | } | ||
71 | |||
72 | size_t Bu::Process::read( void *pBuf, size_t nBytes ) | ||
73 | { | ||
74 | return ::read( iStdOut, pBuf, nBytes ); | ||
75 | } | ||
76 | |||
77 | size_t Bu::Process::write( const void *pBuf, size_t nBytes ) | ||
78 | { | ||
79 | return ::write( iStdIn, pBuf, nBytes ); | ||
80 | } | ||
81 | |||
82 | long Bu::Process::tell() | ||
83 | { | ||
84 | return 0; | ||
85 | } | ||
86 | |||
87 | void Bu::Process::seek( long offset ) | ||
88 | { | ||
89 | } | ||
90 | |||
91 | void Bu::Process::setPos( long pos ) | ||
92 | { | ||
93 | } | ||
94 | |||
95 | void Bu::Process::setPosEnd( long pos ) | ||
96 | { | ||
97 | } | ||
98 | |||
99 | bool Bu::Process::isEOS() | ||
100 | { | ||
101 | return false; | ||
102 | } | ||
103 | |||
104 | bool Bu::Process::isOpen() | ||
105 | { | ||
106 | return true; | ||
107 | } | ||
108 | |||
109 | void Bu::Process::flush() | ||
110 | { | ||
111 | } | ||
112 | |||
113 | bool Bu::Process::canRead() | ||
114 | { | ||
115 | return true; | ||
116 | } | ||
117 | |||
118 | bool Bu::Process::canWrite() | ||
119 | { | ||
120 | return true; | ||
121 | } | ||
122 | |||
123 | bool Bu::Process::isReadable() | ||
124 | { | ||
125 | return true; | ||
126 | } | ||
127 | |||
128 | bool Bu::Process::isWritable() | ||
129 | { | ||
130 | return true; | ||
131 | } | ||
132 | |||
133 | bool Bu::Process::isSeekable() | ||
134 | { | ||
135 | return false; | ||
136 | } | ||
137 | |||
138 | bool Bu::Process::isBlocking() | ||
139 | { | ||
140 | return true; | ||
141 | } | ||
142 | |||
143 | void Bu::Process::setBlocking( bool bBlocking ) | ||
144 | { | ||
145 | } | ||
146 | |||