/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
*/

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: pathfind.c 1.4 89/10/23 $@(#)libgen:pathfind.c	1.2"
/*
	pathfind.c	1.2	1/24/85 10:03:43
*/

/* 
	Search the specified path for a file with the specified
	mode and type. Return a pointer to the path. If the 
	file isn't found, return NULL.
*/

#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <libgen.h>
#include <limits.h>

/*
 * Mode bit definitions -- see mknod(2)
 * Names of flags duplicate those of test(1)
 */

#define OPTSTRING	"rwxfbcdpugks"

/* File type: 0170000 */
#define FFLAG	0100000		/* normal file - also type 0 */
#define BFLAG	060000		/* block special */
#define CFLAG	020000		/* character special */
#define DFLAG	040000		/* directory */
#define PFLAG	010000		/* fifo */

#define UFLAG	04000		/* setuid */
#define GFLAG	02000		/* setgid */
#define KFLAG	01000		/* sticky bit */

/*
 * Perms: 0700 user, 070 group, 07 other
 * Note that pathfind uses access(2), so no need to hassle
 * with shifts and such
 */
#define RFLAG	04		/* read */
#define WFLAG	02		/* write */
#define XFLAG	01		/* execute */

	char *
pathfind(path, name, mode)
	register char *path, *name;
	char *mode;	/* any combination from OPTSTRING */
{
	static char cpath[PATH_MAX];
	register char *cp;
	int imode;
	int nzflag;
	static	int	fullck();
	int		access();
	int		stat();

	/* Build imode */
	imode = 0; nzflag = 0;
	if (mode == ((char *) 0))
		mode = "";
	for (cp = mode; *cp; cp++) {
		switch (*cp) {
		case 'r':
			imode |= RFLAG;
			break;
		case 'w':
			imode |= WFLAG;
			break;
		case 'x':
			imode |= XFLAG;
			break;
		case 'b':
			imode |= BFLAG;
			break;
		case 'c':
			imode |= CFLAG;
			break;
		case 'd':
			imode |= DFLAG;
			break;
		case 'f':
			imode |= FFLAG;
			break;
		case 'p':
			imode |= PFLAG;
			break;
		case 'u':
			imode |= UFLAG;
			break;
		case 'g':
			imode |= GFLAG;
			break;
		case 'k':
			imode |= KFLAG;
			break;
		case 's':
			nzflag = 1;
			break;
		default:
			return ( (char *)0 );
		}
	}
		
	if (name[0] == '/' || path == ((char *) 0) || *path == '\0')
		path = ":";
	while (*path) {
		for (cp = cpath; *cp = *path; cp++) {
			path++;
			if (*cp == ':')
				break;
		}
		if (cp != cpath)
			*cp++ = '/';
		*cp = '\0';
		strcat (cp, name);
		if (access (cpath, imode&07) == 0 &&
			fullck (cpath, imode, nzflag))
			return cpath;
	}

	return ((char *) 0);
}

	static
fullck (name, mode, nzflag)
	char *name;
	int mode;
	int nzflag;
{
	struct stat sbuf;
	int xor;
	int rc;

	if ((mode & 0177000) == 0 && nzflag == 0)	/* no special info wanted */
		return 1;

	if ( (rc=stat (name, &sbuf)) == -1)
		return(0);

	xor = (sbuf.st_mode ^ mode) & 077000;	/* see mknod(2) */
	if ((mode & 0170000) == 0)
		xor &= ~070000;
	if ((mode & 07000) == 0)
		xor &= ~07000;
	if (xor)
		return 0;
	if (nzflag && sbuf.st_size == 0)
		return 0;
	return 1;
}

