From 7269a6502093b7fa21b62f7894e5c01a4d21f811 Mon Sep 17 00:00:00 2001 From: borutr Date: Mon, 10 Mar 2003 20:05:11 +0000 Subject: [PATCH] added my_popen() git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2363 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- support/Util/MySystem.c | 103 +++++++++++++++++++++++++++++----------- support/Util/MySystem.h | 1 + 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/support/Util/MySystem.c b/support/Util/MySystem.c index a574b85c..475e97e8 100644 --- a/support/Util/MySystem.c +++ b/support/Util/MySystem.c @@ -23,33 +23,34 @@ -------------------------------------------------------------------------*/ #include "common.h" +#include "MySystem.h" #include "newalloc.h" -#if defined(_MSC_VER) +#ifdef _WIN32 #include #else -#include -#endif - - -#if !defined(__BORLANDC__) && !defined(_MSC_VER) #include -#else -// No unistd.h in Borland C++ -extern int access (const char *, int); -#define X_OK 1 #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. */ -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; @@ -63,11 +64,12 @@ my_system (const char *cmd) strcat (cmdLine, DIR_SEPARATOR_STRING); strncat (cmdLine, cmd, argsStart); // the command -#if NATIVE_WIN32 +#ifdef _WIN32 strcat (cmdLine, ".exe"); + if (access(cmdLine, 0) == 0) +#else + if (access(cmdLine, X_OK) == 0) #endif - - if (access (cmdLine, X_OK) == 0) { // the arguments strcat (cmdLine, cmd + argsStart); @@ -78,22 +80,67 @@ my_system (const char *cmd) 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); - } + } - if (cmdLine) - { + if (cmdLine) { // command found in predefined path e = system (cmdLine); Safe_free (cmdLine); - } - else - { + } + else { // trust on $PATH e = system (cmd); - } + } + 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; +} diff --git a/support/Util/MySystem.h b/support/Util/MySystem.h index 93eb07f2..f945fe53 100644 --- a/support/Util/MySystem.h +++ b/support/Util/MySystem.h @@ -26,6 +26,7 @@ #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 -- 2.47.2