🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.bugs
Article #138

Re: Usefulness of access(2) -- source available

#138
From: Mike Braca
Date: Tue, 27 Sep 1983 19:57
84 lines
2200 bytes
Here is eaccess for 4.1c BSD:

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/errno.h>

/*
 * Like access(2) except uses effective uid/gid.
 * 4.1c BSD version.
 */

eaccess(filename, mode)
char *filename;
register int mode;
{
	extern int errno;
	struct stat statbuf;
	int euid = geteuid();
	mode &= 7;	/* Clear cruft */

	/*
	 * If we're trying to write, check for
	 * read-only file system or text file busy.
	 */
	if ((mode & 2) &&
	    (access(filename, 2) < 0) &&
	    (errno == EROFS || errno == ETXTBSY))
		return -1;

	/*
	 * Otherwise, superuser always passes.
	 */
	if (euid == 0)
		return 0;

	/*
	 * Stat will fail with correct error number
	 * if we don't have access.
	 */
	if (stat(filename, &statbuf) < 0)
		return -1;

	/*
	 * If we own the file, check owner bits.
	 */
	if (statbuf.st_uid == euid)
		mode <<= 6;

	/*
	 * If we're the same group as the file,
	 * check group bits.
	 */
	else if (statbuf.st_gid == getegid())
		mode <<= 3;

	/*
	 * This is the 4.1c BSD way to check group
	 * membership. 4.2 should be the same.
	 */
	else {
		int ngroups = NGROUPS;	/* from <sys/param.h> */
		int groups[NGROUPS];
		getgroups(&ngroups, groups);
		while (--ngroups >= 0)
			if (groups[ngroups] == statbuf.st_gid) {
				mode <<= 3;
				break;
			}
	}

	/*
	 * Otherwise we are checking "world" bits.
	 * See if the requested access matches the
	 * file permissions.
	 */
	if ((mode & statbuf.st_mode) != mode) {
		errno = EACCES;	/* sic */
		return -1;
	}
	return 0;
}

Mike Braca, Brown U. Institute for Research in Information and Scholarship
{ihnp4,allegra,decvax}!brunix!mjb, mjb.Brown@UDel-Relay, MJB@BROWNCS.BITNET
Technical Details
Message-ID: <4941@brunix.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!linus!vaxine!wjh12!foxvax1!brunix!mjb
References: wjh12.326, <2326@utcsrgv.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!linus!vaxine!wjh12!foxvax1!brunix!mjb
From: mjb@brunix.UUCP (Mike Braca)
Newsgroups: net.bugs,net.unix-wizards
Subject: Re: Usefulness of access(2) -- source available
Message-ID: <4941@brunix.UUCP>
Date: Tue, 27-Sep-83 15:57:58 EDT
Article-I.D.: brunix.4941
Posted: Tue Sep 27 15:57:58 1983
Date-Received: Wed, 28-Sep-83 22:37:24 EDT
References: wjh12.326, <2326@utcsrgv.UUCP>
Lines: 84