/* Improved 'whereis' utility - uses PATH instead of hardcoded paths

What it doesn't do:
   List source code & manpages
   Tilde/wildcard expansion in PATH and MANPATH
   Deal with extensions (see util-linux whereis.c)
   Handle flags like -u (see util-linux whereis.1)

If those features aren't needed I'd rather leave them out to keep it simple. 
I don't need them for my own use.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

char *path[30], *manpath[30];

main(int argc, char **argv) {
	int i;
	char *p;
char **s;

	p = getenv("PATH");
	for( i=0, p=strtok(p, ":"); p; p=strtok(0, ":") )
		path[i++] = p;
	path[i] = 0;

	argc--, argv++;
	if (!argc) {
		printf("whereis name...\n");
		exit(1);
	}
	do lookup(*argv++); while (--argc);
	return 0;
}	

lookup(char *fn) {
	int i;

	printf("%s:", fn);

	for (i=0; path[i]; i++) {
		DIR *dirp;
		struct dirent *dp;

		dirp = opendir(path[i]);
		if (!dirp) continue;
		while (dp = readdir(dirp))
			if (!strcmp(fn, dp->d_name)) {
				printf(" %s/%s", path[i], fn);
				break;
			}
		closedir(dirp);
	}
	
	putchar('\n');
}
