Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / util / findme.c
blobc5092144595d14a064dbceacd577bff590d920d2
1 /** \ingroup popt
2 * \file popt/findme.c
3 */
5 /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
6 file accompanying popt source distributions, available from
7 ftp://ftp.rpm.org/pub/rpm/dist.
9 alloca replaced with malloc()/free() pair */
11 #include "system.h"
12 #include "findme.h"
14 const char * findProgramPath(const char * argv0) {
15 char * path = getenv("PATH");
16 char * pathbuf;
17 char * start, * chptr;
18 char * buf;
20 if (argv0 == NULL) return NULL; /* XXX can't happen */
21 /* If there is a / in the argv[0], it has to be an absolute path */
22 if (strchr(argv0, '/'))
23 return xstrdup(argv0);
25 if (path == NULL) return NULL;
27 start = pathbuf = malloc(strlen(path) + 1);
28 if (pathbuf == NULL) return NULL;
29 buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
30 if (buf == NULL) { /* XXX can't happen */
31 free(pathbuf);
32 return NULL;
34 strcpy(pathbuf, path);
36 chptr = NULL;
37 /*@-branchstate@*/
38 do {
39 if ((chptr = strchr(start, ':')))
40 *chptr = '\0';
41 sprintf(buf, "%s/%s", start, argv0);
43 if (!access(buf, X_OK)) {
44 free(pathbuf);
45 return buf;
48 if (chptr)
49 start = chptr + 1;
50 else
51 start = NULL;
52 } while (start && *start);
53 /*@=branchstate@*/
55 free(buf);
56 free(pathbuf);
58 return NULL;