1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2005 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "opal/util/path.h"
#include "opal/util/os_path.h"
#include "opal/util/argv.h"
#include "opal/util/output.h"
static void path_env_load(char *path, int *pargc, char ***pargv);
static char *path_access(char *fname, char *path, int mode);
static char *list_env_get(char *var, char **list);
/**
* Locates a file with certain permissions
*/
char *opal_path_find(char *fname, char **pathv, int mode, char **envv)
{
char *fullpath;
char *delimit;
char *env;
char *pfix;
int i;
/* If absolute path is given, return it without searching. */
if (OPAL_PATH_SEP[0] == *fname) {
return path_access(fname, "", mode);
}
/* Initialize. */
fullpath = NULL;
i = 0;
/* Consider each directory until the file is found. Thus, the
order of directories is important. */
while (pathv[i] && NULL == fullpath) {
/* Replace environment variable at the head of the string. */
if ('$' == *pathv[i]) {
delimit = strchr(pathv[i], OPAL_PATH_SEP[0]);
if (delimit) {
*delimit = '\0';
}
env = list_env_get(pathv[i]+1, envv);
if (delimit) {
*delimit = OPAL_PATH_SEP[0];
}
if (NULL != env) {
if (!delimit) {
fullpath = path_access(fname, env, mode);
} else {
pfix = (char*) malloc(strlen(env) + strlen(delimit) + 1);
if (NULL == pfix) {
return NULL;
}
strcpy(pfix, env);
strcat(pfix, delimit);
fullpath = path_access(fname, pfix, mode);
free(pfix);
}
}
}
else {
fullpath = path_access(fname, pathv[i], mode);
}
i++;
}
return opal_make_filename_os_friendly(fullpath);
}
/*
* Locates a file with certain permissions from a list of search paths
*/
char *opal_path_findv(char *fname, int mode, char **envv, char *wrkdir)
{
char **dirv;
char *fullpath;
char *path;
int dirc;
int i;
bool found_dot = false;
/* Set the local search paths. */
dirc = 0;
dirv = NULL;
if (NULL != (path = list_env_get("PATH", envv))) {
path_env_load(path, &dirc, &dirv);
}
/* Replace the "." path by the working directory. */
if (NULL != wrkdir) {
for (i = 0; i < dirc; ++i) {
if (0 == strcmp(dirv[i], ".")) {
found_dot = true;
free(dirv[i]);
dirv[i] = strdup(wrkdir);
if (NULL == dirv[i]){
return NULL;
}
}
}
}
/* If we didn't find "." in the path and we have a wrkdir, append
the wrkdir to the end of the path */
if (!found_dot && NULL != wrkdir) {
opal_argv_append(&dirc, &dirv, wrkdir);
}
if(NULL == dirv)
return NULL;
fullpath = opal_path_find(fname, dirv, mode, envv);
opal_argv_free(dirv);
return fullpath;
}
/**
* Forms a complete pathname and checks it for existance and
* permissions
*
* Accepts:
* -fname File name
* -path Path prefix
* -mode Target permissions which must be satisfied
*
* Returns:
* -Full pathname of located file Success
* -NULL Failure
*/
static char *path_access(char *fname, char *path, int mode)
{
char *fullpath;
/* Allocate space for the full pathname. */
fullpath = opal_os_path( false, path, fname, NULL );
if( NULL == fullpath )
return NULL;
/* Get status on the full path name to check for existance. Then
check the permissions. */
if (access(fullpath, mode)) {
free(fullpath);
fullpath = NULL;
}
return fullpath;
}
/**
*
* Loads argument array with $PATH env var.
*
* Accepts
* -path String contiaing the $PATH
* -argc Pointer to argc
* -argv Pointer to list of argv
*/
static void path_env_load(char *path, int *pargc, char ***pargv)
{
char *p;
char saved;
if (NULL == path) {
*pargc = 0;
return;
}
/* Loop through the paths (delimited by PATHENVSEP), adding each
one to argv. */
while ('\0' != *path) {
/* Locate the delimiter. */
for (p = path; *p && (*p != OPAL_ENV_SEP); ++p) {
continue;
}
/* Add the path. */
if (p != path) {
saved = *p;
*p = '\0';
opal_argv_append(pargc, pargv, path);
*p = saved;
path = p;
}
/* Skip past the delimiter, if present. */
if (*path) {
++path;
}
}
}
/**
* Gets value of variable in list or environment. Looks in the list first
*
* Accepts:
* -var String variable
* -list Pointer to environment list
*
* Returns:
* -List Pointer to environment list Success
* -NULL Failure
*/
static char *list_env_get(char *var, char **list)
{
size_t n;
if (NULL != list) {
n = strlen(var);
while (NULL != *list) {
if ((0 == strncmp(var, *list, n)) && ('=' == (*list)[n])) {
return (*list + n + 1);
}
++list;
}
}
return getenv(var);
}
|