]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/libs/cmdlib/cmdlib.cpp
Include netRadiant source in this GIT
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / 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 {
44   char fullcmd[2048];
45   char *pCmd;
46   pid_t pid;
47 #ifdef _DEBUG
48   printf("Q_Exec damnit\n");
49 #endif
50   switch ((pid = fork()))
51   {
52   default:
53     if(waitfor)
54       waitpid(pid, NULL, 0);
55     break;
56   case -1:
57     return true;
58     break;
59   case 0:
60     // always concat the command on linux
61     if (cmd)
62     {
63       strcpy(fullcmd, cmd);
64     }
65     else
66       fullcmd[0] = '\0';
67     if (cmdline)
68     {
69       strcat(fullcmd, " ");
70       strcat(fullcmd, cmdline);
71     }
72     pCmd = fullcmd;
73     while (*pCmd == ' ')
74       pCmd++;
75 #ifdef _DEBUG
76     printf("Running system...\n");
77     printf("Command: %s\n", pCmd);
78 #endif
79     system( pCmd );
80 #ifdef _DEBUG
81     printf ("system() returned\n");
82 #endif
83     _exit (0);
84     break;
85   }
86   return true;
87 }
88
89 #elif defined(WIN32)
90
91 #include <windows.h>
92
93 // NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess
94 bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor)
95 {
96   PROCESS_INFORMATION ProcessInformation;
97   STARTUPINFO startupinfo = {0};
98   DWORD dwCreationFlags;
99   GetStartupInfo (&startupinfo);
100   if (bCreateConsole)
101     dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
102   else
103     dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
104   const char *pCmd;
105   char *pCmdline;
106   pCmd = cmd;
107   if (pCmd)
108   {
109     while (*pCmd == ' ')
110       pCmd++;
111   }
112   pCmdline = cmdline;
113   if (pCmdline)
114   {
115     while (*pCmdline == ' ')
116       pCmdline++;
117   }
118
119   if (CreateProcess(
120                     pCmd,
121                     pCmdline,
122                     NULL,
123                     NULL,
124                     FALSE,
125                     dwCreationFlags,
126                     NULL,
127                     execdir,
128                     &startupinfo,
129                     &ProcessInformation
130                     ))
131   {
132     if(waitfor)
133       WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
134     return true;
135   }
136   return false;
137 }
138
139 #endif
140