3 #if defined(_WIN32) && !defined(__CYGWIN__)
10 #include <sys/types.h>
13 extern char **environ;
17 #include <qdatetime.h>
19 #if defined(_MSC_VER) || defined(__BORLANDC__)
21 #define pclose _pclose
28 //#include "doxygen.h"
30 static double g_sysElapsedTime;
33 int portable_system(const char *command,const char *args,bool commandHasConsole)
36 if (command==0) return 1;
38 QCString fullCmd=command;
39 fullCmd=fullCmd.stripWhiteSpace();
40 if (fullCmd.at(0)!='"' && fullCmd.find(' ')!=-1)
42 // add quotes around command as it contains spaces and is not quoted already
43 fullCmd="\""+fullCmd+"\"";
48 Debug::print(Debug::ExtCmd,0,"Executing external command `%s`\n",qPrint(fullCmd));
51 #if !defined(_WIN32) || defined(__CYGWIN__)
52 (void)commandHasConsole;
53 /*! taken from the system() manpage on my Linux box */
56 #ifdef _OS_SOLARIS // for Solaris we use vfork since it is more memory efficient
58 // on Solaris fork() duplicates the memory usage
59 // so we use vfork instead
68 execl("/bin/sh","sh","-c",fullCmd.data(),(char*)0);
73 while (waitpid(pid,&status,0 )<0)
84 #else // Other Unices just use fork
97 argv[2] = fullCmd.data();
99 execve("/bin/sh",(char * const *)argv,environ);
104 if (waitpid(pid,&status,0)==-1)
106 if (errno!=EINTR) return -1;
110 if (WIFEXITED(status))
112 return WEXITSTATUS(status);
120 #endif // !_OS_SOLARIS
122 #else // Win32 specific
123 if (commandHasConsole)
125 return system(fullCmd);
129 // Because ShellExecuteEx can delegate execution to Shell extensions
130 // (data sources, context menu handlers, verb implementations) that
131 // are activated using Component Object Model (COM), COM should be
132 // initialized before ShellExecuteEx is called. Some Shell extensions
133 // require the COM single-threaded apartment (STA) type.
134 // For that case COM is initialized as follows
135 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
137 QString commandw = QString::fromUtf8( command );
138 QString argsw = QString::fromUtf8( args );
140 // gswin32 is a GUI api which will pop up a window and run
141 // asynchronously. To prevent both, we use ShellExecuteEx and
142 // WaitForSingleObject (thanks to Robert Golias for the code)
144 SHELLEXECUTEINFOW sInfo = {
145 sizeof(SHELLEXECUTEINFOW), /* structure size */
146 SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI, /* tell us the process
147 * handle so we can wait till it's done |
148 * do not display msg box if error
150 NULL, /* window handle */
151 NULL, /* action to perform: open */
152 (LPCWSTR)commandw.ucs2(), /* file to execute */
153 (LPCWSTR)argsw.ucs2(), /* argument list */
154 NULL, /* use current working dir */
155 SW_HIDE, /* minimize on start-up */
156 0, /* application instance handle */
157 NULL, /* ignored: id list */
158 NULL, /* ignored: class name */
159 NULL, /* ignored: key class */
160 0, /* ignored: hot key */
161 NULL, /* ignored: icon */
162 NULL /* resulting application handle */
165 if (!ShellExecuteExW(&sInfo))
169 else if (sInfo.hProcess) /* executable was launched, wait for it to finish */
171 WaitForSingleObject(sInfo.hProcess,INFINITE);
172 /* get process exit code */
174 if (!GetExitCodeProcess(sInfo.hProcess,&exitCode))
178 CloseHandle(sInfo.hProcess);
183 return 1; // we should never get here
190 #if !defined(_WIN32) || defined(__CYGWIN__)
191 pid = (uint)getpid();
193 pid = (uint)GetCurrentProcessId();
198 #if defined(_WIN32) && !defined(__CYGWIN__)
200 static char **last_environ;
203 void portable_setenv(const char *name,const char *value)
205 if (value==0) value="";
206 #if defined(_WIN32) && !defined(__CYGWIN__)
207 SetEnvironmentVariable(name,value);
209 register char **ep = 0;
210 register size_t size;
211 const size_t namelen=qstrlen(name);
212 const size_t vallen=qstrlen(value) + 1;
217 for (ep = environ; *ep; ++ep)
219 if (!qstrncmp (*ep, name, (uint)namelen) &&
220 (*ep)[namelen] == '=')
227 if (environ==0 || *ep==0) /* add new string */
230 if (environ == last_environ && environ!=0)
232 // We allocated this space; we can extend it.
233 new_environ = (char **) realloc (last_environ, (size + 2) * sizeof (char *));
237 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
240 if (new_environ==0) // no more memory
245 new_environ[size] = (char *)malloc (namelen + 1 + vallen);
246 if (new_environ[size]==0)
252 if (environ != last_environ)
254 memcpy ((char *) new_environ, environ, size * sizeof (char *));
257 memcpy(new_environ[size], name, namelen);
258 new_environ[size][namelen] = '=';
259 memcpy(&new_environ[size][namelen + 1], value, vallen);
260 new_environ[size + 1] = 0;
261 last_environ = environ = new_environ;
263 else /* replace existing string */
265 size_t len = qstrlen (*ep);
266 if (len + 1 < namelen + 1 + vallen)
268 /* The existing string is too short; malloc a new one. */
269 char *newString = (char *)malloc(namelen + 1 + vallen);
276 memcpy(*ep, name, namelen);
277 (*ep)[namelen] = '=';
278 memcpy(&(*ep)[namelen + 1], value, vallen);
284 void portable_unsetenv(const char *variable)
286 #if defined(_WIN32) && !defined(__CYGWIN__)
287 SetEnvironmentVariable(variable,0);
289 /* Some systems don't have unsetenv(), so we do it ourselves */
293 if (variable == NULL || *variable == '\0' || strchr (variable, '=') != NULL)
295 return; // not properly formatted
298 len = qstrlen(variable);
303 if (!qstrncmp(*ep, variable, (uint)len) && (*ep)[len]=='=')
305 /* Found it. Remove this pointer by moving later ones back. */
307 do dp[0] = dp[1]; while (*dp++);
308 /* Continue the loop in case NAME appears again. */
318 const char *portable_getenv(const char *variable)
320 return getenv(variable);
323 portable_off_t portable_fseek(FILE *f,portable_off_t offset, int whence)
325 #if defined(__MINGW32__)
326 return fseeko64(f,offset,whence);
327 #elif defined(_WIN32) && !defined(__CYGWIN__)
328 return _fseeki64(f,offset,whence);
330 return fseeko(f,offset,whence);
334 portable_off_t portable_ftell(FILE *f)
336 #if defined(__MINGW32__)
338 #elif defined(_WIN32) && !defined(__CYGWIN__)
345 FILE *portable_fopen(const char *fileName,const char *mode)
347 #if defined(_WIN32) && !defined(__CYGWIN__)
348 QString fn(fileName);
350 return _wfopen((wchar_t*)fn.ucs2(),(wchar_t*)m.ucs2());
352 return fopen(fileName,mode);
356 char portable_pathSeparator()
358 #if defined(_WIN32) && !defined(__CYGWIN__)
365 char portable_pathListSeparator()
367 #if defined(_WIN32) && !defined(__CYGWIN__)
374 const char *portable_ghostScriptCommand()
376 #if defined(_WIN32) && !defined(__CYGWIN__)
377 return "gswin32c.exe";
383 const char *portable_commandExtension()
385 #if defined(_WIN32) && !defined(__CYGWIN__)
392 bool portable_fileSystemIsCaseSensitive()
394 #if defined(_WIN32) || defined(macintosh) || defined(__MACOSX__) || defined(__APPLE__)
401 FILE * portable_popen(const char *name,const char *type)
403 return popen(name,type);
406 int portable_pclose(FILE *stream)
408 return pclose(stream);
411 void portable_sysTimerStart()
416 void portable_sysTimerStop()
418 g_sysElapsedTime+=((double)g_time.elapsed())/1000.0;
421 double portable_getSysElapsedTime()
423 return g_sysElapsedTime;
426 void portable_sleep(int ms)
428 #if defined(_WIN32) && !defined(__CYGWIN__)
435 bool portable_isAbsolutePath(const char *fileName)
438 if (isalpha (fileName [0]) && fileName[1] == ':')
441 char const fst = fileName [0];
453 * Correct a possible wrong PATH variable
455 * This routine was inspired by the cause for bug 766059 was that in the Windows path there were forward slahes.
457 void portable_correct_path(void)
459 #if defined(_WIN32) && !defined(__CYGWIN__)
460 const char *p = portable_getenv("PATH");
461 char *q = (char *)malloc(strlen(p) + 1);
464 for (int i = 0 ; i < strlen(q); i++)
472 if (found) portable_setenv("PATH",q);