xref: /titanic_50/usr/src/lib/libc/port/gen/attropen.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *	attropen -- C library extension routine
31  */
32 
33 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
34 #pragma weak attropen64 = _attropen64
35 #else
36 #pragma weak attropen = _attropen
37 #endif
38 
39 #include	"synonyms.h"
40 #include	<sys/types.h>
41 #include	<sys/stat.h>
42 #include   	<fcntl.h>
43 #include	<sys/errno.h>
44 #include	<stdlib.h>
45 #include	<errno.h>
46 #include	<unistd.h>
47 #include	<stdarg.h>
48 
49 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
50 
51 int
52 attropen64(const char *file, const char *attr, int oflag, ...)
53 {
54 	int fd;
55 	int attrfd;
56 	int saverrno;
57 	va_list ap;
58 
59 	va_start(ap, oflag);
60 
61 	if ((fd = open64(file, O_RDONLY|O_NONBLOCK)) == -1) {
62 		va_end(ap);
63 		return (-1);
64 	}
65 
66 	if ((attrfd = openat64(fd, attr, oflag | O_XATTR,
67 			va_arg(ap, mode_t))) == -1) {
68 		saverrno = errno;
69 		(void) close(fd);
70 		errno = saverrno;
71 		va_end(ap);
72 		return (-1);
73 	}
74 
75 	(void) close(fd);
76 	va_end(ap);
77 	return (attrfd);
78 }
79 
80 #else
81 
82 int
83 attropen(const char *file, const char *attr, int oflag, ...)
84 {
85 	int fd;
86 	int attrfd;
87 	int saverrno;
88 	va_list ap;
89 
90 	va_start(ap, oflag);
91 
92 	if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
93 		va_end(ap);
94 		return (-1);
95 	}
96 
97 	if ((attrfd = openat(fd, attr, oflag | O_XATTR,
98 			va_arg(ap, mode_t))) == -1) {
99 		saverrno = errno;
100 		(void) close(fd);
101 		errno = saverrno;
102 		va_end(ap);
103 		return (-1);
104 	}
105 
106 	(void) close(fd);
107 	va_end(ap);
108 	return (attrfd);
109 }
110 
111 #endif /* _FILE_OFFSET_BITS == 64 */
112