Bug 17395

Summary: This is a replacement for the perl version of which.
Product: Base System Reporter: Ayan George <ayan>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.0-CURRENT   
Hardware: Any   
OS: Any   

Description Ayan George 2000-03-15 17:20:01 UTC
/*
 * Copyright (c) 2000 Ayan George <ayan@datasys.net>. United States. All rights
 * reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer. 2.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * $Id: which.c,v 1.1 2000/03/03 21:23:19 ayan Exp $
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#define	PROCESS_ALL		0x01
#define QUIET_OUTPUT	0x02

int main(int argc, char *argv[])
{

	char *directory;
	char *path;
	char *toklist;
	char filename[FILENAME_MAX]; /* FILENAME_MAX is defined in stdio.h. */

	int arg;
	int mode = 0;
	int rc = 1;

	gid_t myegid;
	uid_t myeuid;

	struct stat file_stat;

	path = getenv("PATH");

	if (path != NULL) {

		myegid = getegid();
		myeuid = geteuid();

		while ((arg = getopt(argc, argv, "ahs")) != (-1)) {

			switch (arg) {

			case 'a':
				mode = (mode | PROCESS_ALL);
				break;

			case 's':
				mode = (mode | QUIET_OUTPUT);
				break;

			case 'h':
				fprintf(stderr, "usage: which [-a] [-s] program ...\n");
				exit(0);
				break;

			}

		}

		argc -= optind;
		argv += optind;

		if (argc == 0)
			exit(0);

		else
			while (argc > 0) {

				toklist = strdup(path);

				while ((directory = (char *) strsep(&toklist, ":")) != NULL) {

					sprintf(filename, "%s/%s", directory, *argv);

					if (stat(filename, &file_stat) != (-1)) {

						if ((file_stat.st_mode & 0100) ||
						    ((file_stat.st_mode & 0010) && (myegid == file_stat.st_gid)) ||
						    ((file_stat.st_mode & 0001) && (myeuid == file_stat.st_uid))) {

							rc = 0;

							if (!(mode & QUIET_OUTPUT)) {
								write(1, filename, strlen(filename));
								putchar('\n');
							}

						}

					}

				}

				free(toklist);

				if (!(mode & PROCESS_ALL))
					exit(rc);

				--argc;
				++argv;

			}

	}

	exit(rc);

}

How-To-Repeat: 
	Since this is a complete rewrite, there I don't see a need for
	a diff.  This replaces /usr/src/usr.bin/which/which.pl.

	This does not require Perl to execute.
Comment 1 Sheldon Hearn 2000-03-15 17:24:46 UTC
On Wed, 15 Mar 2000 12:19:33 EST, Ayan George wrote:

> >Number:         17395
> >Category:       bin
> >Synopsis:       This is a replacement for the perl version of which.

This was discussed recently on the mailing lists (hackers?) and the
general concensus seemed to be that this is unnecessary?

Ciao,
Sheldon.
Comment 2 Mike Heffner freebsd_committer freebsd_triage 2001-06-16 15:41:23 UTC
State Changed
From-To: open->closed

which(1) has been converted to C.