10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53453Sraf * Common Development and Distribution License (the "License").
63453Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
213453Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
31*6812Sraf
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * readdir_r -- C library extension routine
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <sys/feature_tests.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #if !defined(_LP64)
39*6812Sraf #pragma weak _readdir64_r = readdir64_r
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate
42*6812Sraf #include "lint.h"
433453Sraf #include "libc.h"
443453Sraf #include <mtlib.h>
453453Sraf #include <unistd.h>
463453Sraf #include <dirent.h>
473453Sraf #include <string.h>
483453Sraf #include <limits.h>
493453Sraf #include <errno.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate #ifdef _LP64
520Sstevel@tonic-gate
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate * POSIX.1c standard version of the thread function readdir_r.
550Sstevel@tonic-gate */
560Sstevel@tonic-gate
570Sstevel@tonic-gate int
readdir_r(DIR * dirp,dirent_t * entry,dirent_t ** result)583453Sraf readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
590Sstevel@tonic-gate {
603453Sraf private_DIR *pdirp = (private_DIR *)dirp;
613453Sraf dirent_t *dp; /* -> directory data */
620Sstevel@tonic-gate int saveloc = 0;
630Sstevel@tonic-gate
643453Sraf lmutex_lock(&pdirp->dd_lock);
650Sstevel@tonic-gate if (dirp->dd_size != 0) {
663453Sraf dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
673453Sraf saveloc = dirp->dd_loc; /* save for possible EOF */
680Sstevel@tonic-gate dirp->dd_loc += (int)dp->d_reclen;
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate if (dirp->dd_loc >= dirp->dd_size)
720Sstevel@tonic-gate dirp->dd_loc = dirp->dd_size = 0;
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (dirp->dd_size == 0 && /* refill buffer */
750Sstevel@tonic-gate (dirp->dd_size = getdents(dirp->dd_fd,
763453Sraf (dirent_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
773453Sraf if (dirp->dd_size == 0) { /* This means EOF */
783453Sraf dirp->dd_loc = saveloc; /* so save for telldir */
793453Sraf lmutex_unlock(&pdirp->dd_lock);
800Sstevel@tonic-gate *result = NULL;
813453Sraf return (0);
820Sstevel@tonic-gate }
833453Sraf lmutex_unlock(&pdirp->dd_lock);
840Sstevel@tonic-gate *result = NULL;
853453Sraf return (errno); /* error */
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
883453Sraf dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
890Sstevel@tonic-gate (void) memcpy(entry, dp, (size_t)dp->d_reclen);
903453Sraf lmutex_unlock(&pdirp->dd_lock);
910Sstevel@tonic-gate *result = entry;
920Sstevel@tonic-gate return (0);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate #else /* _LP64 */
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * POSIX.1c standard version of the thr function readdir_r.
990Sstevel@tonic-gate * Large file version.
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate int
readdir64_r(DIR * dirp,dirent64_t * entry,dirent64_t ** result)1033453Sraf readdir64_r(DIR *dirp, dirent64_t *entry, dirent64_t **result)
1040Sstevel@tonic-gate {
1053453Sraf private_DIR *pdirp = (private_DIR *)(uintptr_t)dirp;
1063453Sraf dirent64_t *dp64; /* -> directory data */
1070Sstevel@tonic-gate int saveloc = 0;
1080Sstevel@tonic-gate
1093453Sraf lmutex_lock(&pdirp->dd_lock);
1100Sstevel@tonic-gate if (dirp->dd_size != 0) {
1113453Sraf dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
1120Sstevel@tonic-gate /* was converted by readdir and needs to be reversed */
1130Sstevel@tonic-gate if (dp64->d_ino == (ino64_t)-1) {
1143453Sraf dirent_t *dp32; /* -> 32 bit directory data */
1150Sstevel@tonic-gate
1163453Sraf dp32 = (dirent_t *)(&dp64->d_off);
1170Sstevel@tonic-gate dp64->d_ino = (ino64_t)dp32->d_ino;
1180Sstevel@tonic-gate dp64->d_off = (off64_t)dp32->d_off;
1190Sstevel@tonic-gate dp64->d_reclen = (unsigned short)(dp32->d_reclen +
1203453Sraf ((char *)&dp64->d_off - (char *)dp64));
1210Sstevel@tonic-gate }
1223453Sraf saveloc = dirp->dd_loc; /* save for possible EOF */
1230Sstevel@tonic-gate dirp->dd_loc += (int)dp64->d_reclen;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (dirp->dd_loc >= dirp->dd_size)
1270Sstevel@tonic-gate dirp->dd_loc = dirp->dd_size = 0;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate if (dirp->dd_size == 0 && /* refill buffer */
1300Sstevel@tonic-gate (dirp->dd_size = getdents64(dirp->dd_fd,
1313453Sraf (dirent64_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
1323453Sraf if (dirp->dd_size == 0) { /* This means EOF */
1333453Sraf dirp->dd_loc = saveloc; /* so save for telldir */
1343453Sraf lmutex_unlock(&pdirp->dd_lock);
1350Sstevel@tonic-gate *result = NULL;
1363453Sraf return (0);
1370Sstevel@tonic-gate }
1383453Sraf lmutex_unlock(&pdirp->dd_lock);
1390Sstevel@tonic-gate *result = NULL;
1403453Sraf return (errno); /* error */
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1433453Sraf dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
1440Sstevel@tonic-gate (void) memcpy(entry, dp64, (size_t)dp64->d_reclen);
1450Sstevel@tonic-gate *result = entry;
1463453Sraf lmutex_unlock(&pdirp->dd_lock);
1473453Sraf return (0);
1483453Sraf }
1493453Sraf
1503453Sraf /*
1513453Sraf * POSIX.1c standard version of the function readdir_r.
1523453Sraf * User gets it via static readdir_r from header file.
1533453Sraf */
1543453Sraf
1553453Sraf int
__posix_readdir_r(DIR * dirp,dirent_t * entry,dirent_t ** result)1563453Sraf __posix_readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
1573453Sraf {
1583453Sraf int error;
1593453Sraf dirent64_t *dp64;
1603453Sraf struct {
1613453Sraf dirent64_t dirent64;
1623453Sraf char chars[MAXNAMLEN];
1633453Sraf } buf;
1643453Sraf
1653453Sraf error = readdir64_r(dirp, (dirent64_t *)&buf, &dp64);
1663453Sraf if (error != 0 || dp64 == NULL) {
1673453Sraf *result = NULL;
1683453Sraf return (error);
1693453Sraf }
1703453Sraf
1713453Sraf if (dp64->d_ino > SIZE_MAX ||
1723453Sraf (uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) {
1733453Sraf *result = NULL;
1743453Sraf return (EOVERFLOW);
1753453Sraf }
1763453Sraf
1773453Sraf entry->d_ino = (ino_t)dp64->d_ino;
1783453Sraf entry->d_off = (off_t)dp64->d_off;
1793453Sraf entry->d_reclen = (unsigned short)((((char *)entry->d_name -
1803453Sraf (char *)entry) + strlen(dp64->d_name) + 1 + 3) & ~3);
1813453Sraf (void) strcpy(entry->d_name, dp64->d_name);
1823453Sraf *result = entry;
1830Sstevel@tonic-gate return (0);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate * POSIX.1c Draft-6 version of the function readdir_r.
1880Sstevel@tonic-gate * It was implemented by Solaris 2.3.
1890Sstevel@tonic-gate */
1900Sstevel@tonic-gate
1913453Sraf dirent_t *
readdir_r(DIR * dirp,dirent_t * entry)1923453Sraf readdir_r(DIR *dirp, dirent_t *entry)
1930Sstevel@tonic-gate {
1943453Sraf int error;
1953453Sraf dirent_t *result;
1960Sstevel@tonic-gate
1973453Sraf if ((error = __posix_readdir_r(dirp, entry, &result)) != 0)
1983453Sraf errno = error;
1993453Sraf return (result);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate #endif /* _LP64 */
203