1 /* $Header: /cvsroot/ltp/ltp/lib/search_path.c,v 1.4 2009/07/20 10:59:32 vapier Exp $ */
4 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * Further, this software is distributed without any warranty that it is
15 * free of the rightful claim of any third person regarding infringement
16 * or the like. Any license provided herein, whether implied or
17 * otherwise, applies only to this software file. Patent licenses, if
18 * any, provided herein do not apply to combinations of this program with
19 * other software, or any other product whatsoever.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write the Free Software Foundation, Inc., 59
23 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
25 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
26 * Mountain View, CA 94043, or:
30 * For further information regarding this notice, see:
32 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
36 /**********************************************************
38 * UNICOS Feature Test and Evaluation - Cray Research, Inc.
40 * FUNCTION NAME : search_path
42 * FUNCTION TITLE : search PATH locations for desired filename
45 * int search_path(cmd, res_path, access_mode, fullpath)
51 * AUTHOR : Richard Logan
53 * INITIAL RELEASE : UNICOS 7.0
56 * Search_path will walk through PATH and attempt to find "cmd". If cmd is
57 * a full or relative path, it is checked but PATH locations are not scanned.
58 * search_path will put the resulting path in res_path. It is assumed
59 * that res_path points to a string that is at least PATH_MAX
60 * (or MAXPATHLEN on the suns) in size. Access_mode is just as is
61 * says, the mode to be used on access to determine if cmd can be found.
62 * If fullpath is set, res_path will contain the full path to cmd.
63 * If it is not set, res_path may or may not contain the full path to cmd.
64 * If fullpath is not set, the path in PATH prepended to cmd is used,
65 * which could be a relative path. If fullpath is set, the current
66 * directory is prepended to path/cmd before access is called.
67 * If cmd is found, search_path will return 0. If cmd cannot be
68 * found, 1 is returned. If an error has occurred, -1 is returned
69 * and an error mesg is placed in res_path.
70 * If the length of path/cmd is larger then PATH_MAX, then that path
71 * location is skipped.
73 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
80 #include <sys/param.h>
91 * Make sure PATH_MAX is defined. Define it to MAXPATHLEN, if set. Otherwise
97 #else /* MAXPATHLEN */
98 #define PATH_MAX MAXPATHLEN
99 #endif /* MAXPATHLEN */
100 #endif /* PATH_MAX */
112 printf("missing argument\n");
116 for(ind
=1;ind
< argc
; ind
++) {
117 if ( search_path(argv
[ind
], path
, F_OK
, 0) < 0 ) {
118 printf("ERROR: %s\n", path
);
121 printf("path of %s is %s\n", argv
[ind
], path
);
132 search_path(cmd
, res_path
, access_mode
, fullpath
)
133 char *cmd
; /* The requested filename */
134 char *res_path
; /* The resulting path or error mesg */
135 int access_mode
; /* the mode used by access(2) */
136 int fullpath
; /* if set, cwd will be prepended to all non-full paths */
138 char *cp
; /* used to scan PATH for directories */
139 int ret
; /* return value from access */
141 char tmppath
[PATH_MAX
];
142 char curpath
[PATH_MAX
];
148 printf("search_path: cmd = %s, access_mode = %d, fullpath = %d\n", cmd
, access_mode
, fullpath
);
152 * full or relative path was given
154 if ( (cmd
[0] == '/') || ( (cp
=strchr(cmd
, '/')) != NULL
)) {
155 if ( access(cmd
, access_mode
) == 0 ) {
157 if ( cmd
[0] != '/' ) { /* relative path */
158 if ( getcwd(curpath
, PATH_MAX
) == NULL
) {
159 strcpy(res_path
, curpath
);
162 if ( (strlen(curpath
) + strlen(cmd
) + 1) > (size_t)PATH_MAX
) {
163 sprintf(res_path
, "cmd (as relative path) and cwd is longer than %d",
167 sprintf(res_path
, "%s/%s", curpath
, cmd
);
170 strcpy(res_path
, cmd
);
174 sprintf(res_path
, "file %s not found", cmd
);
179 /* get the PATH variable */
180 if ( (pathenv
=getenv("PATH")) == NULL
) {
181 /* no path to scan, return */
182 sprintf(res_path
, "Unable to get PATH env. variable");
187 * walk through each path in PATH.
188 * Each path in PATH is placed in tmppath.
189 * pathenv cannot be modified since it will affect PATH.
190 * If a signal came in while we have modified the PATH
191 * memory, we could create a problem for the caller.
205 path
= ++cp
; /* already set on first iteration */
207 /* find end of current path */
209 for (; ((*cp
!= ':') && (*cp
!= '\0')); cp
++);
212 * copy path to tmppath so it can be NULL terminated
213 * and so we do not modify path memory.
215 strncpy(tmppath
, path
, (cp
-path
) );
216 tmppath
[cp
-path
]='\0';
218 printf("search_path: tmppath = %s\n", tmppath
);
222 lastpath
=1; /* this is the last path entry */
224 /* Check lengths so not to overflow res_path */
225 if ( strlen(tmppath
) + strlen(cmd
) + 2 > (size_t)PATH_MAX
) {
230 sprintf(res_path
, "%s/%s", tmppath
, cmd
);
232 printf("search_path: res_path = '%s'\n", res_path
);
236 /* if the path is not full at this point, prepend the current
237 * path to get the full path.
238 * Note: this could not be wise to do when under a protected
242 if ( fullpath
&& res_path
[0] != '/' ) { /* not a full path */
243 if ( curpath
[0] == '\0' ) {
244 if ( getcwd(curpath
, PATH_MAX
) == NULL
) {
245 strcpy(res_path
, curpath
);
249 if ( (strlen(curpath
) + strlen(res_path
) + 2) > (size_t)PATH_MAX
) {
253 sprintf(tmppath
, "%s/%s", curpath
, res_path
);
254 strcpy(res_path
, tmppath
);
256 printf("search_path: full res_path= '%s'\n", res_path
);
262 if ( (ret
=access(res_path
, access_mode
)) == 0 ) {
264 printf("search_path: found res_path = %s\n", res_path
);
273 "Unable to find file, %d path/file strings were too long", toolong
);
275 strcpy(res_path
, "Unable to find file");
276 return 1; /* not found */