aboutsummaryrefslogtreecommitdiff
path: root/src/process.cpp
blob: 8fe98f3644e69d42e915849e00bd01da79b5dabc (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
/*
 * Copyright (C) 2007 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 <unistd.h>
#include <stdarg.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];

	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 );
	}
}

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

size_t Bu::Process::read( void *pBuf, size_t nBytes )
{
	return ::read( iStdOut, 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 )
{
}