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
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * readdir -- C library extension routine
440Sstevel@tonic-gate */
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <sys/feature_tests.h>
470Sstevel@tonic-gate
480Sstevel@tonic-gate #if !defined(_LP64)
49*6812Sraf #pragma weak _readdir64 = readdir64
500Sstevel@tonic-gate #endif
51*6812Sraf #pragma weak _readdir = readdir
520Sstevel@tonic-gate
53*6812Sraf #include "lint.h"
543453Sraf #include <dirent.h>
553453Sraf #include <limits.h>
563453Sraf #include <errno.h>
573453Sraf #include "libc.h"
580Sstevel@tonic-gate
590Sstevel@tonic-gate #ifdef _LP64
600Sstevel@tonic-gate
613453Sraf dirent_t *
readdir(DIR * dirp)620Sstevel@tonic-gate readdir(DIR *dirp)
630Sstevel@tonic-gate {
643453Sraf dirent_t *dp; /* -> directory data */
650Sstevel@tonic-gate int saveloc = 0;
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (dirp->dd_size != 0) {
683453Sraf dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
693453Sraf saveloc = dirp->dd_loc; /* save for possible EOF */
700Sstevel@tonic-gate dirp->dd_loc += (int)dp->d_reclen;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate if (dirp->dd_loc >= dirp->dd_size)
730Sstevel@tonic-gate dirp->dd_loc = dirp->dd_size = 0;
740Sstevel@tonic-gate
750Sstevel@tonic-gate if (dirp->dd_size == 0 && /* refill buffer */
760Sstevel@tonic-gate (dirp->dd_size = getdents(dirp->dd_fd,
773453Sraf (dirent_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
783453Sraf if (dirp->dd_size == 0) /* This means EOF */
793453Sraf dirp->dd_loc = saveloc; /* so save for telldir */
803453Sraf return (NULL); /* error or EOF */
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
833453Sraf return ((dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate #else /* _LP64 */
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * Welcome to the complicated world of large files on a small system.
900Sstevel@tonic-gate */
910Sstevel@tonic-gate
923453Sraf dirent64_t *
readdir64(DIR * dirp)930Sstevel@tonic-gate readdir64(DIR *dirp)
940Sstevel@tonic-gate {
953453Sraf dirent64_t *dp64; /* -> directory data */
960Sstevel@tonic-gate int saveloc = 0;
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (dirp->dd_size != 0) {
993453Sraf dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
1000Sstevel@tonic-gate /* was converted by readdir and needs to be reversed */
1010Sstevel@tonic-gate if (dp64->d_ino == (ino64_t)-1) {
1023453Sraf dirent_t *dp32;
1030Sstevel@tonic-gate
1043453Sraf dp32 = (dirent_t *)(&dp64->d_off);
1050Sstevel@tonic-gate dp64->d_ino = (ino64_t)dp32->d_ino;
1060Sstevel@tonic-gate dp64->d_off = (off64_t)dp32->d_off;
1070Sstevel@tonic-gate dp64->d_reclen = (unsigned short)(dp32->d_reclen +
1083453Sraf ((char *)&dp64->d_off - (char *)dp64));
1090Sstevel@tonic-gate }
1103453Sraf saveloc = dirp->dd_loc; /* save for possible EOF */
1110Sstevel@tonic-gate dirp->dd_loc += (int)dp64->d_reclen;
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate if (dirp->dd_loc >= dirp->dd_size)
1140Sstevel@tonic-gate dirp->dd_loc = dirp->dd_size = 0;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate if (dirp->dd_size == 0 && /* refill buffer */
1170Sstevel@tonic-gate (dirp->dd_size = getdents64(dirp->dd_fd,
1183453Sraf (dirent64_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
1193453Sraf if (dirp->dd_size == 0) /* This means EOF */
1203453Sraf dirp->dd_loc = saveloc; /* so save for telldir */
1213453Sraf return (NULL); /* error or EOF */
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1243453Sraf dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
1250Sstevel@tonic-gate return (dp64);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * readdir now does translation of dirent64 entries into dirent entries.
1300Sstevel@tonic-gate * We rely on the fact that dirents are smaller than dirent64s and we
1310Sstevel@tonic-gate * reuse the space accordingly.
1320Sstevel@tonic-gate */
1333453Sraf dirent_t *
readdir(DIR * dirp)1340Sstevel@tonic-gate readdir(DIR *dirp)
1350Sstevel@tonic-gate {
1363453Sraf dirent64_t *dp64; /* -> directory data */
1373453Sraf dirent_t *dp32; /* -> directory data */
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if ((dp64 = readdir64(dirp)) == NULL)
1400Sstevel@tonic-gate return (NULL);
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate * Make sure that the offset fits in 32 bits.
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate if (((off_t)dp64->d_off != dp64->d_off &&
1463453Sraf (uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) ||
1473453Sraf dp64->d_ino > SIZE_MAX) {
1483453Sraf errno = EOVERFLOW;
1493453Sraf return (NULL);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1523453Sraf dp32 = (dirent_t *)(&dp64->d_off);
1530Sstevel@tonic-gate dp32->d_off = (off_t)dp64->d_off;
1540Sstevel@tonic-gate dp32->d_ino = (ino_t)dp64->d_ino;
1550Sstevel@tonic-gate dp32->d_reclen = (unsigned short)(dp64->d_reclen -
1560Sstevel@tonic-gate ((char *)&dp64->d_off - (char *)dp64));
1573453Sraf dp64->d_ino = (ino64_t)-1; /* flag as converted for readdir64 */
1580Sstevel@tonic-gate /* d_name d_reclen should not move */
1590Sstevel@tonic-gate return (dp32);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate #endif /* _LP64 */
162