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:
parent
711b80c01d
commit
a509b0baaa
1 changed files with 78 additions and 69 deletions
|
@ -13,36 +13,45 @@
|
||||||
#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];
|
||||||
|
|
||||||
|
/* Sanity check. */
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
fprintf(stderr, "pfind(): Null argument.\n");
|
fprintf(stderr, "pfind(): Null argument.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path == NULL || strlen( path ) <= 0) {
|
/* For absolute name or name with a path, check if it is an executable. */
|
||||||
|
if (name[0] == '/' || name[0] == '.') {
|
||||||
|
if (access(name, X_OK | R_OK) == 0) {
|
||||||
|
return strdup(name);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Search in the PATH environment. */
|
||||||
|
path = getenv("PATH" );
|
||||||
|
|
||||||
|
if (path == NULL || strlen(path) <= 0) {
|
||||||
fprintf(stderr, "Unable to get $PATH.\n");
|
fprintf(stderr, "Unable to get $PATH.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The value return by getenv() is readonly */
|
/* The value return by getenv() is readonly */
|
||||||
path = strdup(path);
|
path = strdup(path);
|
||||||
|
|
||||||
tok = strtok_r(path, ":", &sp);
|
tok = strtok_r(path, ":", &sp);
|
||||||
while (tok != NULL) {
|
while (tok != NULL) {
|
||||||
//strcpy(FullPath, tok);
|
snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name);
|
||||||
//strcat(FullPath, "/");
|
|
||||||
//strcat(FullPath, name);
|
|
||||||
snprintf(FullPath, sizeof(FullPath) - 1, "%s/%s", tok, name);
|
|
||||||
|
|
||||||
if (access(FullPath, X_OK | R_OK) == 0) {
|
if (access(fullpath, X_OK | R_OK) == 0) {
|
||||||
free(path);
|
free(path);
|
||||||
return strdup(FullPath);
|
return strdup(fullpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
tok = strtok_r( NULL, ":", &sp );
|
tok = strtok_r( NULL, ":", &sp );
|
||||||
|
|
Loading…
Add table
Reference in a new issue