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 * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * readdir -- C library extension routine 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #if !defined(_LP64) 49*7257d1b4Sraf #pragma weak _readdir64 = readdir64 507c478bd9Sstevel@tonic-gate #endif 51*7257d1b4Sraf #pragma weak _readdir = readdir 527c478bd9Sstevel@tonic-gate 53*7257d1b4Sraf #include "lint.h" 547c478bd9Sstevel@tonic-gate #include <dirent.h> 557c478bd9Sstevel@tonic-gate #include <limits.h> 567c478bd9Sstevel@tonic-gate #include <errno.h> 577c478bd9Sstevel@tonic-gate #include "libc.h" 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #ifdef _LP64 607c478bd9Sstevel@tonic-gate 61494a4c51Sraf dirent_t * 627c478bd9Sstevel@tonic-gate readdir(DIR *dirp) 637c478bd9Sstevel@tonic-gate { 64494a4c51Sraf dirent_t *dp; /* -> directory data */ 657c478bd9Sstevel@tonic-gate int saveloc = 0; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate if (dirp->dd_size != 0) { 68494a4c51Sraf dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]; 697c478bd9Sstevel@tonic-gate saveloc = dirp->dd_loc; /* save for possible EOF */ 707c478bd9Sstevel@tonic-gate dirp->dd_loc += (int)dp->d_reclen; 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate if (dirp->dd_loc >= dirp->dd_size) 737c478bd9Sstevel@tonic-gate dirp->dd_loc = dirp->dd_size = 0; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate if (dirp->dd_size == 0 && /* refill buffer */ 767c478bd9Sstevel@tonic-gate (dirp->dd_size = getdents(dirp->dd_fd, 77494a4c51Sraf (dirent_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) { 787c478bd9Sstevel@tonic-gate if (dirp->dd_size == 0) /* This means EOF */ 79494a4c51Sraf dirp->dd_loc = saveloc; /* so save for telldir */ 807c478bd9Sstevel@tonic-gate return (NULL); /* error or EOF */ 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate 83494a4c51Sraf return ((dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate #else /* _LP64 */ 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * Welcome to the complicated world of large files on a small system. 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate 92494a4c51Sraf dirent64_t * 937c478bd9Sstevel@tonic-gate readdir64(DIR *dirp) 947c478bd9Sstevel@tonic-gate { 95494a4c51Sraf dirent64_t *dp64; /* -> directory data */ 967c478bd9Sstevel@tonic-gate int saveloc = 0; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate if (dirp->dd_size != 0) { 99494a4c51Sraf dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]; 1007c478bd9Sstevel@tonic-gate /* was converted by readdir and needs to be reversed */ 1017c478bd9Sstevel@tonic-gate if (dp64->d_ino == (ino64_t)-1) { 102494a4c51Sraf dirent_t *dp32; 1037c478bd9Sstevel@tonic-gate 104494a4c51Sraf dp32 = (dirent_t *)(&dp64->d_off); 1057c478bd9Sstevel@tonic-gate dp64->d_ino = (ino64_t)dp32->d_ino; 1067c478bd9Sstevel@tonic-gate dp64->d_off = (off64_t)dp32->d_off; 1077c478bd9Sstevel@tonic-gate dp64->d_reclen = (unsigned short)(dp32->d_reclen + 1087c478bd9Sstevel@tonic-gate ((char *)&dp64->d_off - (char *)dp64)); 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate saveloc = dirp->dd_loc; /* save for possible EOF */ 1117c478bd9Sstevel@tonic-gate dirp->dd_loc += (int)dp64->d_reclen; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate if (dirp->dd_loc >= dirp->dd_size) 1147c478bd9Sstevel@tonic-gate dirp->dd_loc = dirp->dd_size = 0; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate if (dirp->dd_size == 0 && /* refill buffer */ 1177c478bd9Sstevel@tonic-gate (dirp->dd_size = getdents64(dirp->dd_fd, 118494a4c51Sraf (dirent64_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) { 1197c478bd9Sstevel@tonic-gate if (dirp->dd_size == 0) /* This means EOF */ 120494a4c51Sraf dirp->dd_loc = saveloc; /* so save for telldir */ 1217c478bd9Sstevel@tonic-gate return (NULL); /* error or EOF */ 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 124494a4c51Sraf dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]; 1257c478bd9Sstevel@tonic-gate return (dp64); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate /* 1297c478bd9Sstevel@tonic-gate * readdir now does translation of dirent64 entries into dirent entries. 1307c478bd9Sstevel@tonic-gate * We rely on the fact that dirents are smaller than dirent64s and we 1317c478bd9Sstevel@tonic-gate * reuse the space accordingly. 1327c478bd9Sstevel@tonic-gate */ 133494a4c51Sraf dirent_t * 1347c478bd9Sstevel@tonic-gate readdir(DIR *dirp) 1357c478bd9Sstevel@tonic-gate { 136494a4c51Sraf dirent64_t *dp64; /* -> directory data */ 137494a4c51Sraf dirent_t *dp32; /* -> directory data */ 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if ((dp64 = readdir64(dirp)) == NULL) 1407c478bd9Sstevel@tonic-gate return (NULL); 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* 1437c478bd9Sstevel@tonic-gate * Make sure that the offset fits in 32 bits. 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate if (((off_t)dp64->d_off != dp64->d_off && 1467c478bd9Sstevel@tonic-gate (uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) || 147494a4c51Sraf dp64->d_ino > SIZE_MAX) { 1487c478bd9Sstevel@tonic-gate errno = EOVERFLOW; 1497c478bd9Sstevel@tonic-gate return (NULL); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 152494a4c51Sraf dp32 = (dirent_t *)(&dp64->d_off); 1537c478bd9Sstevel@tonic-gate dp32->d_off = (off_t)dp64->d_off; 1547c478bd9Sstevel@tonic-gate dp32->d_ino = (ino_t)dp64->d_ino; 1557c478bd9Sstevel@tonic-gate dp32->d_reclen = (unsigned short)(dp64->d_reclen - 1567c478bd9Sstevel@tonic-gate ((char *)&dp64->d_off - (char *)dp64)); 1577c478bd9Sstevel@tonic-gate dp64->d_ino = (ino64_t)-1; /* flag as converted for readdir64 */ 1587c478bd9Sstevel@tonic-gate /* d_name d_reclen should not move */ 1597c478bd9Sstevel@tonic-gate return (dp32); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate #endif /* _LP64 */ 162