4 * (C) COPYRIGHT CRAY RESEARCH, INC.
5 * UNPUBLISHED PROPRIETARY INFORMATION.
8 /**********************************************************
10 * UNICOS Feature Test and Evaluation - Cray Research, Inc.
12 * FUNCTION NAME : search_path
14 * FUNCTION TITLE : search PATH locations for desired filename
17 * int search_path(cmd, res_path, access_mode, fullpath)
23 * AUTHOR : Richard Logan
25 * INITIAL RELEASE : UNICOS 7.0
28 * Search_path will walk through PATH and attempt to find "cmd". If cmd is
29 * a full or relative path, it is checked but PATH locations are not scanned.
30 * search_path will put the resulting path in res_path. It is assumed
31 * that res_path points to a string that is at least PATH_MAX
32 * (or MAXPATHLEN on the suns) in size. Access_mode is just as is
33 * says, the mode to be used on access to determine if cmd can be found.
34 * If fullpath is set, res_path will contain the full path to cmd.
35 * If it is not set, res_path may or may not contain the full path to cmd.
36 * If fullpath is not set, the path in PATH prepended to cmd is used,
37 * which could be a relative path. If fullpath is set, the current
38 * directory is prepended to path/cmd before access is called.
39 * If cmd is found, search_path will return 0. If cmd cannot be
40 * found, 1 is returned. If an error has occurred, -1 is returned
41 * and an error mesg is placed in res_path.
42 * If the length of path/cmd is larger then PATH_MAX, then that path
43 * location is skipped.
45 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
51 #include <sys/errno.h>
52 #include <sys/param.h>
64 * Make sure PATH_MAX is defined. Define it to MAXPATHLEN, if set. Otherwise
70 #else /* MAXPATHLEN */
71 #define PATH_MAX MAXPATHLEN
72 #endif /* MAXPATHLEN */
85 printf("missing argument\n");
89 for(ind=1;ind < argc; ind++) {
90 if ( search_path(argv[ind], path, F_OK, 0) < 0 ) {
91 printf("ERROR: %s\n", path);
94 printf("path of %s is %s\n", argv[ind], path);
105 search_path(cmd, res_path, access_mode, fullpath)
106 char *cmd; /* The requested filename */
107 char *res_path; /* The resulting path or error mesg */
108 int access_mode; /* the mode used by access(2) */
109 int fullpath; /* if set, cwd will be prepended to all non-full paths */
111 char *cp; /* used to scan PATH for directories */
112 int ret; /* return value from access */
114 char tmppath[PATH_MAX];
115 char curpath[PATH_MAX];
121 printf("search_path: cmd = %s, access_mode = %d, fullpath = %d\n", cmd, access_mode, fullpath);
125 * full or relative path was given
127 if ( (cmd[0] == '/') || ( (cp=strchr(cmd, '/')) != NULL )) {
128 if ( access(cmd, access_mode) == 0 ) {
130 if ( cmd[0] != '/' ) { /* relative path */
131 if ( getcwd(curpath, PATH_MAX) == NULL ) {
132 strcpy(res_path, curpath);
135 if ( (strlen(curpath) + strlen(cmd) + 1) > (size_t)PATH_MAX ) {
136 sprintf(res_path, "cmd (as relative path) and cwd is longer than %d",
140 sprintf(res_path, "%s/%s", curpath, cmd);
143 strcpy(res_path, cmd);
147 sprintf(res_path, "file %s not found", cmd);
152 /* get the PATH variable */
153 if ( (pathenv=getenv("PATH")) == NULL) {
154 /* no path to scan, return */
155 sprintf(res_path, "Unable to get PATH env. variable");
160 * walk through each path in PATH.
161 * Each path in PATH is placed in tmppath.
162 * pathenv cannot be modified since it will affect PATH.
163 * If a signal came in while we have modified the PATH
164 * memory, we could create a problem for the caller.
178 path = ++cp; /* already set on first iteration */
180 /* find end of current path */
182 for (; ((*cp != ':') && (*cp != '\0')); cp++);
185 * copy path to tmppath so it can be NULL terminated
186 * and so we do not modify path memory.
188 strncpy(tmppath, path, (cp-path) );
189 tmppath[cp-path]='\0';
191 printf("search_path: tmppath = %s\n", tmppath);
195 lastpath=1; /* this is the last path entry */
197 /* Check lengths so not to overflow res_path */
198 if ( strlen(tmppath) + strlen(cmd) + 2 > (size_t)PATH_MAX ) {
203 sprintf(res_path, "%s/%s", tmppath, cmd);
205 printf("search_path: res_path = '%s'\n", res_path);
209 /* if the path is not full at this point, prepend the current
210 * path to get the full path.
211 * Note: this could not be wise to do when under a protected
215 if ( fullpath && res_path[0] != '/' ) { /* not a full path */
216 if ( curpath[0] == '\0' ) {
217 if ( getcwd(curpath, PATH_MAX) == NULL ) {
218 strcpy(res_path, curpath);
222 if ( (strlen(curpath) + strlen(res_path) + 2) > (size_t)PATH_MAX ) {
226 sprintf(tmppath, "%s/%s", curpath, res_path);
227 strcpy(res_path, tmppath);
229 printf("search_path: full res_path= '%s'\n", res_path);
235 if ( (ret=access(res_path, access_mode)) == 0 ) {
237 printf("search_path: found res_path = %s\n", res_path);
246 "Unable to find file, %d path/file strings were too long", toolong);
248 strcpy(res_path, "Unable to find file");
249 return 1; /* not found */