174274Srwatson/*-
274274Srwatson * Copyright (c) 2001 Robert N. M. Watson
374274Srwatson * All rights reserved.
474274Srwatson *
574274Srwatson * Redistribution and use in source and binary forms, with or without
674274Srwatson * modification, are permitted provided that the following conditions
774274Srwatson * are met:
874274Srwatson * 1. Redistributions of source code must retain the above copyright
974274Srwatson *    notice, this list of conditions and the following disclaimer.
1074274Srwatson * 2. Redistributions in binary form must reproduce the above copyright
1174274Srwatson *    notice, this list of conditions and the following disclaimer in the
1274274Srwatson *    documentation and/or other materials provided with the distribution.
1374274Srwatson *
1474274Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1574274Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1674274Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1774274Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1874274Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1974274Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2074274Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2174274Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2274274Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2374274Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2474274Srwatson * SUCH DAMAGE.
2574274Srwatson */
2692986Sobrien/*
2792986Sobrien * TrustedBSD: Utility functions for extended attributes.
2892986Sobrien */
2984225Sdillon
3084225Sdillon#include <sys/cdefs.h>
3184225Sdillon__FBSDID("$FreeBSD$");
3284225Sdillon
3374274Srwatson#include <sys/types.h>
3474274Srwatson#include <sys/extattr.h>
3574274Srwatson
3674274Srwatson#include <errno.h>
37150065Sstefanf#include <libutil.h>
3874274Srwatson#include <string.h>
3974274Srwatson
4074274Srwatsonint
4174436Srwatsonextattr_namespace_to_string(int attrnamespace, char **string)
4274274Srwatson{
4374274Srwatson
4474436Srwatson	switch(attrnamespace) {
4574274Srwatson	case EXTATTR_NAMESPACE_USER:
4691428Sgreen		if (string != NULL)
4774274Srwatson			*string = strdup(EXTATTR_NAMESPACE_USER_STRING);
4874274Srwatson		return (0);
4974274Srwatson
5074274Srwatson	case EXTATTR_NAMESPACE_SYSTEM:
5191428Sgreen		if (string != NULL)
5274274Srwatson			*string = strdup(EXTATTR_NAMESPACE_SYSTEM_STRING);
5374274Srwatson		return (0);
5474274Srwatson
5574274Srwatson	default:
5674274Srwatson		errno = EINVAL;
5774274Srwatson		return (-1);
5874274Srwatson	}
5974274Srwatson}
6074274Srwatson
6174274Srwatsonint
6274436Srwatsonextattr_string_to_namespace(const char *string, int *attrnamespace)
6374274Srwatson{
6474274Srwatson
6574274Srwatson	if (!strcmp(string, EXTATTR_NAMESPACE_USER_STRING)) {
6674436Srwatson		if (attrnamespace != NULL)
6774436Srwatson			*attrnamespace = EXTATTR_NAMESPACE_USER;
6874274Srwatson		return (0);
6974274Srwatson	} else if (!strcmp(string, EXTATTR_NAMESPACE_SYSTEM_STRING)) {
7074436Srwatson		if (attrnamespace != NULL)
7174436Srwatson			*attrnamespace = EXTATTR_NAMESPACE_SYSTEM;
7274274Srwatson		return (0);
7374274Srwatson	} else {
7474274Srwatson		errno = EINVAL;
7574274Srwatson		return (-1);
7674274Srwatson	}
7774274Srwatson}
78