blob: f679108be2a989ec8a5ffbb88948cf54b1aa4a59 [file] [log] [blame]
Denis Vlasenko5cb46482006-10-12 06:12:11 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include "libbb.h"
11
12/* check if path points to an executable file;
13 * return 1 if found;
14 * return 0 otherwise;
15 */
16int execable_file(const char *name)
17{
18 struct stat s;
19 return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode));
20}
21
22/* search $PATH for an executable file;
23 * return allocated string containing full path if found;
24 * return NULL otherwise;
25 */
26char *find_execable(const char *filename)
27{
28 char *path, *p, *n;
29
Denis Vlasenko12f56762006-10-12 06:15:13 +000030 p = path = xstrdup(getenv("PATH"));
Denis Vlasenko5cb46482006-10-12 06:12:11 +000031 while (p) {
32 n = strchr(p, ':');
33 if (n)
34 *n++ = '\0';
35 if (*p != '\0') { /* it's not a PATH="foo::bar" situation */
36 p = concat_path_file(p, filename);
37 if (execable_file(p)) {
38 free(path);
39 return p;
40 }
41 free(p);
42 }
43 p = n;
44 }
45 free(path);
46 return NULL;
47}
48
49/* search $PATH for an executable file;
50 * return 1 if found;
51 * return 0 otherwise;
52 */
53int exists_execable(const char *filename)
54{
55 char *ret = find_execable(filename);
56 if (ret) {
57 free(ret);
58 return 1;
59 }
60 return 0;
61}
Denis Vlasenko1d76f432007-02-06 01:20:12 +000062
Denis Vlasenko80d14be2007-04-10 23:03:30 +000063#if ENABLE_FEATURE_PREFER_APPLETS
Denis Vlasenko1d76f432007-02-06 01:20:12 +000064/* just like the real execvp, but try to launch an applet named 'file' first
65 */
66int bb_execvp(const char *file, char *const argv[])
67{
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +000068 return execvp(find_applet_by_name(file) ? bb_busybox_exec_path : file,
Denis Vlasenko1d76f432007-02-06 01:20:12 +000069 argv);
70}
71#endif