xref: /illumos-gate/usr/src/lib/libc/port/gen/readdir_r.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 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * readdir_r -- C library extension routine
34  */
35 
36 #include	<sys/feature_tests.h>
37 
38 #if !defined(_LP64)
39 #pragma weak _readdir64_r = readdir64_r
40 #endif
41 
42 #include "lint.h"
43 #include "libc.h"
44 #include <mtlib.h>
45 #include <unistd.h>
46 #include <dirent.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <errno.h>
50 
51 #ifdef _LP64
52 
53 /*
54  * POSIX.1c standard version of the thread function readdir_r.
55  */
56 
57 int
58 readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
59 {
60 	private_DIR *pdirp = (private_DIR *)dirp;
61 	dirent_t *dp;		/* -> directory data */
62 	int saveloc = 0;
63 
64 	lmutex_lock(&pdirp->dd_lock);
65 	if (dirp->dd_size != 0) {
66 		dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
67 		saveloc = dirp->dd_loc;		/* save for possible EOF */
68 		dirp->dd_loc += (int)dp->d_reclen;
69 	}
70 
71 	if (dirp->dd_loc >= dirp->dd_size)
72 		dirp->dd_loc = dirp->dd_size = 0;
73 
74 	if (dirp->dd_size == 0 &&	/* refill buffer */
75 	    (dirp->dd_size = getdents(dirp->dd_fd,
76 	    (dirent_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
77 		if (dirp->dd_size == 0) {	/* This means EOF */
78 			dirp->dd_loc = saveloc;	/* so save for telldir */
79 			lmutex_unlock(&pdirp->dd_lock);
80 			*result = NULL;
81 			return (0);
82 		}
83 		lmutex_unlock(&pdirp->dd_lock);
84 		*result = NULL;
85 		return (errno);		/* error */
86 	}
87 
88 	dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
89 	(void) memcpy(entry, dp, (size_t)dp->d_reclen);
90 	lmutex_unlock(&pdirp->dd_lock);
91 	*result = entry;
92 	return (0);
93 }
94 
95 #else	/* _LP64 */
96 
97 /*
98  * POSIX.1c standard version of the thr function readdir_r.
99  * Large file version.
100  */
101 
102 int
103 readdir64_r(DIR *dirp, dirent64_t *entry, dirent64_t **result)
104 {
105 	private_DIR *pdirp = (private_DIR *)(uintptr_t)dirp;
106 	dirent64_t *dp64;	/* -> directory data */
107 	int saveloc = 0;
108 
109 	lmutex_lock(&pdirp->dd_lock);
110 	if (dirp->dd_size != 0) {
111 		dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
112 		/* was converted by readdir and needs to be reversed */
113 		if (dp64->d_ino == (ino64_t)-1) {
114 			dirent_t *dp32;	/* -> 32 bit directory data */
115 
116 			dp32 = (dirent_t *)(&dp64->d_off);
117 			dp64->d_ino = (ino64_t)dp32->d_ino;
118 			dp64->d_off = (off64_t)dp32->d_off;
119 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
120 			    ((char *)&dp64->d_off - (char *)dp64));
121 		}
122 		saveloc = dirp->dd_loc;		/* save for possible EOF */
123 		dirp->dd_loc += (int)dp64->d_reclen;
124 	}
125 
126 	if (dirp->dd_loc >= dirp->dd_size)
127 		dirp->dd_loc = dirp->dd_size = 0;
128 
129 	if (dirp->dd_size == 0 &&	/* refill buffer */
130 	    (dirp->dd_size = getdents64(dirp->dd_fd,
131 	    (dirent64_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
132 		if (dirp->dd_size == 0) {	/* This means EOF */
133 			dirp->dd_loc = saveloc;	/* so save for telldir */
134 			lmutex_unlock(&pdirp->dd_lock);
135 			*result = NULL;
136 			return (0);
137 		}
138 		lmutex_unlock(&pdirp->dd_lock);
139 		*result = NULL;
140 		return (errno);		/* error */
141 	}
142 
143 	dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
144 	(void) memcpy(entry, dp64, (size_t)dp64->d_reclen);
145 	*result = entry;
146 	lmutex_unlock(&pdirp->dd_lock);
147 	return (0);
148 }
149 
150 /*
151  * POSIX.1c standard version of the function readdir_r.
152  * User gets it via static readdir_r from header file.
153  */
154 
155 int
156 __posix_readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
157 {
158 	int error;
159 	dirent64_t *dp64;
160 	struct {
161 		dirent64_t dirent64;
162 		char chars[MAXNAMLEN];
163 	} buf;
164 
165 	error = readdir64_r(dirp, (dirent64_t *)&buf, &dp64);
166 	if (error != 0 || dp64 == NULL) {
167 		*result = NULL;
168 		return (error);
169 	}
170 
171 	if (dp64->d_ino > SIZE_MAX ||
172 	    (uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) {
173 		*result = NULL;
174 		return (EOVERFLOW);
175 	}
176 
177 	entry->d_ino = (ino_t)dp64->d_ino;
178 	entry->d_off = (off_t)dp64->d_off;
179 	entry->d_reclen = (unsigned short)((((char *)entry->d_name -
180 	    (char *)entry) + strlen(dp64->d_name) + 1 + 3) & ~3);
181 	(void) strcpy(entry->d_name, dp64->d_name);
182 	*result = entry;
183 	return (0);
184 }
185 
186 /*
187  * POSIX.1c Draft-6 version of the function readdir_r.
188  * It was implemented by Solaris 2.3.
189  */
190 
191 dirent_t *
192 readdir_r(DIR *dirp, dirent_t *entry)
193 {
194 	int error;
195 	dirent_t *result;
196 
197 	if ((error = __posix_readdir_r(dirp, entry, &result)) != 0)
198 		errno = error;
199 	return (result);
200 }
201 
202 #endif	/* _LP64 */
203