-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-\r
-//\r
-// start of shared cmdlib stuff\r
-// \r
-\r
-#include "cmdlib.h"\r
-\r
-#ifdef _WIN32\r
- #include <windows.h>\r
-#endif\r
-#if defined (__linux__) || defined (__APPLE__)\r
- #include <unistd.h>\r
-#endif\r
-\r
-// FIXME TTimo this should be cleaned up ..\r
-// NOTE: we don't use this crap .. with the total mess of mixing win32/unix paths we need to recognize both '/' and '\\'\r
-#define PATHSEPERATOR '/'\r
-\r
-#if defined (__linux__) || defined (__APPLE__)\r
-bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)\r
-{\r
- char fullcmd[2048];\r
- char *pCmd;\r
-#ifdef _DEBUG\r
- printf("Q_Exec damnit\n");\r
-#endif\r
- switch (fork())\r
- {\r
- case -1:\r
- return true;\r
- break;\r
- case 0:\r
- // always concat the command on linux\r
- if (cmd)\r
- {\r
- strcpy(fullcmd, cmd);\r
- }\r
- else\r
- fullcmd[0] = '\0';\r
- if (cmdline)\r
- {\r
- strcat(fullcmd, " ");\r
- strcat(fullcmd, cmdline);\r
- }\r
- pCmd = fullcmd;\r
- while (*pCmd == ' ')\r
- pCmd++;\r
-#ifdef _DEBUG\r
- printf("Running system...\n");\r
- printf("Command: %s\n", pCmd);\r
-#endif\r
- system( pCmd );\r
-#ifdef _DEBUG\r
- printf ("system() returned\n");\r
-#endif\r
- _exit (0);\r
- break;\r
- }\r
- return true;\r
-}\r
-#endif\r
-\r
-#ifdef _WIN32\r
-// NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess\r
-bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)\r
-{\r
- PROCESS_INFORMATION ProcessInformation;\r
- STARTUPINFO startupinfo = {0};\r
- DWORD dwCreationFlags;\r
- GetStartupInfo (&startupinfo);\r
- if (bCreateConsole)\r
- dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;\r
- else\r
- dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;\r
- const char *pCmd;\r
- char *pCmdline;\r
- pCmd = cmd;\r
- if (pCmd)\r
- {\r
- while (*pCmd == ' ')\r
- pCmd++;\r
- }\r
- pCmdline = cmdline;\r
- if (pCmdline)\r
- {\r
- while (*pCmdline == ' ')\r
- pCmdline++;\r
- }\r
- if (CreateProcess(\r
- pCmd,\r
- pCmdline,\r
- NULL,\r
- NULL,\r
- FALSE,\r
- dwCreationFlags,\r
- NULL,\r
- execdir,\r
- &startupinfo,\r
- &ProcessInformation\r
- ))\r
- return true;\r
- return false;\r
-}\r
-#endif\r
-\r
-#define MEM_BLOCKSIZE 4096\r
-void* qblockmalloc(size_t nSize)\r
-{\r
- void *b;\r
- // round up to threshold\r
- int nAllocSize = nSize % MEM_BLOCKSIZE;\r
- if ( nAllocSize > 0)\r
- {\r
- nSize += MEM_BLOCKSIZE - nAllocSize;\r
- }\r
- b = malloc(nSize + 1);\r
- memset (b, 0, nSize);\r
- return b;\r
-}\r
-\r
-//++timo NOTE: can be replaced by g_malloc0(nSize+1) when moving to glib memory handling\r
-void* qmalloc (size_t nSize)\r
-{\r
- void *b;\r
- b = malloc(nSize + 1);\r
- memset (b, 0, nSize);\r
- return b;\r
-}\r
-\r
-/*\r
-================\r
-Q_filelength\r
-================\r
-*/\r
-int Q_filelength (FILE *f)\r
-{\r
- int pos;\r
- int end;\r
-\r
- pos = ftell (f);\r
- fseek (f, 0, SEEK_END);\r
- end = ftell (f);\r
- fseek (f, pos, SEEK_SET);\r
-\r
- return end;\r
-}\r
-\r
-void DefaultExtension (char *path, char *extension)\r
-{\r
- char *src;\r
-//\r
-// if path doesn't have a .EXT, append extension\r
-// (extension should include the .)\r
-//\r
- src = path + strlen(path) - 1;\r
-\r
- while (*src != PATHSEPERATOR && src != path)\r
- {\r
- if (*src == '.')\r
- return; // it has an extension\r
- src--;\r
- }\r
-\r
- strcat (path, extension);\r
-}\r
-\r
-void DefaultPath (char *path, char *basepath)\r
-{\r
- char temp[128];\r
-\r
- if (path[0] == PATHSEPERATOR)\r
- return; // absolute path location\r
- strcpy (temp,path);\r
- strcpy (path,basepath);\r
- strcat (path,temp);\r
-}\r
-\r
-\r
-void StripFilename (char *path)\r
-{\r
- int length;\r
-\r
- length = strlen(path)-1;\r
- while (length > 0 && path[length] != PATHSEPERATOR)\r
- length--;\r
- path[length] = 0;\r
-}\r
-\r
-void StripExtension (char *path)\r
-{\r
- int length;\r
-\r
- length = strlen(path)-1;\r
- while (length > 0 && path[length] != '.')\r
- {\r
- length--;\r
- if (path[length] == '/')\r
- return; // no extension\r
- }\r
- if (length)\r
- path[length] = 0;\r
-}\r
-\r
-\r
-/*\r
-====================\r
-Extract file parts\r
-====================\r
-*/\r
-void ExtractFilePath (const char *path, char *dest)\r
-{\r
- const char *src;\r
-\r
- src = path + strlen(path) - 1;\r
-\r
-//\r
-// back up until a \ or the start\r
-//\r
- while (src != path && *(src-1) != '/' && *(src-1) != '\\')\r
- src--;\r
-\r
- memcpy (dest, path, src-path);\r
- dest[src-path] = 0;\r
-}\r
-\r
-void ExtractFileName (const char *path, char *dest)\r
-{\r
- const char *src;\r
-\r
- src = path + strlen(path) - 1;\r
-\r
-//\r
-// back up until a \ or the start\r
-//\r
- while (src != path && *(src-1) != '/' \r
- && *(src-1) != '\\' )\r
- src--;\r
-\r
- while (*src)\r
- {\r
- *dest++ = *src++;\r
- }\r
- *dest = 0;\r
-}\r
-\r
-inline const char* path_get_filename_start(const char* path)\r
-{\r
- {\r
- const char* last_forward_slash = strrchr(path, '/');\r
- if(last_forward_slash != NULL)\r
- return last_forward_slash + 1;\r
- }\r
-\r
- {\r
- const char* last_backward_slash = strrchr(path, '\\');\r
- if(last_backward_slash != NULL)\r
- return last_backward_slash + 1;\r
- }\r
-\r
- return path;\r
-}\r
-\r
-inline unsigned int filename_get_base_length(const char* filename)\r
-{\r
- const char* last_period = strrchr(filename, '.');\r
- return (last_period != NULL) ? last_period - filename : strlen(filename);\r
-}\r
-\r
-void ExtractFileBase (const char *path, char *dest)\r
-{\r
- const char* filename = path_get_filename_start(path);\r
- unsigned int length = filename_get_base_length(filename);\r
- strncpy(dest, filename, length);\r
- dest[length] = '\0';\r
-}\r
-\r
-void ExtractFileExtension (const char *path, char *dest)\r
-{\r
- const char *src;\r
-\r
- src = path + strlen(path) - 1;\r
-\r
-//\r
-// back up until a . or the start\r
-//\r
- while (src != path && *(src-1) != '.')\r
- src--;\r
- if (src == path)\r
- {\r
- *dest = 0; // no extension\r
- return;\r
- }\r
-\r
- strcpy (dest,src);\r
-}\r
-\r
-\r
-void ConvertDOSToUnixName( char *dst, const char *src )\r
-{\r
- while ( *src )\r
- {\r
- if ( *src == '\\' )\r
- *dst = '/';\r
- else\r
- *dst = *src;\r
- dst++; src++;\r
- }\r
- *dst = 0;\r
-}\r
-\r
-\r
-char* StrDup(char* pStr)\r
-{ \r
- if (pStr)\r
- {\r
- return strcpy(new char[strlen(pStr)+1], pStr); \r
- }\r
- return NULL;\r
-}\r
-\r
-char* StrDup(const char* pStr)\r
-{ \r
- if (pStr)\r
- {\r
- return strcpy(new char[strlen(pStr)+1], pStr); \r
- }\r
- return NULL;\r
-}\r
-\r
-void CreateDirectoryPath (const char *path) {\r
- char base[PATH_MAX];\r
- char *src;\r
- char back;\r
- \r
- ExtractFilePath(path, base);\r
-\r
- src = base+1;\r
- while (1) {\r
- while (*src != '\0' && *src != '/' && *src != '\\') {\r
- src++;\r
- }\r
- if (*src == '\0') {\r
- break;\r
- }\r
- back = *src; *src = '\0';\r
- Q_mkdir(base, 0755);\r
- *src = back; src++;\r
- }\r
-}\r
-\r
-/*\r
-============================================================================\r
-\r
- BYTE ORDER FUNCTIONS\r
-\r
-============================================================================\r
-*/\r
-\r
-#ifdef _SGI_SOURCE\r
- #define __BIG_ENDIAN__\r
-#endif\r
-\r
-#ifdef __BIG_ENDIAN__\r
-\r
-short LittleShort (short l)\r
-{\r
- byte b1,b2;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
-\r
- return(b1<<8) + b2;\r
-}\r
-\r
-short BigShort (short l)\r
-{\r
- return l;\r
-}\r
-\r
-\r
-int LittleLong (int l)\r
-{\r
- byte b1,b2,b3,b4;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
- b3 = (l>>16)&255;\r
- b4 = (l>>24)&255;\r
-\r
- return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;\r
-}\r
-\r
-int BigLong (int l)\r
-{\r
- return l;\r
-}\r
-\r
-\r
-float LittleFloat (float l)\r
-{\r
- union\r
- {\r
- byte b[4]; float f;\r
- } in, out;\r
-\r
- in.f = l;\r
- out.b[0] = in.b[3];\r
- out.b[1] = in.b[2];\r
- out.b[2] = in.b[1];\r
- out.b[3] = in.b[0];\r
-\r
- return out.f;\r
-}\r
-\r
-float BigFloat (float l)\r
-{\r
- return l;\r
-}\r
-\r
-#else\r
-\r
-short BigShort (short l)\r
-{\r
- byte b1,b2;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
-\r
- return(b1<<8) + b2;\r
-}\r
-\r
-short LittleShort (short l)\r
-{\r
- return l;\r
-}\r
-\r
-\r
-int BigLong (int l)\r
-{\r
- byte b1,b2,b3,b4;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
- b3 = (l>>16)&255;\r
- b4 = (l>>24)&255;\r
-\r
- return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;\r
-}\r
-\r
-int LittleLong (int l)\r
-{\r
- return l;\r
-}\r
-\r
-float BigFloat (float l)\r
-{\r
- union\r
- {\r
- byte b[4]; float f;\r
- } in, out;\r
-\r
- in.f = l;\r
- out.b[0] = in.b[3];\r
- out.b[1] = in.b[2];\r
- out.b[2] = in.b[1];\r
- out.b[3] = in.b[0];\r
-\r
- return out.f;\r
-}\r
-\r
-float LittleFloat (float l)\r
-{\r
- return l;\r
-}\r
-\r
-#endif\r
+/*
+ Copyright (C) 1999-2006 Id Software, Inc. and contributors.
+ For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+ This file is part of GtkRadiant.
+
+ GtkRadiant is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ GtkRadiant is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GtkRadiant; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+//
+// start of shared cmdlib stuff
+//
+
+#include "cmdlib.h"
+#include "globaldefs.h"
+
+#include <string.h>
+#include <stdio.h>
+
+#include "string/string.h"
+#include "os/path.h"
+#include "container/array.h"
+
+
+#if GDEF_OS_POSIX
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+bool Q_Exec( const char *cmd, char *cmdline, const char *, bool, bool waitfor ){
+ char fullcmd[2048];
+ char *pCmd;
+ pid_t pid;
+#if GDEF_DEBUG
+ printf( "Q_Exec damnit\n" );
+#endif
+ switch ( ( pid = fork() ) )
+ {
+ default:
+ if ( waitfor ) {
+ waitpid( pid, NULL, 0 );
+ }
+ break;
+ case -1:
+ return true;
+ break;
+ case 0:
+ // always concat the command on linux
+ if ( cmd ) {
+ strcpy( fullcmd, cmd );
+ }
+ else{
+ fullcmd[0] = '\0';
+ }
+ if ( cmdline ) {
+ strcat( fullcmd, " " );
+ strcat( fullcmd, cmdline );
+ }
+ pCmd = fullcmd;
+ while ( *pCmd == ' ' )
+ pCmd++;
+#if GDEF_DEBUG
+ printf( "Running system...\n" );
+ printf( "Command: %s\n", pCmd );
+#endif
+ int ret = system( pCmd );
+#if GDEF_DEBUG
+ printf( "system() returned\n" );
+#endif
+ _exit( ret );
+ break;
+ }
+ return true;
+}
+
+#elif GDEF_OS_WINDOWS
+
+#include <windows.h>
+
+// NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess
+bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor ){
+ PROCESS_INFORMATION ProcessInformation;
+ STARTUPINFO startupinfo = {0};
+ DWORD dwCreationFlags;
+ GetStartupInfo( &startupinfo );
+ if ( bCreateConsole ) {
+ dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
+ }
+ else{
+ dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
+ }
+ const char *pCmd;
+ char *pCmdline;
+ pCmd = cmd;
+ if ( pCmd ) {
+ while ( *pCmd == ' ' )
+ pCmd++;
+ }
+ pCmdline = cmdline;
+ if ( pCmdline ) {
+ while ( *pCmdline == ' ' )
+ pCmdline++;
+ }
+
+ if ( CreateProcess(
+ pCmd,
+ pCmdline,
+ NULL,
+ NULL,
+ FALSE,
+ dwCreationFlags,
+ NULL,
+ execdir,
+ &startupinfo,
+ &ProcessInformation
+ ) ) {
+ if ( waitfor ) {
+ WaitForSingleObject( ProcessInformation.hProcess, INFINITE );
+ }
+ return true;
+ }
+ return false;
+}
+
+#endif