summaryrefslogtreecommitdiff
path: root/src/process.cpp
blob: e71b782d18774e49f005682668ac6cb5c0d8015f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "process.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdarg.h>
#include <signal.h>
#include <fcntl.h>

Bu::Process::Process( const char *sName, char *const argv[] )
{
	gexec( sName, argv );
}

Bu::Process::Process( const char *sName, const char *argv, ...)
{
	int iCnt = 0;
	va_list ap;
	va_start( ap, argv );
	for(; va_arg( ap, const char *); iCnt++ );
	va_end( ap );

	char const **list = new char const *[iCnt+2];
	va_start( ap, argv );
	list[0] = argv;
	for( int j = 1; j <= iCnt; j++ )
	{
		list[j] = va_arg( ap, const char *);
	}
	list[iCnt+1] = NULL;
	va_end( ap );

	gexec( sName, (char *const *)list );
	delete[] list;
}

Bu::Process::~Process()
{
}

void Bu::Process::gexec( const char *sName, char *const argv[] )
{
	int iaStdIn[2];
	int iaStdOut[2];
//	int iaStdErr[2];
	pipe( iaStdIn );
	pipe( iaStdOut );
//	pipe( iaStdErr );

	iStdIn = iaStdIn[1];
	iStdOut = iaStdOut[0];
//	iStdErr = iaStdErr[0];

//	fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )|O_NONBLOCK );

	iPid = fork();
	if( iPid == 0 )
	{
		::close( iaStdIn[1] );
		::close( iaStdOut[0] );
//		::close( iaStdErr[0] );
		dup2( iaStdIn[0], 0 );
		dup2( iaStdOut[1], 1 );
//		dup2( iaStdErr[1], 2 );
		execvp( sName, argv );
	}
	::close( iaStdIn[0] );
	::close( iaStdOut[1] );
//	::close( iaStdErr[1] );
}

void Bu::Process::close()
{
}

size_t Bu::Process::read( void *pBuf, size_t nBytes )
{
	size_t iTotal = 0;
	for(;;)
	{
		size_t iRet = ::read( iStdOut, (char *)pBuf+iTotal, nBytes-iTotal );
		if( iRet == 0 )
			return iTotal;
		iTotal += iRet;
		if( iTotal == nBytes )
			return iTotal;
	}
	/*
	size_t iTotal = 0;
	fd_set rfs;
	FD_ZERO( &rfs );
	for(;;)
	{
		if( waitpid( iPid, NULL, WNOHANG ) )
		{
			printf("!!!wait failed!\n");
			size_t iRet = ::read( iStdOut, (char *)pBuf+iTotal,
					nBytes-iTotal );
			iTotal += iRet;
			return iTotal;
		}

		FD_SET( iStdOut, &rfs );
		select( iStdOut+1, &rfs, NULL, &rfs, NULL );
		size_t iRet = ::read( iStdOut, (char *)pBuf+iTotal, nBytes-iTotal );
		printf("--read=%d / %d--\n", iRet, iTotal+iRet );
		iTotal += iRet;
		if( iTotal == nBytes )
			return iTotal;
	}
	*/
}

size_t Bu::Process::readErr( void *pBuf, size_t nBytes )
{
	return ::read( iStdErr, pBuf, nBytes );
}

size_t Bu::Process::write( const void *pBuf, size_t nBytes )
{
	return ::write( iStdIn, pBuf, nBytes );
}

long Bu::Process::tell()
{
	return 0;
}

void Bu::Process::seek( long offset )
{
}

void Bu::Process::setPos( long pos )
{
}

void Bu::Process::setPosEnd( long pos )
{
}

bool Bu::Process::isEOS()
{
	return false;
}

bool Bu::Process::isOpen()
{
	return true;
}

void Bu::Process::flush()
{
}

bool Bu::Process::canRead()
{
	return true;
}

bool Bu::Process::canWrite()
{
	return true;
}

bool Bu::Process::isReadable()
{
	return true;
}

bool Bu::Process::isWritable()
{
	return true;
}

bool Bu::Process::isSeekable()
{
	return false;
}

bool Bu::Process::isBlocking()
{
	return true;
}

void Bu::Process::setBlocking( bool bBlocking )
{
}