Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Mar 2000 12:19:33 -0500 (EST)
From:      Ayan George <ayan@kiwi.datasys.net>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/17395: bin 
Message-ID:  <200003151719.MAA44288@kiwi.datasys.net>

next in thread | raw e-mail | index | archive | help

>Number:         17395
>Category:       bin
>Synopsis:       This is a replacement for the perl version of which.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar 15 09:20:01 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Ayan George
>Release:        FreeBSD 4.0-CURRENT i386
>Organization:
DSS Online, LLC 
>Environment:

>Description:
/*
 * 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.

>Fix:


>Release-Note:
>Audit-Trail:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200003151719.MAA44288>