cmdlib.cpp 2.9 KB

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