blob: c7bbead55032a59bf28f5ed164c8b1c6f8edd966 [file] [log] [blame]
Subrata Modakca758b72010-05-18 01:28:14 +05301/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Subrata Modakca758b72010-05-18 01:28:14 +053022 */
23
24 /*
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080025 * Looks for binary prog_name in $PATH.
Subrata Modakca758b72010-05-18 01:28:14 +053026 *
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 "test.h"
35
36#include <stdio.h>
37#include <string.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <sys/types.h>
41#include <sys/stat.h>
42
43#define MIN(a, b) ((a)<(b)?(a):(b))
44
45static int file_exist(const char *path)
46{
47 struct stat st;
48
49 if (!access(path, R_OK) && !stat(path, &st) && S_ISREG(st.st_mode))
50 return 1;
51
52 return 0;
53}
54
55int tst_get_path(const char *prog_name, char *buf, size_t buf_len)
56{
Wanlong Gao354ebb42012-12-07 10:10:04 +080057 const char *path = (const char *)getenv("PATH");
Subrata Modakca758b72010-05-18 01:28:14 +053058 const char *start = path;
59 const char *end;
60 size_t size, ret;
61
Subrata Modakca758b72010-05-18 01:28:14 +053062 if (path == NULL)
63 return -1;
64
65 do {
66 end = strchr(start, ':');
67
68 if (end != NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 snprintf(buf, MIN(buf_len, (size_t) (end - start + 1)),
70 "%s", start);
Subrata Modakca758b72010-05-18 01:28:14 +053071 else
72 snprintf(buf, buf_len, "%s", start);
73
74 size = strlen(buf);
75
76 /*
Garrett Cooper936344f2010-12-16 15:12:18 -080077 * "::" inside $PATH, $PATH ending with ':' or $PATH starting
Subrata Modakca758b72010-05-18 01:28:14 +053078 * with ':' should be expanded into current working directory.
79 */
80 if (size == 0) {
81 snprintf(buf, buf_len, ".");
82 size = strlen(buf);
83 }
84
85 /*
86 * If there is no '/' ad the end of path from $PATH add it.
87 */
88 if (buf[size - 1] != '/')
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 ret =
90 snprintf(buf + size, buf_len - size, "/%s",
91 prog_name);
Subrata Modakca758b72010-05-18 01:28:14 +053092 else
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 ret =
94 snprintf(buf + size, buf_len - size, "%s",
95 prog_name);
Subrata Modakca758b72010-05-18 01:28:14 +053096
97 if (buf_len - size > ret && file_exist(buf))
98 return 0;
99
100 start = end + 1;
101
102 } while (end != NULL);
103
104 return -1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700105}