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