+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,
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 \
#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.
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
--- /dev/null
+/** \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;
+}
--- /dev/null
+/** \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
--- /dev/null
+/*
+ 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 */