bae66caadf99658d3b8a0a777c854994b0f0f4f4
[fw/sdcc] / support / Util / MySystem.c
1 /*-------------------------------------------------------------------------
2   MySystem - SDCC Support function
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #include "common.h"
26 #include "newalloc.h"
27 #include <io.h>
28
29 #define X_OK 1
30
31 /*!
32 Call an external program with arguements
33 */
34
35 //char *ExePathList[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
36 char *ExePathList[] = {NULL, NULL};                     /* First entry may be overwritten, so use two. */
37
38 int
39 my_system (const char *cmd)
40 {
41   int argsStart, e, i = 0;
42   char *cmdLine = NULL;
43
44   argsStart = strstr (cmd, " ") - cmd;
45
46   // try to find the command in predefined path's
47   while (ExePathList[i])
48     {
49       cmdLine = (char *) Safe_malloc (strlen (ExePathList[i]) + strlen (cmd) + 10);
50       strcpy (cmdLine, ExePathList[i]); // the path
51
52       strcat (cmdLine, DIR_SEPARATOR_STRING);
53       strncat (cmdLine, cmd, argsStart);        // the command
54
55 #if NATIVE_WIN32
56       strcat (cmdLine, ".exe");
57 #endif
58
59       if (access (cmdLine, X_OK) == 0)
60         {
61           // the arguments
62           strcat (cmdLine, cmd + argsStart);
63           break;
64         }
65       free (cmdLine);
66       cmdLine = NULL;
67       i++;
68     }
69
70   if (verboseExec)
71     {
72       printf ("+ %s\n", cmdLine ? cmdLine : cmd);
73     }
74
75   if (cmdLine)
76     {
77       // command found in predefined path
78       e = system (cmdLine);
79       free (cmdLine);
80     }
81   else
82     {
83       // trust on $PATH
84       e = system (cmd);
85     }
86   return e;
87 }
88