]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/cmdlib/cmdlib.cpp
Merge remote-tracking branch 'ttimo/master'
[xonotic/netradiant.git] / libs / cmdlib / cmdlib.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 //
23 // start of shared cmdlib stuff
24 //
25
26 #include "cmdlib.h"
27
28 #include <string.h>
29 #include <stdio.h>
30
31 #include "string/string.h"
32 #include "os/path.h"
33 #include "container/array.h"
34
35
36 #if defined ( POSIX )
37
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41
42 bool Q_Exec( const char *cmd, char *cmdline, const char *, bool, bool waitfor ){
43         char fullcmd[2048];
44         char *pCmd;
45         pid_t pid;
46 #ifdef _DEBUG
47         printf( "Q_Exec damnit\n" );
48 #endif
49         switch ( ( pid = fork() ) )
50         {
51         default:
52                 if ( waitfor ) {
53                         waitpid( pid, NULL, 0 );
54                 }
55                 break;
56         case -1:
57                 return true;
58                 break;
59         case 0:
60                 // always concat the command on linux
61                 if ( cmd ) {
62                         strcpy( fullcmd, cmd );
63                 }
64                 else{
65                         fullcmd[0] = '\0';
66                 }
67                 if ( cmdline ) {
68                         strcat( fullcmd, " " );
69                         strcat( fullcmd, cmdline );
70                 }
71                 pCmd = fullcmd;
72                 while ( *pCmd == ' ' )
73                         pCmd++;
74 #ifdef _DEBUG
75                 printf( "Running system...\n" );
76                 printf( "Command: %s\n", pCmd );
77 #endif
78                 system( pCmd );
79 #ifdef _DEBUG
80                 printf( "system() returned\n" );
81 #endif
82                 _exit( 0 );
83                 break;
84         }
85         return true;
86 }
87
88 #elif defined( WIN32 )
89
90 #include <windows.h>
91
92 // NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess
93 bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor ){
94         PROCESS_INFORMATION ProcessInformation;
95         STARTUPINFO startupinfo = {0};
96         DWORD dwCreationFlags;
97         GetStartupInfo( &startupinfo );
98         if ( bCreateConsole ) {
99                 dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
100         }
101         else{
102                 dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
103         }
104         const char *pCmd;
105         char *pCmdline;
106         pCmd = cmd;
107         if ( pCmd ) {
108                 while ( *pCmd == ' ' )
109                         pCmd++;
110         }
111         pCmdline = cmdline;
112         if ( pCmdline ) {
113                 while ( *pCmdline == ' ' )
114                         pCmdline++;
115         }
116
117         if ( CreateProcess(
118                          pCmd,
119                          pCmdline,
120                          NULL,
121                          NULL,
122                          FALSE,
123                          dwCreationFlags,
124                          NULL,
125                          execdir,
126                          &startupinfo,
127                          &ProcessInformation
128                          ) ) {
129                 if ( waitfor ) {
130                         WaitForSingleObject( ProcessInformation.hProcess, INFINITE );
131                 }
132                 return true;
133         }
134         return false;
135 }
136
137 #endif