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