FILE * win32_popen (const char *cmd, const char *fmode) { char mode[3]; char *p, *q; const char *cmd2; FILE *ret; mode[0] = fmode[0]; mode[1] = 'b'; mode[2] = '\0'; if (is_include_space (cmd)) { cmd2 = xmalloc (strlen (cmd) + 3); q = (char *)cmd2; p = (char *)cmd; *q++= '\"'; while(*p) *q++ = *p++; *q++ = '\"'; *q = '\0'; ret = _popen (cmd2, mode); free ((char *)cmd2); return ret; } else { return _popen (cmd, mode); } }
FILE * fsyscp_popen (const char *command, const char *mode) { FILE *f; wchar_t *commandw, modew[4]; int i; #if defined (KPSE_COMPAT_API) kpathsea kpse; #endif assert(command && mode); if (is_include_space (command)) { const char *p; char *command2, *q; command2 = xmalloc (strlen (command) + 3); p = command; q = command2; *q++ = '\"'; while (*p) *q++ = *p++; *q++ = '\"'; *q = '\0'; commandw = get_wstring_from_fsyscp(command2, commandw=NULL); free (command2); } else { commandw = get_wstring_from_fsyscp(command, commandw=NULL); } for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ f = _wpopen(commandw, modew); #if defined (KPSE_COMPAT_API) if (f != NULL) { kpse = kpse_def; if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { DEBUGF_START (); fprintf (stderr, "fsyscp_popen(%s [", command); WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), commandw, wcslen( commandw ), NULL, NULL ); #if defined(_WIN64) fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); #else fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); #endif DEBUGF_END (); } } #endif free (commandw); /* We use always binary mode on Windows */ if(f) _setmode (fileno (f), _O_BINARY); return f; }
/* system by file system codepage */ int fsyscp_system (const char *cmd) { const char *p; char *q; char *av[4]; int len, ret; int spacep = 0; if (cmd == NULL) return 1; av[0] = xstrdup ("cmd.exe"); av[1] = xstrdup ("/c"); len = strlen (cmd) + 3; spacep = is_include_space (cmd); av[2] = xmalloc (len); q = av[2]; if (spacep) *q++ = '"'; for (p = cmd; *p; p++, q++) { if (*p == '\'') *q = '"'; else *q = *p; } if (spacep) *q++ = '"'; *q = '\0'; av[3] = NULL; ret = fsyscp_spawnvp (_P_WAIT, av[0], (const char* const*) av); free (av[0]); free (av[1]); free (av[2]); return ret; }