]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/cmdlib/cmdlib.cpp
gcc: appease the hardening warnings
[xonotic/netradiant.git] / libs / cmdlib / cmdlib.cpp
index f6ace0fe117f3424ae6d4108d83840f73b88cef1..075ff2c7d45c0a6c80021ad8c1a889afe0aa0a69 100644 (file)
@@ -1,29 +1,30 @@
 /*
-Copyright (C) 1999-2006 Id Software, Inc. and contributors.
-For a list of contributors, see the accompanying CONTRIBUTORS file.
+   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.
+   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 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.
+   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
-*/
+   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>
@@ -33,108 +34,105 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "container/array.h"
 
 
-#if defined (POSIX)
+#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;
-#ifdef _DEBUG
-  printf("Q_Exec damnit\n");
+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++;
-#ifdef _DEBUG
-    printf("Running system...\n");
-    printf("Command: %s\n", pCmd);
+       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
-    system( pCmd );
-#ifdef _DEBUG
-    printf ("system() returned\n");
+               int ret = system( pCmd );
+#if GDEF_DEBUG
+               printf( "system() returned\n" );
 #endif
-    _exit (0);
-    break;
-  }
-  return true;
+               _exit( ret );
+               break;
+       }
+       return true;
 }
 
-#elif defined(WIN32)
+#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;
+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
-