1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 14:55:41 +02:00

Make it a Unix format. Check the path with access().

This commit is contained in:
Alain Magloire 2002-10-16 00:55:24 +00:00
parent 711b80c01d
commit a509b0baaa

View file

@ -1,69 +1,78 @@
/* /*
* 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) { /* Sanity check. */
fprintf(stderr, "pfind(): Null argument.\n"); if (name == NULL) {
return NULL; fprintf(stderr, "pfind(): Null argument.\n");
} return NULL;
}
if (path == NULL || strlen( path ) <= 0) {
fprintf(stderr, "Unable to get $PATH.\n"); /* For absolute name or name with a path, check if it is an executable. */
return NULL; if (name[0] == '/' || name[0] == '.') {
} if (access(name, X_OK | R_OK) == 0) {
return strdup(name);
// The value return by getenv() is readonly */ }
path = strdup(path); return NULL;
}
tok = strtok_r(path, ":", &sp);
while (tok != NULL) { /* Search in the PATH environment. */
//strcpy(FullPath, tok); path = getenv("PATH" );
//strcat(FullPath, "/");
//strcat(FullPath, name); if (path == NULL || strlen(path) <= 0) {
snprintf(FullPath, sizeof(FullPath) - 1, "%s/%s", tok, name); fprintf(stderr, "Unable to get $PATH.\n");
return NULL;
if (access(FullPath, X_OK | R_OK) == 0) { }
free(path);
return strdup(FullPath); /* The value return by getenv() is readonly */
} path = strdup(path);
tok = strtok_r( NULL, ":", &sp ); tok = strtok_r(path, ":", &sp);
} while (tok != NULL) {
snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name);
free(path);
return NULL; if (access(fullpath, X_OK | R_OK) == 0) {
} free(path);
return strdup(fullpath);
#ifdef BUILD_WITH_MAIN }
int main(int argc, char **argv)
{ tok = strtok_r( NULL, ":", &sp );
int i; }
char *fullpath;
free(path);
for (i=1; i<argc; i++) { return NULL;
fullpath = pfind(argv[i]); }
if (fullpath == NULL)
printf("Unable to find %s in $PATH.\n", argv[i]); #ifdef BUILD_WITH_MAIN
else int main(int argc, char **argv)
printf("Found %s @ %s.\n", argv[i], fullpath); {
} int i;
} char *fullpath;
#endif
for (i=1; i<argc; i++) {
fullpath = pfind(argv[i]);
if (fullpath == NULL)
printf("Unable to find %s in $PATH.\n", argv[i]);
else
printf("Found %s @ %s.\n", argv[i], fullpath);
}
}
#endif