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