24 #include <sys/types.h> 37 #ifdef RLWIN32_NOT_USABLE 38 WaitNamedPipe
function 39 Minimum supported client Windows 2000 Professional [desktop apps only]
41 static int GetFileNameFromHandle(HANDLE hFile,
char *pszFilename,
const int max_path)
48 DWORD dwFileSizeHi = 0;
49 DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
51 if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
53 printf(TEXT(
"Cannot map a file with a length of zero.\n"));
58 hFileMap = CreateFileMapping(hFile,
68 void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
72 if (GetMappedFileName (GetCurrentProcess(),
82 if (GetLogicalDriveStrings((
int)
sizeof(szTemp) -1, szTemp))
84 TCHAR szName[max_path];
85 TCHAR szDrive[3] = TEXT(
" :");
95 if (QueryDosDevice(szDrive, szName, max_path))
97 int uNameLen = strlen(szName);
99 if (uNameLen < max_path)
103 bFound = rlName.strnnocasecmp(pszFilename, uNameLen) == 0
104 && *(pszFilename + uNameLen) == _T(
'\\');
110 TCHAR szTempFile[max_path];
111 StringCchPrintf(szTempFile,
115 pszFilename+uNameLen);
116 StringCchCopyN(pszFilename, max_path+1, szTempFile, _tcslen(szTempFile));
123 }
while (!bFound && *p);
127 UnmapViewOfFile(pMem);
130 CloseHandle(hFileMap);
148 toChild = fromChild = NULL;
163 if(toChild != NULL) ::fclose((FILE*) toChild);
164 if(fromChild != NULL) ::fclose((FILE*) fromChild);
184 SECURITY_ATTRIBUTES saAttr;
185 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
186 saAttr.bInheritHandle = TRUE;
187 saAttr.lpSecurityDescriptor = NULL;
189 char cmd[strlen(command)+1];
194 printf(
"CreatePipe() - pipe for child process STDOUT failed\n");
197 if(!SetHandleInformation(
fromChildRD, HANDLE_FLAG_INHERIT, 0))
199 printf(
"SetHandleInformation() - pipe STDOUT read handle failed for inheritance\n");
208 printf(
"CreatePipe() - pipe for child process STDIN failed\n");
215 if(!SetHandleInformation(
toChildWR, HANDLE_FLAG_INHERIT, 0))
217 printf(
"SetHandleInformation() - pipe STDIN write handle failed for inheritance\n");
230 memset(&si,0,
sizeof(si));
235 si.dwFlags |= STARTF_USESTDHANDLES;
237 PROCESS_INFORMATION pi;
238 memset(&pi,0,
sizeof(pi));
240 DWORD dwCreationFlags = 0;
242 int ret = (int) CreateProcess( NULL, cmd
244 , TRUE, dwCreationFlags
264 int to_child[2],from_child[2],ret;
266 if(toChild != NULL) ::fclose((FILE*) toChild);
267 if(fromChild != NULL) ::fclose((FILE*) fromChild);
268 toChild = fromChild = NULL;
270 ret = ::pipe(to_child);
271 if(ret == -1)
return -1;
272 ret = ::pipe(from_child);
273 if(ret == -1)
return -1;
275 if((
pid = ::fork()) == 0)
279 ::dup2(to_child[0],0);
280 ::close(to_child[0]);
282 if(from_child[1] != 2)
284 ::dup2(from_child[1] ,2);
286 if(from_child[1] != 1)
288 ::dup2(from_child[1],1);
289 ::close(from_child[1]);
291 ::close(to_child[1]);
292 ::close(from_child[0]);
297 ::close(to_child[0]);
298 ::close(from_child[1]);
299 toChild = (
void*) ::fdopen(to_child[1],
"w");
300 if(toChild == NULL) {
return -1; }
301 fromChild = (
void*) ::fdopen(from_child[0],
"r");
302 if(fromChild == NULL) { ::fclose((FILE*) toChild);
return -1; }
313 for(i=0; i < (int)
sizeof(
line) - 1; i++)
317 if(i==0)
return NULL;
324 cptr = strchr(
line,0x0d);
338 if(fromChild == NULL)
return NULL;
339 if(::fgets(
line,
sizeof(
line)-1,(FILE*) fromChild) == NULL)
345 waitpid(
pid, &status, 0);
360 unsigned char buf[4];
361 int ret = (int) ReadFile(
fromChildRD, buf, 1, &readed, NULL);
364 if(readed == 1)
return buf[0];
380 if(fromChild == NULL)
return EOF;
381 return ::fgetc((FILE*) fromChild);
390 int ret = (int) WriteFile(
toChildWR, buf, len, &written, NULL);
397 if(toChild == NULL)
return -1;
398 return ::write(fileno((FILE*)toChild),buf,len);
411 if(ret < 0)
return ret;
412 return write(message,strlen(message));
417 if(buf[0] == EOF && buf[1] ==
'\0')
433 if(toChild != NULL) ::fclose((FILE*) toChild);
434 if(fromChild != NULL) ::fclose((FILE*) fromChild);
442 int len = strlen(buf);
443 return write(buf,len+1);
445 if(toChild == NULL)
return -1;
446 return fprintf((FILE*)toChild,
"%s",buf);
463 if(timeout > 0)
return 0;
467 struct timeval timout;
468 fd_set wset,rset,eset;
471 if(fromChild == NULL)
return -1;
472 s = fileno((FILE *) fromChild);
479 timout.tv_sec = timeout / 1000;
480 timout.tv_usec = (timeout % 1000) * 1000;
482 ret =
::select(maxfdp1,&rset,&wset,&eset,&timout);
483 if(ret == 0)
return 0;
493 return (FILE *) fromChild;
500 if(
pid == -1)
return -1;
501 if(
pid == 0)
return -1;
507 if(
pid == -1)
return -1;
508 if(
pid == 0)
return -1;
514 TerminateProcess((HANDLE)
hProcess, 0);
515 WaitForSingleObject((HANDLE)
hProcess, 60000);
533 if((c1 =
getchar()) < 0)
return -1;
536 if((c2 =
getchar()) < 0)
return -2;
545 buffer[ind++] = (
unsigned char) c1;
546 buffer[ind++] = (
unsigned char) c2;
549 if(ind >= maxbuffer)
return -3;
550 if((c1 =
getchar()) < 0)
return -4;
551 buffer[ind++] = (
unsigned char) c1;
554 if((c2 =
getchar()) < 0)
return -5;
555 buffer[ind++] = (
unsigned char) c2;
int select(int timeout=50)
int writeString(const char *buf)
int printf(const char *format,...)
int readJpegBuffer(unsigned char *buffer, int maxbuffer)
int write(const char *buf, int len)
int rlexec(const char *command)
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
int spawn(const char *command)