Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenpopen.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS C runtime library 00004 * FILE: lib/sdk/crt/stdio/popen.c 00005 * PURPOSE: Pipe Functions 00006 * PROGRAMERS: Eric Kohl 00007 Hartmut Birr 00008 */ 00009 00010 #include <precomp.h> 00011 #include <tchar.h> 00012 00013 #ifdef _UNICODE 00014 #define sT "S" 00015 #else 00016 #define sT "s" 00017 #endif 00018 00019 #define MK_STR(s) #s 00020 00021 int alloc_fd(HANDLE hand, int flag); //FIXME: Remove 00022 unsigned split_oflags(unsigned oflags); //FIXME: Remove 00023 00024 /* 00025 * @implemented 00026 */ 00027 FILE *_tpopen (const _TCHAR *cm, const _TCHAR *md) /* program name, pipe mode */ 00028 { 00029 _TCHAR *szCmdLine=NULL; 00030 _TCHAR *szComSpec=NULL; 00031 _TCHAR *s; 00032 FILE *pf; 00033 HANDLE hReadPipe, hWritePipe; 00034 BOOL result; 00035 STARTUPINFO StartupInfo; 00036 PROCESS_INFORMATION ProcessInformation; 00037 SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE}; 00038 00039 TRACE(MK_STR(_tpopen)"('%"sT"', '%"sT"')\n", cm, md); 00040 00041 if (cm == NULL) 00042 return( NULL ); 00043 00044 szComSpec = _tgetenv(_T("COMSPEC")); 00045 if (szComSpec == NULL) 00046 { 00047 szComSpec = _T("cmd.exe"); 00048 } 00049 00050 s = max(_tcsrchr(szComSpec, '\\'), _tcsrchr(szComSpec, '/')); 00051 if (s == NULL) 00052 s = szComSpec; 00053 else 00054 s++; 00055 00056 szCmdLine = malloc((_tcslen(s) + 4 + _tcslen(cm) + 1) * sizeof(_TCHAR)); 00057 if (szCmdLine == NULL) 00058 { 00059 return NULL; 00060 } 00061 00062 _tcscpy(szCmdLine, s); 00063 s = _tcsrchr(szCmdLine, '.'); 00064 if (s) 00065 *s = 0; 00066 _tcscat(szCmdLine, _T(" /C ")); 00067 _tcscat(szCmdLine, cm); 00068 00069 if ( !CreatePipe(&hReadPipe,&hWritePipe,&sa,1024)) 00070 { 00071 free (szCmdLine); 00072 return NULL; 00073 } 00074 00075 memset(&StartupInfo, 0, sizeof(STARTUPINFO)); 00076 StartupInfo.cb = sizeof(STARTUPINFO); 00077 00078 if (*md == 'r' ) { 00079 StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); 00080 StartupInfo.hStdOutput = hWritePipe; 00081 StartupInfo.dwFlags |= STARTF_USESTDHANDLES; 00082 } 00083 else if ( *md == 'w' ) { 00084 StartupInfo.hStdInput = hReadPipe; 00085 StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); 00086 StartupInfo.dwFlags |= STARTF_USESTDHANDLES; 00087 } 00088 00089 if (StartupInfo.dwFlags & STARTF_USESTDHANDLES) 00090 StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); 00091 00092 result = CreateProcess(szComSpec, 00093 szCmdLine, 00094 NULL, 00095 NULL, 00096 TRUE, 00097 0, 00098 NULL, 00099 NULL, 00100 &StartupInfo, 00101 &ProcessInformation); 00102 free (szCmdLine); 00103 00104 if (result == FALSE) 00105 { 00106 CloseHandle(hReadPipe); 00107 CloseHandle(hWritePipe); 00108 return NULL; 00109 } 00110 00111 CloseHandle(ProcessInformation.hThread); 00112 00113 if ( *md == 'r' ) 00114 { 00115 pf = _tfdopen(alloc_fd(hReadPipe, split_oflags(_fmode)) , _T("r")); 00116 CloseHandle(hWritePipe); 00117 } 00118 else 00119 { 00120 pf = _tfdopen( alloc_fd(hWritePipe, split_oflags(_fmode)) , _T("w")); 00121 CloseHandle(hReadPipe); 00122 } 00123 00124 return( pf ); 00125 } 00126 00127 #ifndef _UNICODE 00128 00129 /* 00130 * @implemented 00131 */ 00132 int _pclose (FILE *pp) 00133 { 00134 TRACE("_pclose(%x)",pp); 00135 00136 fclose(pp); 00137 //if (!TerminateProcess(pp->_tmpfname ,0)) 00138 // return( -1 ); 00139 return( 0 ); 00140 } 00141 00142 #endif 00143 00144 Generated on Fri May 25 2012 04:34:57 for ReactOS by
1.7.6.1
|