/*
 * Program: access
 *
 * Description:
 *	This program allows a user (typically an administrator) to determine
 *	if the current user has access to various files.  It is useful for
 *	testing the setting of file security attributes (for example, ensuring
 *	a web server UID does not have write access to read-only HTML files).
 *
 * Copyright (c) 2003, John T. Criswell
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * 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.
 *     * Neither the name of the author nor the contributors may be used to
 *       endorse or promote products derived from this software without
 *       specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
 *
 * RCS_ID: $Header: /u/jcriswel/src/access/RCS/access.c,v 1.1 2003/03/24 14:50:26 jcriswel Exp jcriswel $
 */

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

#include <unistd.h>
#include <sys/types.h>

int
main (int argc, char ** argv)
{
	/* The access modes to check */
	register int mode;

	/* Indexing variable */
	register int index;

	/*
	 * Determine if the number of command line arguments is correct.
	 */
	if (argc < 3)
	{
		usage (argv[0]);
		exit (1);
	}

	/*
	 * Get the mode options from the first argument.
	 */
	if ((mode = parsemode (argv[1])) == 0)
	{
		usage (argv[0]);
		exit (2);
	}

	/*
	 * Determine which files have the specified access.
	 */
	for (index=2; index < argc; index++)
	{
		if ((access (argv[index], mode)) == 0)
		{
			write (STDOUT_FILENO,argv[index],strlen (argv[index]));
			write (STDOUT_FILENO,"\n",1);
		}
	}

	exit (0);
}

int
parsemode (char * perms)
{
	/* Pointer to the current character in the permissions string */
	register char * p;

	/* The mode that will return */
	register int mode = 0;

	/*
	 * Determine if we want to find read access.
	 */
	for (p=perms; *p != '\0'; p++)
	{
		switch (*p)
		{
			case 'r':
			case 'R':
				mode |= R_OK;
				break;

			case 'w':
			case 'W':
				mode |= W_OK;
				break;

			case 'x':
			case 'X':
				mode |= X_OK;
				break;
		}
	}

	return mode;
}

int
usage (char * name)
{
	fprintf (stderr, "%s: <mode> file [file ...]\n", name);
	fprintf (stderr, "\t<mode> can be any combination of r,w,x where:");
	fprintf (stderr, "\t\tr=read access");
	fprintf (stderr, "\t\tw=write access");
	fprintf (stderr, "\t\tx=execute access");
	return 0;
}

