added my_popen()
[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 "MySystem.h"
27 #include "newalloc.h"
28 #ifdef _WIN32
29 #include <io.h>
30 #else
31 #include <unistd.h>
32 #endif
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 /*!
39 Find the comamnd in one of predefined paths
40
41 NOTE: this function does't do it's job if:
42 - the program name also contains the path
43   (and it seems thet this is always true :-(()
44 - there are no spaces in cmd
45 - the program name contains space characters
46
47 It has to be rewritten.
48 */
49
50 static char *
51 get_path (const char *cmd)
52 {
53   int argsStart, i = 0;
54   char *cmdLine = NULL;
55
56   argsStart = strstr (cmd, " ") - cmd;
57
58   // try to find the command in predefined path's
59   while (ExePathList[i])
60     {
61       cmdLine = (char *) Safe_alloc (strlen (ExePathList[i]) + strlen (cmd) + 10);
62       strcpy (cmdLine, ExePathList[i]); // the path
63
64       strcat (cmdLine, DIR_SEPARATOR_STRING);
65       strncat (cmdLine, cmd, argsStart);        // the command
66
67 #ifdef _WIN32
68       strcat (cmdLine, ".exe");
69       if (access(cmdLine, 0) == 0)
70 #else
71       if (access(cmdLine, X_OK) == 0)
72 #endif
73         {
74           // the arguments
75           strcat (cmdLine, cmd + argsStart);
76           break;
77         }
78       Safe_free (cmdLine);
79       cmdLine = NULL;
80       i++;
81     }
82
83   return cmdLine;
84 }
85
86
87 /*!
88 Call an external program with arguements
89 */
90
91 int
92 my_system (const char *cmd)
93 {
94   int e;
95   char *cmdLine = get_path (cmd);
96
97   if (options.verboseExec) {
98       printf ("+ %s\n", cmdLine ? cmdLine : cmd);
99   }
100
101   if (cmdLine) {
102       // command found in predefined path
103       e = system (cmdLine);
104       Safe_free (cmdLine);
105   }
106   else {
107       // trust on $PATH
108       e = system (cmd);
109   }
110
111   return e;
112 }
113
114
115 /*!
116 Pipe an external program with arguements
117 */
118
119 #ifdef _WIN32
120 #define popen_read(cmd) _popen((cmd), "rt")
121 #else
122 #define popen_read(cmd) popen((cmd), "r")
123 #endif
124
125 FILE *
126 my_popen (const char *cmd)
127 {
128   FILE *fp;
129   char *cmdLine = get_path (cmd);
130
131   if (options.verboseExec) {
132       printf ("+ %s\n", cmdLine ? cmdLine : cmd);
133   }
134
135   if (cmdLine) {
136       // command found in predefined path
137       fp = popen_read (cmdLine);
138       Safe_free (cmdLine);
139   }
140   else {
141       // trust on $PATH
142       fp = popen_read (cmd);
143   }
144
145   return fp;
146 }