Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / lib / search_path.c
bloba35f0463b92982ba273aea8e382ef6035c26130a
1 /* $Header: /cvsroot/ltp/ltp/lib/search_path.c,v 1.4 2009/07/20 10:59:32 vapier Exp $ */
3 /*
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:
28 * http://www.sgi.com
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
44 * SYNOPSIS:
45 * int search_path(cmd, res_path, access_mode, fullpath)
46 * char *cmd;
47 * char *res_path;
48 * int access_mode;
49 * int fullpath;
51 * AUTHOR : Richard Logan
53 * INITIAL RELEASE : UNICOS 7.0
55 * DESCRIPTION
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 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
75 #include <stdio.h>
76 #include <unistd.h>
77 #include <string.h>
78 #include <stdlib.h>
79 #include <errno.h>
80 #include <sys/param.h>
81 #include <sys/stat.h>
84 struct stat stbuf;
86 #ifndef AS_CMD
87 #define AS_CMD 0
88 #endif
91 * Make sure PATH_MAX is defined. Define it to MAXPATHLEN, if set. Otherwise
92 * set it to 1024.
94 #ifndef PATH_MAX
95 #ifndef MAXPATHLEN
96 #define PATH_MAX 1024
97 #else /* MAXPATHLEN */
98 #define PATH_MAX MAXPATHLEN
99 #endif /* MAXPATHLEN */
100 #endif /* PATH_MAX */
103 #if AS_CMD
104 main(argc, argv)
105 int argc;
106 char **argv;
108 char path[PATH_MAX];
109 int ind;
111 if (argc <= 1 ) {
112 printf("missing argument\n");
113 exit(1);
116 for(ind=1;ind < argc; ind++) {
117 if ( search_path(argv[ind], path, F_OK, 0) < 0 ) {
118 printf("ERROR: %s\n", path);
120 else {
121 printf("path of %s is %s\n", argv[ind], path);
127 #endif
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 */
140 char *pathenv;
141 char tmppath[PATH_MAX];
142 char curpath[PATH_MAX];
143 char *path;
144 int lastpath;
145 int toolong=0;
147 #if DEBUG
148 printf("search_path: cmd = %s, access_mode = %d, fullpath = %d\n", cmd, access_mode, fullpath);
149 #endif
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);
160 return -1;
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",
164 PATH_MAX);
165 return -1;
167 sprintf(res_path, "%s/%s", curpath, cmd);
169 else
170 strcpy(res_path, cmd);
171 return 0;
173 else {
174 sprintf(res_path, "file %s not found", cmd);
175 return -1;
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");
183 return -1;
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.
194 curpath[0]='\0';
196 cp = pathenv;
197 path = pathenv;
198 lastpath = 0;
199 for (;;) {
201 if ( lastpath )
202 break;
204 if ( cp != pathenv )
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';
217 #if DEBUG
218 printf("search_path: tmppath = %s\n", tmppath);
219 #endif
221 if ( *cp == '\0' )
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 ) {
226 toolong++;
227 continue;
230 sprintf(res_path, "%s/%s", tmppath, cmd);
231 #if DEBUG
232 printf("search_path: res_path = '%s'\n", res_path);
233 #endif
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
239 * directory.
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);
246 return -1;
249 if ( (strlen(curpath) + strlen(res_path) + 2) > (size_t)PATH_MAX ) {
250 toolong++;
251 continue;
253 sprintf(tmppath, "%s/%s", curpath, res_path);
254 strcpy(res_path, tmppath);
255 #if DEBUG
256 printf("search_path: full res_path= '%s'\n", res_path);
257 #endif
262 if ( (ret=access(res_path, access_mode)) == 0 ) {
263 #if DEBUG
264 printf("search_path: found res_path = %s\n", res_path);
265 #endif
266 return 0;
270 /* return failure */
271 if ( toolong )
272 sprintf(res_path,
273 "Unable to find file, %d path/file strings were too long", toolong);
274 else
275 strcpy(res_path, "Unable to find file");
276 return 1; /* not found */