xref: /illumos-gate/usr/src/lib/libc/port/gen/attropen.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
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 #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 "lint.h"
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <stdarg.h>
47 
48 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
49 
50 int
51 attropen64(const char *file, const char *attr, int oflag, ...)
52 {
53 	int fd;
54 	int attrfd;
55 	int saverrno;
56 	va_list ap;
57 
58 	va_start(ap, oflag);
59 
60 	if ((fd = open64(file, O_RDONLY|O_NONBLOCK)) == -1) {
61 		va_end(ap);
62 		return (-1);
63 	}
64 
65 	if ((attrfd = openat64(fd, attr, oflag | O_XATTR,
66 	    va_arg(ap, mode_t))) == -1) {
67 		saverrno = errno;
68 		(void) close(fd);
69 		errno = saverrno;
70 		va_end(ap);
71 		return (-1);
72 	}
73 
74 	(void) close(fd);
75 	va_end(ap);
76 	return (attrfd);
77 }
78 
79 #else
80 
81 int
82 attropen(const char *file, const char *attr, int oflag, ...)
83 {
84 	int fd;
85 	int attrfd;
86 	int saverrno;
87 	va_list ap;
88 
89 	va_start(ap, oflag);
90 
91 	if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
92 		va_end(ap);
93 		return (-1);
94 	}
95 
96 	if ((attrfd = openat(fd, attr, oflag | O_XATTR,
97 	    va_arg(ap, mode_t))) == -1) {
98 		saverrno = errno;
99 		(void) close(fd);
100 		errno = saverrno;
101 		va_end(ap);
102 		return (-1);
103 	}
104 
105 	(void) close(fd);
106 	va_end(ap);
107 	return (attrfd);
108 }
109 
110 #endif /* _FILE_OFFSET_BITS == 64 */
111