2006-05-17 04:04:09 +04:00
/** \ingroup popt
* \ file popt / findme . c
*/
/* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
2001-10-23 18:16:22 +04:00
file accompanying popt source distributions , available from
2006-05-17 04:04:09 +04:00
ftp : //ftp.rpm.org/pub/rpm/dist. */
2001-10-23 18:16:22 +04:00
# include "system.h"
# include "findme.h"
const char * findProgramPath ( const char * argv0 ) {
char * path = getenv ( " PATH " ) ;
char * pathbuf ;
char * start , * chptr ;
2006-05-17 04:04:09 +04:00
char * buf ;
2001-10-23 18:16:22 +04:00
2006-05-17 04:04:09 +04:00
if ( argv0 = = NULL ) return NULL ; /* XXX can't happen */
/* If there is a / in the argv[0], it has to be an absolute path */
2001-10-23 18:16:22 +04:00
if ( strchr ( argv0 , ' / ' ) )
return xstrdup ( argv0 ) ;
2006-05-17 04:04:09 +04:00
if ( path = = NULL ) return NULL ;
2001-10-23 18:16:22 +04:00
2006-07-31 08:30:55 +04:00
start = pathbuf = ( char * ) alloca ( strlen ( path ) + 1 ) ;
buf = ( char * ) malloc ( strlen ( path ) + strlen ( argv0 ) + sizeof ( " / " ) ) ;
2006-05-17 04:04:09 +04:00
if ( buf = = NULL ) return NULL ; /* XXX can't happen */
2001-10-23 18:16:22 +04:00
strcpy ( pathbuf , path ) ;
chptr = NULL ;
2006-05-17 04:04:09 +04:00
/*@-branchstate@*/
2001-10-23 18:16:22 +04:00
do {
if ( ( chptr = strchr ( start , ' : ' ) ) )
* chptr = ' \0 ' ;
sprintf ( buf , " %s/%s " , start , argv0 ) ;
2006-05-17 04:04:09 +04:00
if ( ! access ( buf , X_OK ) )
return buf ;
2001-10-23 18:16:22 +04:00
if ( chptr )
start = chptr + 1 ;
else
start = NULL ;
} while ( start & & * start ) ;
2006-05-17 04:04:09 +04:00
/*@=branchstate@*/
2001-10-23 18:16:22 +04:00
free ( buf ) ;
return NULL ;
}