src/Makefile.in, src/SDCCutil.c, support/Util/findme.c,
authorborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sun, 29 Feb 2004 20:08:39 +0000 (20:08 +0000)
committerborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sun, 29 Feb 2004 20:08:39 +0000 (20:08 +0000)
support/Util/findme.h, support/Util/system.h: enhance binary relative
search for lib and include by using findProgramPath()

git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@3237 4a8a32a2-be11-0410-ad9d-d568d2c75423

ChangeLog
src/Makefile.in
src/SDCCutil.c
support/Util/findme.c [new file with mode: 0644]
support/Util/findme.h [new file with mode: 0644]
support/Util/system.h [new file with mode: 0644]

index d3bc54633886b43ac7c9c4842bcb9614de3ea784..d50f997b120002d2f58b1c98cef44e3fbf620c8e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2004-02-29  Borut Razem <borut.razem AT siol.net>
+
+       * src/Makefile.in, src/SDCCutil.c, support/Util/findme.c,
+       support/Util/findme.h, support/Util/system.h: enhance binary relative
+       search for lib and include by using findProgramPath()
+
 2004-02-28 Erik Petrich <epetrich AT ivorytower.norman.ok.us>
 
        * src/SDCCpeeph.h,
index a7d91aa5f5b3d35548054e6502dc419fc2e5b7ca..26478f8bffe4baa5070eaa035a7d786714001762 100644 (file)
@@ -20,7 +20,7 @@ ifdef SDCC_SUB_VERSION
 CFLAGS         += -DSDCC_SUB_VERSION_STR=\"$(SDCC_SUB_VERSION)\"
 endif
 
-SLIBOBJS       = SDCCerr.o NewAlloc.o MySystem.o BuildCmd.o dbuf.o
+SLIBOBJS       = SDCCerr.o NewAlloc.o MySystem.o BuildCmd.o dbuf.o findme.o
 
 OBJECTS        = SDCCy.o SDCChasht.o SDCCmain.o \
                  SDCCsymt.o SDCCopt.o SDCCast.o SDCCmem.o SDCCval.o \
index c0c317bee6930677988f2e64babfec74885ed5aa..d100aa463d54e407c00ad1f9a1504dc66524d440 100644 (file)
@@ -32,6 +32,9 @@
 #include "SDCCmacro.h"
 #include "SDCCutil.h"
 #include "newalloc.h"
+#ifndef _WIN32
+#include "findme.h"
+#endif
 
 /** Given an array of name, value string pairs creates a new hash
     containing all of the pairs.
@@ -143,18 +146,29 @@ getBinPath(const char *prel)
 char *
 getBinPath(const char *prel)
 {
-  char *p;
-  size_t len;
   static char path[PATH_MAX];
-    
-  if ((p = strrchr(prel, DIR_SEPARATOR_CHAR)) == NULL)
-    return NULL;
+  const char *ret_path;
+
+  if (NULL != (ret_path = findProgramPath(prel))) {
+    char *p;
+    size_t len;
 
-  len = min((sizeof path) - 1, p - prel);
-  strncpy(path, prel, len);
-  path[len] = '\0';
+    if (NULL != (p = strrchr(ret_path, DIR_SEPARATOR_CHAR)) &&
+      PATH_MAX > (len = p - ret_path)) {
+      memcpy(path, ret_path, len);
+      path[len] = '\0';
+      free((void *)ret_path);
 
-  return path;
+      return path;
+    }
+    else {
+      free((void *)ret_path);
+
+      return NULL;
+    }
+  }
+  else
+    return NULL;
 }
 #endif
 
diff --git a/support/Util/findme.c b/support/Util/findme.c
new file mode 100644 (file)
index 0000000..a950e50
--- /dev/null
@@ -0,0 +1,50 @@
+/** \ingroup popt
+ * \file popt/findme.c
+ */
+
+/* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
+   file accompanying popt source distributions, available from 
+   ftp://ftp.rpm.org/pub/rpm/dist. */
+
+#include "system.h"
+#include "findme.h"
+
+const char * findProgramPath(const char * argv0) {
+    char * path = getenv("PATH");
+    char * pathbuf;
+    char * start, * chptr;
+    char * buf;
+
+    if (argv0 == NULL) return NULL;    /* XXX can't happen */
+    /* If there is a / in the argv[0], it has to be an absolute path */
+    if (strchr(argv0, '/'))
+       return xstrdup(argv0);
+
+    if (path == NULL) return NULL;
+
+    start = pathbuf = alloca(strlen(path) + 1);
+    buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
+    if (buf == NULL) return NULL;      /* XXX can't happen */
+    strcpy(pathbuf, path);
+
+    chptr = NULL;
+    /*@-branchstate@*/
+    do {
+       if ((chptr = strchr(start, ':')))
+           *chptr = '\0';
+       sprintf(buf, "%s/%s", start, argv0);
+
+       if (!access(buf, X_OK))
+           return buf;
+
+       if (chptr) 
+           start = chptr + 1;
+       else
+           start = NULL;
+    } while (start && *start);
+    /*@=branchstate@*/
+
+    free(buf);
+
+    return NULL;
+}
diff --git a/support/Util/findme.h b/support/Util/findme.h
new file mode 100644 (file)
index 0000000..a016b86
--- /dev/null
@@ -0,0 +1,20 @@
+/** \ingroup popt
+ * \file popt/findme.h
+ */
+
+/* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
+   file accompanying popt source distributions, available from 
+   ftp://ftp.rpm.org/pub/rpm/dist. */
+
+#ifndef H_FINDME
+#define H_FINDME
+
+/**
+ * Return absolute path to executable by searching PATH.
+ * @param argv0                name of executable
+ * @return             (malloc'd) absolute path to executable (or NULL)
+ */
+/*@null@*/ const char * findProgramPath(/*@null@*/ const char * argv0)
+       /*@*/;
+
+#endif
diff --git a/support/Util/system.h b/support/Util/system.h
new file mode 100644 (file)
index 0000000..6bb01cd
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+  system.h - includes, required by findme.c
+
+  Copyright (c) 2004 Borut Razem
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+
+  Borut Razem
+  borut.razem@siol.net
+*/ 
+
+#ifndef _SYSTEM_H
+#define _SYSTEM_H
+
+/* findme.c is compiled only on *nix, so includes are *nix specific */
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#define        xstrdup(_str) strdup(_str)
+
+#endif  /* _SYSTEM_H */