3 #include <msvcrt/errno.h>
4 #include <msvcrt/stdio.h>
5 #include <msvcrt/stdlib.h>
6 #include <msvcrt/string.h>
7 #include <msvcrt/internal/file.h>
9 #include <msvcrt/msvcrtdbg.h>
15 FILE *_popen (const char *cm
, const char *md
) /* program name, pipe mode */
21 HANDLE hReadPipe
, hWritePipe
;
23 STARTUPINFOA StartupInfo
;
24 PROCESS_INFORMATION ProcessInformation
;
25 SECURITY_ATTRIBUTES sa
= {sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
27 DPRINT("_popen('%s', '%s')\n", cm
, md
);
32 szComSpec
= getenv("COMSPEC");
33 if (szComSpec
== NULL
)
35 szComSpec
= "cmd.exe";
38 s
= max(strrchr(szComSpec
, '\\'), strrchr(szComSpec
, '/'));
44 szCmdLine
= malloc(strlen(s
) + 4 + strlen(cm
) + 1);
45 if (szCmdLine
== NULL
)
51 s
= strrchr(szCmdLine
, '.');
54 strcat(szCmdLine
, " /C ");
55 strcat(szCmdLine
, cm
);
57 if ( !CreatePipe(&hReadPipe
,&hWritePipe
,&sa
,1024))
63 memset(&StartupInfo
, 0, sizeof(STARTUPINFOA
));
64 StartupInfo
.cb
= sizeof(STARTUPINFOA
);
67 StartupInfo
.hStdOutput
= hWritePipe
;
68 StartupInfo
.dwFlags
|= STARTF_USESTDHANDLES
;
70 else if ( *md
== 'w' ) {
71 StartupInfo
.hStdInput
= hReadPipe
;
72 StartupInfo
.dwFlags
|= STARTF_USESTDHANDLES
;
75 result
= CreateProcessA(szComSpec
,
89 CloseHandle(hReadPipe
);
90 CloseHandle(hWritePipe
);
94 CloseHandle(ProcessInformation
.hThread
);
98 pf
= _fdopen(__fileno_alloc(hReadPipe
, _fmode
) , "r");
99 CloseHandle(hWritePipe
);
103 pf
= _fdopen( __fileno_alloc(hWritePipe
, _fmode
) , "w");
104 CloseHandle(hReadPipe
);
107 pf
->_name_to_remove
= ProcessInformation
.hProcess
;
116 int _pclose (FILE *pp
)
119 if (!TerminateProcess(pp
->_name_to_remove
,0))
128 FILE *_wpopen (const wchar_t *cm
, const wchar_t *md
) /* program name, pipe mode */
130 wchar_t *szCmdLine
=NULL
;
131 wchar_t *szComSpec
=NULL
;
134 HANDLE hReadPipe
, hWritePipe
;
136 STARTUPINFOW StartupInfo
;
137 PROCESS_INFORMATION ProcessInformation
;
138 SECURITY_ATTRIBUTES sa
= {sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
140 DPRINT("_wpopen('%S', '%S')\n", cm
, md
);
145 szComSpec
= _wgetenv(L
"COMSPEC");
147 if (szComSpec
== NULL
)
149 szComSpec
= L
"cmd.exe";
152 s
= max(wcsrchr(szComSpec
, L
'\\'), wcsrchr(szComSpec
, L
'/'));
158 szCmdLine
= malloc((wcslen(s
) + 4 + wcslen(cm
) + 1) * sizeof(wchar_t));
159 if (szCmdLine
== NULL
)
164 wcscpy(szCmdLine
, s
);
165 s
= wcsrchr(szCmdLine
, L
'.');
168 wcscat(szCmdLine
, L
" /C ");
169 wcscat(szCmdLine
, cm
);
171 if ( !CreatePipe(&hReadPipe
,&hWritePipe
,&sa
,1024))
177 memset(&StartupInfo
, 0, sizeof(STARTUPINFOW
));
178 StartupInfo
.cb
= sizeof(STARTUPINFOW
);
181 StartupInfo
.hStdOutput
= hWritePipe
;
182 StartupInfo
.dwFlags
|= STARTF_USESTDHANDLES
;
184 else if ( *md
== L
'w' ) {
185 StartupInfo
.hStdInput
= hReadPipe
;
186 StartupInfo
.dwFlags
|= STARTF_USESTDHANDLES
;
189 result
= CreateProcessW(szComSpec
,
198 &ProcessInformation
);
203 CloseHandle(hReadPipe
);
204 CloseHandle(hWritePipe
);
208 CloseHandle(ProcessInformation
.hThread
);
212 pf
= _wfdopen(__fileno_alloc(hReadPipe
, _fmode
) , L
"r");
213 CloseHandle(hWritePipe
);
217 pf
= _wfdopen( __fileno_alloc(hWritePipe
, _fmode
) , L
"w");
218 CloseHandle(hReadPipe
);
221 pf
->_name_to_remove
= ProcessInformation
.hProcess
;