]> git.gag.com Git - fw/sdcc/commitdiff
added my_popen()
authorborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 10 Mar 2003 20:05:11 +0000 (20:05 +0000)
committerborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 10 Mar 2003 20:05:11 +0000 (20:05 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2363 4a8a32a2-be11-0410-ad9d-d568d2c75423

support/Util/MySystem.c
support/Util/MySystem.h

index a574b85c201a0f747b66d34cf4af07e89f4e3642..475e97e8c259097ecd19b87809e4fbde4241357a 100644 (file)
 -------------------------------------------------------------------------*/
 
 #include "common.h"
 -------------------------------------------------------------------------*/
 
 #include "common.h"
+#include "MySystem.h"
 #include "newalloc.h"
 #include "newalloc.h"
-#if defined(_MSC_VER)
+#ifdef _WIN32
 #include <io.h>
 #else
 #include <io.h>
 #else
-#include <sys/stat.h>
-#endif
-
-
-#if !defined(__BORLANDC__) && !defined(_MSC_VER)
 #include <unistd.h>
 #include <unistd.h>
-#else
-// No unistd.h in Borland C++
-extern int access (const char *, int);
-#define X_OK 1
 #endif
 
 #endif
 
-/*!
-Call an external program with arguements
-*/
 
 //char *ExePathList[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
 char *ExePathList[] = {NULL, NULL};                    /* First entry may be overwritten, so use two. */
 
 
 //char *ExePathList[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
 char *ExePathList[] = {NULL, NULL};                    /* First entry may be overwritten, so use two. */
 
-int
-my_system (const char *cmd)
+/*!
+Find the comamnd in one of predefined paths
+
+NOTE: this function does't do it's job if:
+- the program name also contains the path
+  (and it seems thet this is always true :-(()
+- there are no spaces in cmd
+- the program name contains space characters
+
+It has to be rewritten.
+*/
+
+static char *
+get_path (const char *cmd)
 {
 {
-  int argsStart, e, i = 0;
+  int argsStart, i = 0;
   char *cmdLine = NULL;
 
   argsStart = strstr (cmd, " ") - cmd;
   char *cmdLine = NULL;
 
   argsStart = strstr (cmd, " ") - cmd;
@@ -63,11 +64,12 @@ my_system (const char *cmd)
       strcat (cmdLine, DIR_SEPARATOR_STRING);
       strncat (cmdLine, cmd, argsStart);       // the command
 
       strcat (cmdLine, DIR_SEPARATOR_STRING);
       strncat (cmdLine, cmd, argsStart);       // the command
 
-#if NATIVE_WIN32
+#ifdef _WIN32
       strcat (cmdLine, ".exe");
       strcat (cmdLine, ".exe");
+      if (access(cmdLine, 0) == 0)
+#else
+      if (access(cmdLine, X_OK) == 0)
 #endif
 #endif
-
-      if (access (cmdLine, X_OK) == 0)
        {
          // the arguments
          strcat (cmdLine, cmd + argsStart);
        {
          // the arguments
          strcat (cmdLine, cmd + argsStart);
@@ -78,22 +80,67 @@ my_system (const char *cmd)
       i++;
     }
 
       i++;
     }
 
-  if (options.verboseExec)
-    {
+  return cmdLine;
+}
+
+
+/*!
+Call an external program with arguements
+*/
+
+int
+my_system (const char *cmd)
+{
+  int e;
+  char *cmdLine = get_path (cmd);
+
+  if (options.verboseExec) {
       printf ("+ %s\n", cmdLine ? cmdLine : cmd);
       printf ("+ %s\n", cmdLine ? cmdLine : cmd);
-    }
+  }
 
 
-  if (cmdLine)
-    {
+  if (cmdLine) {
       // command found in predefined path
       e = system (cmdLine);
       Safe_free (cmdLine);
       // command found in predefined path
       e = system (cmdLine);
       Safe_free (cmdLine);
-    }
-  else
-    {
+  }
+  else {
       // trust on $PATH
       e = system (cmd);
       // trust on $PATH
       e = system (cmd);
-    }
+  }
+
   return e;
 }
 
   return e;
 }
 
+
+/*!
+Pipe an external program with arguements
+*/
+
+#ifdef _WIN32
+#define popen_read(cmd) _popen((cmd), "rt")
+#else
+#define popen_read(cmd) popen((cmd), "r")
+#endif
+
+FILE *
+my_popen (const char *cmd)
+{
+  FILE *fp;
+  char *cmdLine = get_path (cmd);
+
+  if (options.verboseExec) {
+      printf ("+ %s\n", cmdLine ? cmdLine : cmd);
+  }
+
+  if (cmdLine) {
+      // command found in predefined path
+      fp = popen_read (cmdLine);
+      Safe_free (cmdLine);
+  }
+  else {
+      // trust on $PATH
+      fp = popen_read (cmd);
+  }
+
+  return fp;
+}
index 93eb07f27e7e0ab7c73c9ccfc084247bf0516a4d..f945fe5357413622d6678e09a6e230dc2ac514d6 100644 (file)
@@ -26,6 +26,7 @@
 #define __MYSYSTEM_H
 
 int my_system (const char *cmd) ;
 #define __MYSYSTEM_H
 
 int my_system (const char *cmd) ;
+FILE *my_popen (const char *cmd) ;
 
 extern char *ExePathList[] ;     // List of paths to try to locate exeucatbles
 
 
 extern char *ExePathList[] ;     // List of paths to try to locate exeucatbles