xref: /aosp_15_r20/external/ltp/lib/get_path.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2010 Cyril Hrubis [email protected]
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker  * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker  /*
25*49cdfc7eSAndroid Build Coastguard Worker   * Looks for binary prog_name in $PATH.
26*49cdfc7eSAndroid Build Coastguard Worker   *
27*49cdfc7eSAndroid Build Coastguard Worker   * If such file exists and if you are able at least to read it, zero is
28*49cdfc7eSAndroid Build Coastguard Worker   * returned and absolute path to the file is filled into buf. In case buf is
29*49cdfc7eSAndroid Build Coastguard Worker   * too short to hold the absolute path + prog_name for the file we are looking
30*49cdfc7eSAndroid Build Coastguard Worker   * for -1 is returned as well as when there is no such file in all paths in
31*49cdfc7eSAndroid Build Coastguard Worker   * $PATH.
32*49cdfc7eSAndroid Build Coastguard Worker   */
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
42*49cdfc7eSAndroid Build Coastguard Worker 
file_exist(const char * path)43*49cdfc7eSAndroid Build Coastguard Worker static int file_exist(const char *path)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	struct stat st;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if (!access(path, R_OK) && !stat(path, &st) && S_ISREG(st.st_mode))
48*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
tst_get_path(const char * prog_name,char * buf,size_t buf_len)53*49cdfc7eSAndroid Build Coastguard Worker int tst_get_path(const char *prog_name, char *buf, size_t buf_len)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker 	const char *path = (const char *)getenv("PATH");
56*49cdfc7eSAndroid Build Coastguard Worker 	const char *start = path;
57*49cdfc7eSAndroid Build Coastguard Worker 	const char *end;
58*49cdfc7eSAndroid Build Coastguard Worker 	size_t size, ret;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	if (path == NULL)
61*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	do {
64*49cdfc7eSAndroid Build Coastguard Worker 		end = strchr(start, ':');
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 		if (end != NULL)
67*49cdfc7eSAndroid Build Coastguard Worker 			snprintf(buf, MIN(buf_len, (size_t) (end - start + 1)),
68*49cdfc7eSAndroid Build Coastguard Worker 				 "%s", start);
69*49cdfc7eSAndroid Build Coastguard Worker 		else
70*49cdfc7eSAndroid Build Coastguard Worker 			snprintf(buf, buf_len, "%s", start);
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 		size = strlen(buf);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 		/*
75*49cdfc7eSAndroid Build Coastguard Worker 		 * "::" inside $PATH, $PATH ending with ':' or $PATH starting
76*49cdfc7eSAndroid Build Coastguard Worker 		 * with ':' should be expanded into current working directory.
77*49cdfc7eSAndroid Build Coastguard Worker 		 */
78*49cdfc7eSAndroid Build Coastguard Worker 		if (size == 0) {
79*49cdfc7eSAndroid Build Coastguard Worker 			snprintf(buf, buf_len, ".");
80*49cdfc7eSAndroid Build Coastguard Worker 			size = strlen(buf);
81*49cdfc7eSAndroid Build Coastguard Worker 		}
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 		/*
84*49cdfc7eSAndroid Build Coastguard Worker 		 * If there is no '/' ad the end of path from $PATH add it.
85*49cdfc7eSAndroid Build Coastguard Worker 		 */
86*49cdfc7eSAndroid Build Coastguard Worker 		if (buf[size - 1] != '/')
87*49cdfc7eSAndroid Build Coastguard Worker 			ret =
88*49cdfc7eSAndroid Build Coastguard Worker 			    snprintf(buf + size, buf_len - size, "/%s",
89*49cdfc7eSAndroid Build Coastguard Worker 				     prog_name);
90*49cdfc7eSAndroid Build Coastguard Worker 		else
91*49cdfc7eSAndroid Build Coastguard Worker 			ret =
92*49cdfc7eSAndroid Build Coastguard Worker 			    snprintf(buf + size, buf_len - size, "%s",
93*49cdfc7eSAndroid Build Coastguard Worker 				     prog_name);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 		if (buf_len - size > ret && file_exist(buf))
96*49cdfc7eSAndroid Build Coastguard Worker 			return 0;
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 		start = end + 1;
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	} while (end != NULL);
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	return -1;
103*49cdfc7eSAndroid Build Coastguard Worker }
104