Exemple #1
0
int which_main(int argc, char **argv)
{
	int status = EXIT_SUCCESS;
	char *p;

	if (argc <= 1 || argv[1][0] == '-') {
		bb_show_usage();
	}

/* We shouldn't do this. Ever. Not our business.
	if (!getenv("PATH")) {
		putenv((char*)bb_PATH_root_path);
	}
*/

	while (--argc > 0) {
		argv++;
		if (strchr(*argv, '/')) {
			if (execable_file(*argv)) {
				puts(*argv);
				continue;
			}
		} else {
			p = find_execable(*argv);
			if (p) {
				puts(p);
				free(p);
				continue;
			}
		}
		status = EXIT_FAILURE;
	}

	fflush_stdout_and_exit(status);
}
Exemple #2
0
int which_main(int argc, char **argv)
{
	int status = EXIT_SUCCESS;
	char *p;

	if (argc <= 1 || argv[1][0] == '-') {
		bb_show_usage();
	}

	if (!getenv("PATH")) {
		setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 1);
	}

	while (--argc > 0) {
		argv++;
		if (strchr(*argv, '/')) {
			if (execable_file(*argv)) {
				puts(*argv);
				continue;
			}
		} else {
			p = find_execable(*argv);
			if (p) {
				puts(p);
				free(p);
				continue;
			}
		}
		status = EXIT_FAILURE;
	}

	fflush_stdout_and_exit(status);
}
/* search (*PATHp) for an executable file;
 * return allocated string containing full path if found;
 *  PATHp points to the component after the one where it was found
 *  (or NULL),
 *  you may call find_execable again with this PATHp to continue
 *  (if it's not NULL).
 * return NULL otherwise; (PATHp is undefined)
 * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
 */
char* FAST_FUNC find_execable(const char *filename, char **PATHp)
{
	char *p, *n;

	p = *PATHp;
	while (p) {
		n = strchr(p, ':');
		if (n)
			*n++ = '\0';
		if (*p != '\0') { /* it's not a PATH="foo::bar" situation */
			p = concat_path_file(p, filename);
			if (execable_file(p)) {
				*PATHp = n;
				return p;
			}
			free(p);
		}
		p = n;
	} /* on loop exit p == NULL */
	return p;
}
Exemple #4
0
/* search $PATH for an executable file;
 * return allocated string containing full path if found;
 * return NULL otherwise;
 */
char *find_execable(const char *filename)
{
	char *path, *p, *n;

	p = path = xstrdup(getenv("PATH"));
	while (p) {
		n = strchr(p, ':');
		if (n)
			*n++ = '\0';
		if (*p != '\0') { /* it's not a PATH="foo::bar" situation */
			p = concat_path_file(p, filename);
			if (execable_file(p)) {
				free(path);
				return p;
			}
			free(p);
		}
		p = n;
	}
	free(path);
	return NULL;
}