From: borutr Date: Sun, 29 Feb 2004 20:08:39 +0000 (+0000) Subject: src/Makefile.in, src/SDCCutil.c, support/Util/findme.c, X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=3774d10bc17b9409d341b4897b7f332003cec3cb;p=fw%2Fsdcc 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() git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@3237 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- diff --git a/ChangeLog b/ChangeLog index d3bc5463..d50f997b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-02-29 Borut Razem + + * 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 * src/SDCCpeeph.h, diff --git a/src/Makefile.in b/src/Makefile.in index a7d91aa5..26478f8b 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -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 \ diff --git a/src/SDCCutil.c b/src/SDCCutil.c index c0c317be..d100aa46 100644 --- a/src/SDCCutil.c +++ b/src/SDCCutil.c @@ -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 index 00000000..a950e500 --- /dev/null +++ b/support/Util/findme.c @@ -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 index 00000000..a016b867 --- /dev/null +++ b/support/Util/findme.h @@ -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 index 00000000..6bb01cd4 --- /dev/null +++ b/support/Util/system.h @@ -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 +#include +#include +#include + +#define xstrdup(_str) strdup(_str) + +#endif /* _SYSTEM_H */