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