Commit 118bee86 authored by Hans Leidekker's avatar Hans Leidekker Committed by Alexandre Julliard

msvcrt: Implement _wpopen and forward _popen to it.

parent 3debf282
...@@ -623,6 +623,7 @@ MSVCRT_clock_t MSVCRT_clock(void); ...@@ -623,6 +623,7 @@ MSVCRT_clock_t MSVCRT_clock(void);
double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2); double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2);
MSVCRT_time_t MSVCRT_time(MSVCRT_time_t*); MSVCRT_time_t MSVCRT_time(MSVCRT_time_t*);
MSVCRT_FILE* MSVCRT__fdopen(int, const char *); MSVCRT_FILE* MSVCRT__fdopen(int, const char *);
MSVCRT_FILE* MSVCRT__wfdopen(int, const MSVCRT_wchar_t *);
int MSVCRT_vsnprintf(char *str, unsigned int len, const char *format, va_list valist); int MSVCRT_vsnprintf(char *str, unsigned int len, const char *format, va_list valist);
int MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, unsigned int len, int MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, unsigned int len,
const MSVCRT_wchar_t *format, va_list valist ); const MSVCRT_wchar_t *format, va_list valist );
......
...@@ -721,22 +721,21 @@ MSVCRT_intptr_t CDECL _wspawnvp(int flags, const MSVCRT_wchar_t* name, const MSV ...@@ -721,22 +721,21 @@ MSVCRT_intptr_t CDECL _wspawnvp(int flags, const MSVCRT_wchar_t* name, const MSV
} }
/********************************************************************* /*********************************************************************
* _popen (MSVCRT.@) * _wpopen (MSVCRT.@)
* FIXME: convert to _wpopen and call that from here instead? But it *
* would have to convert the command back to ANSI to call msvcrt_spawn, * Unicode version of _popen
* less than ideal.
*/ */
MSVCRT_FILE* CDECL MSVCRT__popen(const char* command, const char* mode) MSVCRT_FILE* CDECL MSVCRT__wpopen(const MSVCRT_wchar_t* command, const MSVCRT_wchar_t* mode)
{ {
static const char wcmd[] = "cmd", cmdFlag[] = " /C ", comSpec[] = "COMSPEC";
MSVCRT_FILE *ret; MSVCRT_FILE *ret;
BOOL readPipe = TRUE; BOOL readPipe = TRUE;
int textmode, fds[2], fdToDup, fdToOpen, fdStdHandle = -1, fdStdErr = -1; int textmode, fds[2], fdToDup, fdToOpen, fdStdHandle = -1, fdStdErr = -1;
const char *p; const MSVCRT_wchar_t *p;
char *cmdcopy; MSVCRT_wchar_t *comspec, *fullcmd;
DWORD comSpecLen; unsigned int len;
static const MSVCRT_wchar_t flag[] = {' ','/','c',' ',0};
TRACE("(command=%s, mode=%s)\n", debugstr_a(command), debugstr_a(mode)); TRACE("(command=%s, mode=%s)\n", debugstr_w(command), debugstr_w(mode));
if (!command || !mode) if (!command || !mode)
return NULL; return NULL;
...@@ -782,27 +781,27 @@ MSVCRT_FILE* CDECL MSVCRT__popen(const char* command, const char* mode) ...@@ -782,27 +781,27 @@ MSVCRT_FILE* CDECL MSVCRT__popen(const char* command, const char* mode)
MSVCRT__close(fds[fdToDup]); MSVCRT__close(fds[fdToDup]);
comSpecLen = GetEnvironmentVariableA(comSpec, NULL, 0); if (!(comspec = msvcrt_get_comspec())) goto error;
if (!comSpecLen) len = strlenW(comspec) + strlenW(flag) + strlenW(command) + 1;
comSpecLen = strlen(wcmd) + 1;
cmdcopy = HeapAlloc(GetProcessHeap(), 0, comSpecLen + strlen(cmdFlag) if (!(fullcmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(MSVCRT_wchar_t)))) goto error;
+ strlen(command)); strcpyW(fullcmd, comspec);
if (!GetEnvironmentVariableA(comSpec, cmdcopy, comSpecLen)) strcatW(fullcmd, flag);
strcpy(cmdcopy, wcmd); strcatW(fullcmd, command);
strcat(cmdcopy, cmdFlag); HeapFree(GetProcessHeap(), 0, comspec);
strcat(cmdcopy, command);
if (msvcrt_spawn(MSVCRT__P_NOWAIT, NULL, cmdcopy, NULL) == -1) if (msvcrt_spawn_wide(MSVCRT__P_NOWAIT, NULL, fullcmd, NULL) == -1)
{ {
MSVCRT__close(fds[fdToOpen]); MSVCRT__close(fds[fdToOpen]);
ret = NULL; ret = NULL;
} }
else else
{ {
ret = MSVCRT__fdopen(fds[fdToOpen], mode); ret = MSVCRT__wfdopen(fds[fdToOpen], mode);
if (!ret) if (!ret)
MSVCRT__close(fds[fdToOpen]); MSVCRT__close(fds[fdToOpen]);
} }
HeapFree(GetProcessHeap(), 0, cmdcopy); HeapFree(GetProcessHeap(), 0, fullcmd);
MSVCRT__dup2(fdStdHandle, fdToDup); MSVCRT__dup2(fdStdHandle, fdToDup);
MSVCRT__close(fdStdHandle); MSVCRT__close(fdStdHandle);
if (readPipe) if (readPipe)
...@@ -821,12 +820,30 @@ error: ...@@ -821,12 +820,30 @@ error:
} }
/********************************************************************* /*********************************************************************
* _wpopen (MSVCRT.@) * _popen (MSVCRT.@)
*/ */
MSVCRT_FILE* CDECL MSVCRT__wpopen(const MSVCRT_wchar_t* command, const MSVCRT_wchar_t* mode) MSVCRT_FILE* CDECL MSVCRT__popen(const char* command, const char* mode)
{ {
FIXME("(command=%s, mode=%s): stub\n", debugstr_w(command), debugstr_w(mode)); MSVCRT_FILE *ret;
MSVCRT_wchar_t *cmdW, *modeW;
TRACE("(command=%s, mode=%s)\n", debugstr_a(command), debugstr_a(mode));
if (!command || !mode)
return NULL; return NULL;
if (!(cmdW = msvcrt_wstrdupa(command))) return NULL;
if (!(modeW = msvcrt_wstrdupa(mode)))
{
HeapFree(GetProcessHeap(), 0, cmdW);
return NULL;
}
ret = MSVCRT__wpopen(cmdW, modeW);
HeapFree(GetProcessHeap(), 0, cmdW);
HeapFree(GetProcessHeap(), 0, modeW);
return ret;
} }
/********************************************************************* /*********************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment