1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 23:05:47 +02:00

Reformat to be Unix format. Check the program

with access() to see if executable.
This commit is contained in:
Alain Magloire 2002-10-17 15:52:57 +00:00
parent b968757017
commit cad297693f

View file

@ -1,74 +1,76 @@
/* /*
* pfind.c - Search for a binary in $PATH. * pfind.c - Search for a binary in $PATH.
*/ */
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#ifndef PATH_MAX #ifndef PATH_MAX
#define PATH_MAX 1024 #define PATH_MAX 1024
#endif #endif
char *pfind( const char *name ) char *pfind(const char *name)
{ {
char *tok; char *tok;
char *sp; char *sp;
char *path = getenv( "PATH" ); char *path;
char FullPath[PATH_MAX+1]; char fullpath[PATH_MAX+1];
if( name == NULL ) if (name == NULL) {
{ fprintf(stderr, "pfind(): Null argument.\n");
fprintf( stderr, "pfind(): Null argument.\n" ); return NULL;
return NULL; }
}
/* For absolute namer or name with a path, check if it is an executable. */
if( path == NULL || strlen( path ) <= 0 ) if (name[0] == '/' || name[0] == '.') {
{ if (access(name, X_OK | R_OK) == 0) {
fprintf( stderr, "Unable to get $PATH.\n" ); return strdup(name);
return NULL; }
} return NULL;
}
// The value return by getenv() is readonly */
path = strdup( path ); /* Search in the PATH environment. */
path = getenv("PATH");
tok = strtok_r( path, ":", &sp ); if (path == NULL || strlen(path) <= 0) {
while( tok != NULL ) fprintf(stderr, "Unable to get $PATH.\n");
{ return NULL;
//strcpy( FullPath, tok ); }
//strcat( FullPath, "/" );
//strcat( FullPath, name ); // The value return by getenv() is readonly */
snprintf(FullPath, sizeof(FullPath) - 1, "%s/%s", tok, name); path = strdup(path);
if( access( FullPath, X_OK | R_OK ) == 0 ) tok = strtok_r(path, ":", &sp);
{ while (tok != NULL) {
free( path ); snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name);
return strdup(FullPath);
} if (access(fullpath, X_OK | R_OK) == 0) {
free(path);
tok = strtok_r( NULL, ":", &sp ); return strdup(fullpath);
} }
free( path ); tok = strtok_r(NULL, ":", &sp);
return NULL; }
}
free(path);
#ifdef BUILD_WITH_MAIN return NULL;
int main( int argc, char **argv ) }
{
int i; #ifdef BUILD_WITH_MAIN
char *fullpath; int main(int argc, char **argv)
{
for( i=1; i<argc; i++ ) int i;
{ char *fullpath;
fullpath = pfind( argv[i] );
if( fullpath == NULL ) for (i = 1; i<argc; i++) {
printf( "Unable to find %s in $PATH.\n", argv[i] ); fullpath = pfind(argv[i]);
else if (fullpath == NULL)
printf( "Found %s @ %s.\n", argv[i], fullpath ); printf("Unable to find %s in $PATH.\n", argv[i]);
} else
} printf( "Found %s @ %s.\n", argv[i], fullpath );
#endif }
}
#endif