view toys/which.c @ 293:6baa13382880

Sort was including the trailing comma and getting the order wrong. (Specifically, it was comparing "sh," with "sha1sum," and putting sha1sum first in generated/newtoys.h so the binary search wasn't finding sha1sum. Alas, you can't feed separate beginning and ending delimiters to "sort -t". The fix is to copy the appropriate field out with sed, duplicate it at the start of the string where it's easy to compare, and then remove it again with a second sed after the sort.
author Rob Landley <rob@landley.net>
date Mon, 12 May 2008 01:23:19 -0500
parents 163498bf547b
children 5a0faa267866
line wrap: on
line source

/* vi: set sw=4 ts=4:
 *
 * which.c - Find executable files in $PATH.
 *
 * Copyright 2006 Rob landley <rob@landley.net>
 *
 * Not in SUSv3.

USE_WHICH(NEWTOY(which, "a", TOYFLAG_USR|TOYFLAG_BIN))

config WHICH
	bool "which"
	default y
	help
	  usage: which [-a] filename ...

	  Search $PATH for executable files matching filename(s).

	  -a	Show all matches
*/
#include "toys.h"

#define OPT_a   1

// Find an exectuable file either at a path with a slash in it (absolute or
// relative to current directory), or in $PATH.  Returns absolute path to file,
// or NULL if not found.

static int which_in_path(char *filename)
{
	struct string_list *list;

	// If they gave us a path, don't worry about $PATH or -a

	if (index(filename, '/')) {
		// Confirm it has the executable bit set, and it's not a directory.
		if (!access(filename, X_OK)) {
			struct stat st;

			if (!stat(filename, &st) && S_ISREG(st.st_mode)) {
				puts(filename);
				return 0;
			}
			return 1;
		}
	}

	// Search $PATH for matches.
	list = find_in_path(getenv("PATH"), filename);
	if (!list) return 1;

	// Print out matches
	while (list) {
		if (!access(list->str, X_OK)) {
			puts(list->str);
			// If we should stop at one match, do so
			if (toys.optflags & OPT_a) {
				llist_free(list, NULL);
				break;
			}
		}
		free(llist_pop(&list));
	}

	return 0;
}

void which_main(void)
{
	if (!*toys.optargs) toys.exitval++;
	else {
		int i;
		for (i=0; toys.optargs[i]; i++)
			toys.exitval |= which_in_path(toys.optargs[i]);
	}
}