1 1.27 riastrad /* $NetBSD: readdir.c,v 1.27 2024/09/10 17:11:19 riastradh Exp $ */ 2 1.5 cgd 3 1.20 christos /* 4 1.20 christos * Copyright (c) 1983, 1993 5 1.20 christos * The Regents of the University of California. All rights reserved. 6 1.20 christos * 7 1.20 christos * Redistribution and use in source and binary forms, with or without 8 1.20 christos * modification, are permitted provided that the following conditions 9 1.20 christos * are met: 10 1.20 christos * 1. Redistributions of source code must retain the above copyright 11 1.20 christos * notice, this list of conditions and the following disclaimer. 12 1.20 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.20 christos * notice, this list of conditions and the following disclaimer in the 14 1.20 christos * documentation and/or other materials provided with the distribution. 15 1.20 christos * 3. Neither the name of the University nor the names of its contributors 16 1.20 christos * may be used to endorse or promote products derived from this software 17 1.20 christos * without specific prior written permission. 18 1.20 christos * 19 1.20 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.20 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.20 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.20 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.20 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.20 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.20 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.20 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.20 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.20 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.20 christos * SUCH DAMAGE. 30 1.20 christos */ 31 1.1 cgd 32 1.20 christos #include <sys/cdefs.h> 33 1.20 christos #if defined(LIBC_SCCS) && !defined(lint) 34 1.20 christos #if 0 35 1.20 christos static char sccsid[] = "@(#)readdir.c 8.3 (Berkeley) 9/29/94"; 36 1.20 christos #else 37 1.27 riastrad __RCSID("$NetBSD: readdir.c,v 1.27 2024/09/10 17:11:19 riastradh Exp $"); 38 1.20 christos #endif 39 1.20 christos #endif /* LIBC_SCCS and not lint */ 40 1.20 christos 41 1.20 christos #include "namespace.h" 42 1.20 christos #include "reentrant.h" 43 1.22 christos #include "extern.h" 44 1.20 christos #include <sys/param.h> 45 1.20 christos 46 1.20 christos #include <dirent.h> 47 1.20 christos #include <errno.h> 48 1.20 christos #include <string.h> 49 1.20 christos #include <unistd.h> 50 1.20 christos 51 1.23 christos #include "dirent_private.h" 52 1.23 christos 53 1.20 christos /* 54 1.20 christos * get next entry in a directory. 55 1.20 christos */ 56 1.21 christos struct dirent * 57 1.24 tonnerre _readdir_unlocked(DIR *dirp, int skipdeleted) 58 1.20 christos { 59 1.27 riastrad const int saved_errno = errno; 60 1.20 christos struct dirent *dp; 61 1.20 christos 62 1.20 christos for (;;) { 63 1.20 christos if (dirp->dd_loc >= dirp->dd_size) { 64 1.20 christos if (dirp->dd_flags & __DTF_READALL) 65 1.27 riastrad break; 66 1.20 christos dirp->dd_loc = 0; 67 1.20 christos } 68 1.20 christos if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) { 69 1.20 christos dirp->dd_seek = lseek(dirp->dd_fd, (off_t)0, SEEK_CUR); 70 1.20 christos dirp->dd_size = getdents(dirp->dd_fd, 71 1.20 christos dirp->dd_buf, (size_t)dirp->dd_len); 72 1.27 riastrad if (dirp->dd_size == 0) /* end of directory */ 73 1.27 riastrad break; 74 1.27 riastrad if (dirp->dd_size == -1) /* getdents sets errno */ 75 1.27 riastrad return NULL; 76 1.27 riastrad if (dirp->dd_size < 0) { /* paranoia */ 77 1.27 riastrad errno = EIO; 78 1.27 riastrad return NULL; 79 1.27 riastrad } 80 1.20 christos } 81 1.20 christos dp = (struct dirent *) 82 1.20 christos (void *)(dirp->dd_buf + (size_t)dirp->dd_loc); 83 1.27 riastrad /* bogus pointer check */ 84 1.27 riastrad if ((intptr_t)dp & _DIRENT_ALIGN(dp)) { 85 1.27 riastrad errno = EIO; 86 1.27 riastrad return NULL; 87 1.27 riastrad } 88 1.20 christos /* d_reclen is unsigned; no need to compare it <= 0 */ 89 1.27 riastrad if (dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) { 90 1.27 riastrad errno = EIO; 91 1.27 riastrad return NULL; 92 1.27 riastrad } 93 1.20 christos dirp->dd_loc += dp->d_reclen; 94 1.24 tonnerre if (dp->d_ino == 0 && skipdeleted) 95 1.20 christos continue; 96 1.20 christos if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW)) 97 1.20 christos continue; 98 1.27 riastrad return dp; 99 1.20 christos } 100 1.27 riastrad 101 1.27 riastrad errno = saved_errno; 102 1.27 riastrad return NULL; 103 1.20 christos } 104 1.20 christos 105 1.20 christos struct dirent * 106 1.26 abs readdir(DIR *dirp) 107 1.20 christos { 108 1.20 christos struct dirent *dp; 109 1.20 christos 110 1.20 christos #ifdef _REENTRANT 111 1.20 christos if (__isthreaded) { 112 1.20 christos mutex_lock((mutex_t *)dirp->dd_lock); 113 1.24 tonnerre dp = _readdir_unlocked(dirp, 1); 114 1.20 christos mutex_unlock((mutex_t *)dirp->dd_lock); 115 1.20 christos } 116 1.20 christos else 117 1.20 christos #endif 118 1.24 tonnerre dp = _readdir_unlocked(dirp, 1); 119 1.20 christos return (dp); 120 1.20 christos } 121 1.20 christos 122 1.20 christos int 123 1.26 abs readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) 124 1.20 christos { 125 1.20 christos struct dirent *dp; 126 1.20 christos int saved_errno; 127 1.20 christos 128 1.20 christos saved_errno = errno; 129 1.20 christos errno = 0; 130 1.20 christos #ifdef _REENTRANT 131 1.20 christos if (__isthreaded) { 132 1.20 christos mutex_lock((mutex_t *)dirp->dd_lock); 133 1.24 tonnerre if ((dp = _readdir_unlocked(dirp, 1)) != NULL) 134 1.20 christos memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp)); 135 1.20 christos mutex_unlock((mutex_t *)dirp->dd_lock); 136 1.20 christos } 137 1.20 christos else 138 1.20 christos #endif 139 1.24 tonnerre if ((dp = _readdir_unlocked(dirp, 1)) != NULL) 140 1.20 christos memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp)); 141 1.20 christos 142 1.20 christos if (errno != 0) { 143 1.20 christos if (dp == NULL) 144 1.20 christos return (errno); 145 1.20 christos } else 146 1.20 christos errno = saved_errno; 147 1.20 christos 148 1.20 christos if (dp != NULL) 149 1.20 christos *result = entry; 150 1.20 christos else 151 1.20 christos *result = NULL; 152 1.20 christos 153 1.20 christos return (0); 154 1.20 christos } 155