forked from MRVN-Radiant/MRVN-Radiant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandlib.cpp
142 lines (124 loc) · 3.02 KB
/
commandlib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
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 "commandlib.h"
#include <cstring>
#include <cstdio>
#if defined ( POSIX )
#include <cstdlib>
#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" );
#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 );
#endif
system( pCmd );
#ifdef _DEBUG
printf( "system() returned\n" );
#endif
_exit( 0 );
break;
}
return true;
}
#elif defined( WIN32 )
#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;
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
#include <filesystem>
bool Q_mkdir( const char* path ){
std::error_code err;
std::filesystem::create_directories( path, err );
return !err;
}