| 1 | //======================================================================== |
| 2 | // PtProcess.cxx |
| 3 | //======================================================================== |
| 4 | // Portable Process with pipes Handling |
| 5 | // Features easy File and Pipe/Process opening and reading uniformization |
| 6 | // support of read "r" or write "w" popen modes |
| 7 | // Copyright F. Costantini 2006 |
| 8 | // Version 1.0 |
| 9 | //======================================================================== |
| 10 | // This program is free software; you can redistribute it and/or modify |
| 11 | // it under the terms of the GNU General Public License v2 as published |
| 12 | // by the Free Software Foundation. |
| 13 | //======================================================================== |
| 14 | |
| 15 | #if defined(WIN32) |
| 16 | #include <windows.h> |
| 17 | #include <io.h> |
| 18 | #include <FCNTL.H> |
| 19 | #endif |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include "PtProcess.h" |
| 23 | |
| 24 | #ifdef WIN32 |
| 25 | void CPtProcess::clean_close(HANDLE& h) { |
| 26 | if (h!= INVALID_HANDLE_VALUE) CloseHandle(h); |
| 27 | h = INVALID_HANDLE_VALUE; |
| 28 | } |
| 29 | |
| 30 | bool CPtProcess::createPipe(HANDLE * h, BOOL bInheritHnd) { |
| 31 | SECURITY_ATTRIBUTES sa; |
| 32 | |
| 33 | sa.nLength = sizeof(sa); |
| 34 | sa.lpSecurityDescriptor = NULL; |
| 35 | sa.bInheritHandle = bInheritHnd; |
| 36 | |
| 37 | return CreatePipe (&h[0],&h[1],&sa,0) ? true : false; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | FILE * CPtProcess::freeHandles() { |
| 42 | clean_close(pin[0]); clean_close(pin[1]); |
| 43 | clean_close(pout[0]); clean_close(pout[1]); |
| 44 | clean_close(perr[0]); clean_close(perr[1]); |
| 45 | |
| 46 | return NULL; // convenient for error management |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | CPtProcess::CPtProcess() { |
| 52 | _fpt= NULL; |
| 53 | } |
| 54 | |
| 55 | CPtProcess::~CPtProcess() { |
| 56 | if (_fpt) { |
| 57 | close(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // file open : |
| 62 | FILE * CPtProcess::fopen(const char *cmd, const char *mode) { |
| 63 | #ifdef WIN32 |
| 64 | pin[0] = pin[1] = pout[0] = pout[1] = perr[0] = perr[1] = INVALID_HANDLE_VALUE; |
| 65 | #endif |
| 66 | if (_fpt) close(); // close first before reuse |
| 67 | _fpt = ::fopen(cmd,mode); |
| 68 | return _fpt; |
| 69 | } |
| 70 | |
| 71 | // pipe open : |
| 72 | FILE * CPtProcess::popen(const char *cmd, const char *mode) { |
| 73 | #if defined(WIN32) |
| 74 | |
| 75 | // PRECONDITIONS |
| 76 | if (!mode || !*mode || (*mode!='r' && *mode!='w') ) return NULL; |
| 77 | if (_fpt) close(); // close first before reuse |
| 78 | |
| 79 | ptmode = *mode; |
| 80 | |
| 81 | pin[0] = pin[1] = pout[0] = pout[1] = perr[0] = perr[1] = INVALID_HANDLE_VALUE; |
| 82 | // stderr to stdout wanted ? |
| 83 | int fusion = (strstr(cmd,"2>&1") !=NULL); |
| 84 | |
| 85 | |
| 86 | // Create windows pipes |
| 87 | if (!createPipe(pin) || !createPipe(pout) || (!fusion && !createPipe(perr) ) ) |
| 88 | return freeHandles(); // error |
| 89 | |
| 90 | // Initialize Startup Info |
| 91 | ZeroMemory(&si, sizeof(STARTUPINFO)); |
| 92 | si.cb = sizeof(STARTUPINFO); |
| 93 | si.dwFlags = STARTF_USESTDHANDLES; |
| 94 | si.hStdInput = pin[0]; |
| 95 | si.hStdOutput = pout[1]; |
| 96 | si.hStdError = fusion ? pout[1] : perr [1]; |
| 97 | |
| 98 | |
| 99 | if ( CreateProcess(NULL, (LPTSTR) cmd,NULL,NULL,TRUE, |
| 100 | DETACHED_PROCESS,NULL,NULL, &si, &pi)) { |
| 101 | // don't need theses handles inherited by child process: |
| 102 | clean_close(pin[0]); clean_close(pout[1]); clean_close(perr[1]); |
| 103 | HANDLE & h = *mode == 'r' ? pout[0] : pin[1]; |
| 104 | _fpt = _fdopen(_open_osfhandle((long) h,_O_BINARY),mode); |
| 105 | h= INVALID_HANDLE_VALUE; // reset the handle pointer that is shared |
| 106 | // with _fpt so we don't free it twice |
| 107 | } |
| 108 | |
| 109 | if (!_fpt) freeHandles(); |
| 110 | return _fpt; |
| 111 | #else |
| 112 | _fpt=::popen(cmd,mode); |
| 113 | return _fpt; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | // close: |
| 118 | int CPtProcess::close() { |
| 119 | #if defined(WIN32) |
| 120 | if (_fpt) { |
| 121 | fclose(_fpt); |
| 122 | clean_close(perr[0]); |
| 123 | fclose(_fpt); |
| 124 | clean_close(pin[1]); |
| 125 | clean_close(pout[0]); |
| 126 | _fpt = NULL; |
| 127 | return 0; |
| 128 | } |
| 129 | return -1; |
| 130 | #else |
| 131 | int ret = ::pclose(_fpt); |
| 132 | _fpt=NULL; |
| 133 | return ret; |
| 134 | #endif |
| 135 | } |