Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 25 | * Looks for binary prog_name in $PATH. |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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 "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 | |
| 45 | static 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 | |
| 55 | int tst_get_path(const char *prog_name, char *buf, size_t buf_len) |
| 56 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 57 | const char *path = (const char *)getenv("PATH"); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 58 | const char *start = path; |
| 59 | const char *end; |
| 60 | size_t size, ret; |
| 61 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 62 | if (path == NULL) |
| 63 | return -1; |
| 64 | |
| 65 | do { |
| 66 | end = strchr(start, ':'); |
| 67 | |
| 68 | if (end != NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | snprintf(buf, MIN(buf_len, (size_t) (end - start + 1)), |
| 70 | "%s", start); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 71 | else |
| 72 | snprintf(buf, buf_len, "%s", start); |
| 73 | |
| 74 | size = strlen(buf); |
| 75 | |
| 76 | /* |
Garrett Cooper | 936344f | 2010-12-16 15:12:18 -0800 | [diff] [blame] | 77 | * "::" inside $PATH, $PATH ending with ':' or $PATH starting |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 78 | * 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | ret = |
| 90 | snprintf(buf + size, buf_len - size, "/%s", |
| 91 | prog_name); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 92 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | ret = |
| 94 | snprintf(buf + size, buf_len - size, "%s", |
| 95 | prog_name); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 96 | |
| 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 Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 105 | } |