~ubuntu-branches/ubuntu/edgy/pilot-link/edgy

« back to all changes in this revision

Viewing changes to popt/findme.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Rousseau
  • Date: 2006-09-22 11:51:36 UTC
  • mfrom: (3.1.8 edgy)
  • Revision ID: james.westby@ubuntu.com-20060922115136-qqmy17bx8j5x0y72
Tags: 0.12.1-5
* urgency medium since libpisock-dev was not usable to build any package
* libpisock-dev now depends on libusb-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** \ingroup popt
 
2
 * \file popt/findme.c
 
3
 */
 
4
 
 
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. */
 
8
 
 
9
#include "system.h"
 
10
#include "findme.h"
 
11
 
 
12
const char * findProgramPath(const char * argv0) {
 
13
    char * path = getenv("PATH");
 
14
    char * pathbuf;
 
15
    char * start, * chptr;
 
16
    char * buf;
 
17
 
 
18
    if (argv0 == NULL) return NULL;     /* XXX can't happen */
 
19
    /* If there is a / in the argv[0], it has to be an absolute path */
 
20
    if (strchr(argv0, '/'))
 
21
        return xstrdup(argv0);
 
22
 
 
23
    if (path == NULL) return NULL;
 
24
 
 
25
    start = pathbuf = alloca(strlen(path) + 1);
 
26
    buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
 
27
    if (buf == NULL) return NULL;       /* XXX can't happen */
 
28
    strcpy(pathbuf, path);
 
29
 
 
30
    chptr = NULL;
 
31
    /*@-branchstate@*/
 
32
    do {
 
33
        if ((chptr = strchr(start, ':')))
 
34
            *chptr = '\0';
 
35
        sprintf(buf, "%s/%s", start, argv0);
 
36
 
 
37
        if (!access(buf, X_OK))
 
38
            return buf;
 
39
 
 
40
        if (chptr) 
 
41
            start = chptr + 1;
 
42
        else
 
43
            start = NULL;
 
44
    } while (start && *start);
 
45
    /*@=branchstate@*/
 
46
 
 
47
    free(buf);
 
48
 
 
49
    return NULL;
 
50
}