Fixed references to BuildCmdLine and my_system
[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 #if defined(_MSC_VER)
28 #include <io.h>
29 #else
30 #include <sys/stat.h>
31 #endif
32
33 #define X_OK 1
34
35 /*!
36 Call an external program with arguements
37 */
38
39 //char *ExePathList[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
40 char *ExePathList[] = {NULL, NULL};                     /* First entry may be overwritten, so use two. */
41
42 int
43 my_system (const char *cmd)
44 {
45   int argsStart, e, i = 0;
46   char *cmdLine = NULL;
47
48   argsStart = strstr (cmd, " ") - cmd;
49
50   // try to find the command in predefined path's
51   while (ExePathList[i])
52     {
53       cmdLine = (char *) Safe_malloc (strlen (ExePathList[i]) + strlen (cmd) + 10);
54       strcpy (cmdLine, ExePathList[i]); // the path
55
56       strcat (cmdLine, DIR_SEPARATOR_STRING);
57       strncat (cmdLine, cmd, argsStart);        // the command
58
59 #if NATIVE_WIN32
60       strcat (cmdLine, ".exe");
61 #endif
62
63       if (access (cmdLine, X_OK) == 0)
64         {
65           // the arguments
66           strcat (cmdLine, cmd + argsStart);
67           break;
68         }
69       free (cmdLine);
70       cmdLine = NULL;
71       i++;
72     }
73
74   if (verboseExec)
75     {
76       printf ("+ %s\n", cmdLine ? cmdLine : cmd);
77     }
78
79   if (cmdLine)
80     {
81       // command found in predefined path
82       e = system (cmdLine);
83       free (cmdLine);
84     }
85   else
86     {
87       // trust on $PATH
88       e = system (cmd);
89     }
90   return e;
91 }
92