xref: /illumos-gate/usr/src/lib/libc/port/gen/readdir_r.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5494a4c51Sraf  * Common Development and Distribution License (the "License").
6494a4c51Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21494a4c51Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * readdir_r -- C library extension routine
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	<sys/feature_tests.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if !defined(_LP64)
37*7257d1b4Sraf #pragma weak _readdir64_r = readdir64_r
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
40*7257d1b4Sraf #include "lint.h"
41494a4c51Sraf #include "libc.h"
42494a4c51Sraf #include <mtlib.h>
43494a4c51Sraf #include <unistd.h>
44494a4c51Sraf #include <dirent.h>
45494a4c51Sraf #include <string.h>
46494a4c51Sraf #include <limits.h>
47494a4c51Sraf #include <errno.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #ifdef _LP64
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * POSIX.1c standard version of the thread function readdir_r.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate int
readdir_r(DIR * dirp,dirent_t * entry,dirent_t ** result)56494a4c51Sraf readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
577c478bd9Sstevel@tonic-gate {
58494a4c51Sraf 	private_DIR *pdirp = (private_DIR *)dirp;
59494a4c51Sraf 	dirent_t *dp;		/* -> directory data */
607c478bd9Sstevel@tonic-gate 	int saveloc = 0;
617c478bd9Sstevel@tonic-gate 
62494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
637c478bd9Sstevel@tonic-gate 	if (dirp->dd_size != 0) {
64494a4c51Sraf 		dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
65494a4c51Sraf 		saveloc = dirp->dd_loc;		/* save for possible EOF */
667c478bd9Sstevel@tonic-gate 		dirp->dd_loc += (int)dp->d_reclen;
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (dirp->dd_loc >= dirp->dd_size)
707c478bd9Sstevel@tonic-gate 		dirp->dd_loc = dirp->dd_size = 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (dirp->dd_size == 0 &&	/* refill buffer */
737c478bd9Sstevel@tonic-gate 	    (dirp->dd_size = getdents(dirp->dd_fd,
74494a4c51Sraf 	    (dirent_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
75494a4c51Sraf 		if (dirp->dd_size == 0) {	/* This means EOF */
76494a4c51Sraf 			dirp->dd_loc = saveloc;	/* so save for telldir */
77494a4c51Sraf 			lmutex_unlock(&pdirp->dd_lock);
787c478bd9Sstevel@tonic-gate 			*result = NULL;
79494a4c51Sraf 			return (0);
807c478bd9Sstevel@tonic-gate 		}
81494a4c51Sraf 		lmutex_unlock(&pdirp->dd_lock);
827c478bd9Sstevel@tonic-gate 		*result = NULL;
83494a4c51Sraf 		return (errno);		/* error */
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
86494a4c51Sraf 	dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
877c478bd9Sstevel@tonic-gate 	(void) memcpy(entry, dp, (size_t)dp->d_reclen);
88494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
897c478bd9Sstevel@tonic-gate 	*result = entry;
907c478bd9Sstevel@tonic-gate 	return (0);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #else	/* _LP64 */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * POSIX.1c standard version of the thr function readdir_r.
977c478bd9Sstevel@tonic-gate  * Large file version.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate int
readdir64_r(DIR * dirp,dirent64_t * entry,dirent64_t ** result)101494a4c51Sraf readdir64_r(DIR *dirp, dirent64_t *entry, dirent64_t **result)
1027c478bd9Sstevel@tonic-gate {
103494a4c51Sraf 	private_DIR *pdirp = (private_DIR *)(uintptr_t)dirp;
104494a4c51Sraf 	dirent64_t *dp64;	/* -> directory data */
1057c478bd9Sstevel@tonic-gate 	int saveloc = 0;
1067c478bd9Sstevel@tonic-gate 
107494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
1087c478bd9Sstevel@tonic-gate 	if (dirp->dd_size != 0) {
109494a4c51Sraf 		dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
1107c478bd9Sstevel@tonic-gate 		/* was converted by readdir and needs to be reversed */
1117c478bd9Sstevel@tonic-gate 		if (dp64->d_ino == (ino64_t)-1) {
112494a4c51Sraf 			dirent_t *dp32;	/* -> 32 bit directory data */
1137c478bd9Sstevel@tonic-gate 
114494a4c51Sraf 			dp32 = (dirent_t *)(&dp64->d_off);
1157c478bd9Sstevel@tonic-gate 			dp64->d_ino = (ino64_t)dp32->d_ino;
1167c478bd9Sstevel@tonic-gate 			dp64->d_off = (off64_t)dp32->d_off;
1177c478bd9Sstevel@tonic-gate 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
118494a4c51Sraf 			    ((char *)&dp64->d_off - (char *)dp64));
1197c478bd9Sstevel@tonic-gate 		}
120494a4c51Sraf 		saveloc = dirp->dd_loc;		/* save for possible EOF */
1217c478bd9Sstevel@tonic-gate 		dirp->dd_loc += (int)dp64->d_reclen;
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (dirp->dd_loc >= dirp->dd_size)
1257c478bd9Sstevel@tonic-gate 		dirp->dd_loc = dirp->dd_size = 0;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (dirp->dd_size == 0 &&	/* refill buffer */
1287c478bd9Sstevel@tonic-gate 	    (dirp->dd_size = getdents64(dirp->dd_fd,
129494a4c51Sraf 	    (dirent64_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
130494a4c51Sraf 		if (dirp->dd_size == 0) {	/* This means EOF */
131494a4c51Sraf 			dirp->dd_loc = saveloc;	/* so save for telldir */
132494a4c51Sraf 			lmutex_unlock(&pdirp->dd_lock);
1337c478bd9Sstevel@tonic-gate 			*result = NULL;
134494a4c51Sraf 			return (0);
1357c478bd9Sstevel@tonic-gate 		}
136494a4c51Sraf 		lmutex_unlock(&pdirp->dd_lock);
1377c478bd9Sstevel@tonic-gate 		*result = NULL;
138494a4c51Sraf 		return (errno);		/* error */
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
141494a4c51Sraf 	dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
1427c478bd9Sstevel@tonic-gate 	(void) memcpy(entry, dp64, (size_t)dp64->d_reclen);
1437c478bd9Sstevel@tonic-gate 	*result = entry;
144494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
1457c478bd9Sstevel@tonic-gate 	return (0);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
149494a4c51Sraf  * POSIX.1c standard version of the function readdir_r.
1507c478bd9Sstevel@tonic-gate  * User gets it via static readdir_r from header file.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate int
__posix_readdir_r(DIR * dirp,dirent_t * entry,dirent_t ** result)154494a4c51Sraf __posix_readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
1557c478bd9Sstevel@tonic-gate {
156494a4c51Sraf 	int error;
157494a4c51Sraf 	dirent64_t *dp64;
158494a4c51Sraf 	struct {
159494a4c51Sraf 		dirent64_t dirent64;
160494a4c51Sraf 		char chars[MAXNAMLEN];
161494a4c51Sraf 	} buf;
162494a4c51Sraf 
163494a4c51Sraf 	error = readdir64_r(dirp, (dirent64_t *)&buf, &dp64);
164494a4c51Sraf 	if (error != 0 || dp64 == NULL) {
1657c478bd9Sstevel@tonic-gate 		*result = NULL;
166494a4c51Sraf 		return (error);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
169494a4c51Sraf 	if (dp64->d_ino > SIZE_MAX ||
170494a4c51Sraf 	    (uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) {
1717c478bd9Sstevel@tonic-gate 		*result = NULL;
1727c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	entry->d_ino = (ino_t)dp64->d_ino;
1767c478bd9Sstevel@tonic-gate 	entry->d_off = (off_t)dp64->d_off;
1777c478bd9Sstevel@tonic-gate 	entry->d_reclen = (unsigned short)((((char *)entry->d_name -
178494a4c51Sraf 	    (char *)entry) + strlen(dp64->d_name) + 1 + 3) & ~3);
1797c478bd9Sstevel@tonic-gate 	(void) strcpy(entry->d_name, dp64->d_name);
1807c478bd9Sstevel@tonic-gate 	*result = entry;
1817c478bd9Sstevel@tonic-gate 	return (0);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
184494a4c51Sraf /*
185494a4c51Sraf  * POSIX.1c Draft-6 version of the function readdir_r.
186494a4c51Sraf  * It was implemented by Solaris 2.3.
187494a4c51Sraf  */
188494a4c51Sraf 
189494a4c51Sraf dirent_t *
readdir_r(DIR * dirp,dirent_t * entry)190494a4c51Sraf readdir_r(DIR *dirp, dirent_t *entry)
191494a4c51Sraf {
192494a4c51Sraf 	int error;
193494a4c51Sraf 	dirent_t *result;
194494a4c51Sraf 
195494a4c51Sraf 	if ((error = __posix_readdir_r(dirp, entry, &result)) != 0)
196494a4c51Sraf 		errno = error;
197494a4c51Sraf 	return (result);
198494a4c51Sraf }
199494a4c51Sraf 
2007c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
201