Sun-Solaris-Exacct

 view release on metacpan or  search on metacpan

File/File.xs  view on Meta::CPAN

PROTOTYPES: ENABLE

 #
 # Define the stash pointers if required and create and populate @_Constants.
 #
BOOT:
	{
	init_stashes();
	define_constants(PKGBASE "::File", constants);
	}

 #
 # Open an exacct file and return an object with which to manipulate it.
 # The parameters are the filename, the open mode and a list of optional
 # (key => value) parameters where the key may be  one of creator, aflags or
 # mode.  For a full explanation of the various combinations, see the manpage
 # for ea_open_file(3EXACCT).
 #
ea_file_t *
new(class, name, oflags, ...)
	char	*class;
	char	*name;
	int	oflags;
PREINIT:
	int	i;
	/* Assume usernames are <= 32 chars (pwck(1M) assumes <= 8) */
	char	user[33];
	char	*creator = NULL;
	int	aflags   = -1;
	mode_t	mode     = 0666;
CODE:
	/*
	 * Account for the mandatory parameters,
	 * and the rest must be an even number.
	 */
	i = items - 3;
	if ((i % 2) != 0) {
		croak("Usage: Sun::Solaris::Exacct::File::new"
		    "(class, name, oflags, ...)");
	}

	/* Process any optional parameters. */
	for (i = 3; i < items; i += 2) {
		if (strEQ(SvPV_nolen(ST(i)), "creator")) {
			creator = SvPV_nolen(ST(i + 1));
		} else if (strEQ(SvPV_nolen(ST(i)), "aflags")) {
			aflags = SvIV(ST(i + 1));
		} else if (strEQ(SvPV_nolen(ST(i)), "mode")) {
			mode = SvIV(ST(i + 1));
		} else {
			croak("invalid named argument %s", SvPV_nolen(ST(i)));
		}
	}

	/* Check and default the creator parameter. */
	if (oflags & O_CREAT && creator == NULL) {
		uid_t		uid;
		struct passwd	*pwent;

		uid = getuid();
		if ((pwent = getpwuid(uid)) == NULL) {
			snprintf(user, sizeof (user), "%d", uid);
		} else {
			strlcpy(user, pwent->pw_name, sizeof (user));
		}
		creator = user;
	}

	/* Check and default the aflags parameter. */
	if (aflags == -1) {
		if (oflags == O_RDONLY) {
			aflags = EO_HEAD;
		} else {
			aflags = EO_TAIL;
		}
	}
	RETVAL = ea_alloc(sizeof (ea_file_t));
	PERL_ASSERT(RETVAL != NULL);
	if (ea_open(RETVAL, name, creator, aflags, oflags, mode) == -1) {
		ea_free(RETVAL, sizeof (ea_file_t));
		RETVAL = NULL;
	}
OUTPUT:
	RETVAL

void
DESTROY(self)
	ea_file_t	*self;
CODE:
	ea_close(self);
	ea_free(self, sizeof(ea_file_t));

 #
 # Return the creator of the file.
 #
SV*
creator(self)
	ea_file_t	*self;
PREINIT:
	const char	*creator;
CODE:
	if ((creator = ea_get_creator(self)) == NULL) {
		RETVAL = &PL_sv_undef;
	} else {
		RETVAL = newSVpv(creator, 0);
	}
OUTPUT:
	RETVAL

 #
 # Return the hostname the file was created on.
 #
SV*
hostname(self)
	ea_file_t	*self;
PREINIT:
	const char	*hostname;
CODE:
	if ((hostname = ea_get_hostname(self)) == NULL) {
		RETVAL = &PL_sv_undef;
	} else {



( run in 1.792 second using v1.01-cache-2.11-cpan-39bf76dae61 )