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