2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1992, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * readdir_r -- C library extension routine
2N/A */
2N/A
2N/A#include <sys/feature_tests.h>
2N/A
2N/A#if !defined(_LP64)
2N/A#pragma weak _readdir64_r = readdir64_r
2N/A#endif
2N/A
2N/A#include "lint.h"
2N/A#include "libc.h"
2N/A#include <mtlib.h>
2N/A#include <unistd.h>
2N/A#include <dirent.h>
2N/A#include <string.h>
2N/A#include <limits.h>
2N/A#include <errno.h>
2N/A
2N/A#ifdef _LP64
2N/A
2N/A/*
2N/A * POSIX.1c standard version of the thread function readdir_r.
2N/A */
2N/A
2N/Aint
2N/Areaddir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
2N/A{
2N/A private_DIR *pdirp = (private_DIR *)dirp;
2N/A dirent_t *dp; /* -> directory data */
2N/A int saveloc = 0;
2N/A
2N/A lmutex_lock(&pdirp->d_lock);
2N/A if (dirp->d_size != 0) {
2N/A dp = (dirent_t *)(uintptr_t)&dirp->d_buf[dirp->d_loc];
2N/A saveloc = dirp->d_loc; /* save for possible EOF */
2N/A dirp->d_loc += (int)dp->d_reclen;
2N/A }
2N/A
2N/A if (dirp->d_loc >= dirp->d_size)
2N/A dirp->d_loc = dirp->d_size = 0;
2N/A
2N/A if (dirp->d_size == 0 && /* refill buffer */
2N/A (dirp->d_size = getdents(dirp->d_fd,
2N/A (dirent_t *)(uintptr_t)dirp->d_buf, DIRBUF)) <= 0) {
2N/A if (dirp->d_size == 0) { /* This means EOF */
2N/A dirp->d_loc = saveloc; /* so save for telldir */
2N/A lmutex_unlock(&pdirp->d_lock);
2N/A *result = NULL;
2N/A return (0);
2N/A }
2N/A lmutex_unlock(&pdirp->d_lock);
2N/A *result = NULL;
2N/A return (errno); /* error */
2N/A }
2N/A
2N/A dp = (dirent_t *)(uintptr_t)&dirp->d_buf[dirp->d_loc];
2N/A (void) memcpy(entry, dp, (size_t)dp->d_reclen);
2N/A lmutex_unlock(&pdirp->d_lock);
2N/A *result = entry;
2N/A return (0);
2N/A}
2N/A
2N/A#else /* _LP64 */
2N/A
2N/A/*
2N/A * POSIX.1c standard version of the thr function readdir_r.
2N/A * Large file version.
2N/A */
2N/A
2N/Aint
2N/Areaddir64_r(DIR *dirp, dirent64_t *entry, dirent64_t **result)
2N/A{
2N/A private_DIR *pdirp = (private_DIR *)(uintptr_t)dirp;
2N/A dirent64_t *dp64; /* -> directory data */
2N/A int saveloc = 0;
2N/A
2N/A lmutex_lock(&pdirp->d_lock);
2N/A if (dirp->d_size != 0) {
2N/A dp64 = (dirent64_t *)(uintptr_t)&dirp->d_buf[dirp->d_loc];
2N/A /* was converted by readdir and needs to be reversed */
2N/A if (dp64->d_ino == (ino64_t)-1) {
2N/A dirent_t *dp32; /* -> 32 bit directory data */
2N/A
2N/A dp32 = (dirent_t *)(&dp64->d_off);
2N/A dp64->d_ino = (ino64_t)dp32->d_ino;
2N/A dp64->d_off = (off64_t)dp32->d_off;
2N/A dp64->d_reclen = (unsigned short)(dp32->d_reclen +
2N/A ((char *)&dp64->d_off - (char *)dp64));
2N/A }
2N/A saveloc = dirp->d_loc; /* save for possible EOF */
2N/A dirp->d_loc += (int)dp64->d_reclen;
2N/A }
2N/A
2N/A if (dirp->d_loc >= dirp->d_size)
2N/A dirp->d_loc = dirp->d_size = 0;
2N/A
2N/A if (dirp->d_size == 0 && /* refill buffer */
2N/A (dirp->d_size = getdents64(dirp->d_fd,
2N/A (dirent64_t *)(uintptr_t)dirp->d_buf, DIRBUF)) <= 0) {
2N/A if (dirp->d_size == 0) { /* This means EOF */
2N/A dirp->d_loc = saveloc; /* so save for telldir */
2N/A lmutex_unlock(&pdirp->d_lock);
2N/A *result = NULL;
2N/A return (0);
2N/A }
2N/A lmutex_unlock(&pdirp->d_lock);
2N/A *result = NULL;
2N/A return (errno); /* error */
2N/A }
2N/A
2N/A dp64 = (dirent64_t *)(uintptr_t)&dirp->d_buf[dirp->d_loc];
2N/A (void) memcpy(entry, dp64, (size_t)dp64->d_reclen);
2N/A *result = entry;
2N/A lmutex_unlock(&pdirp->d_lock);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * POSIX.1c standard version of the function readdir_r.
2N/A * User gets it via static readdir_r from header file.
2N/A */
2N/A
2N/Aint
2N/A__posix_readdir_r(DIR *dirp, dirent_t *entry, dirent_t **result)
2N/A{
2N/A int error;
2N/A dirent64_t *dp64;
2N/A struct {
2N/A dirent64_t dirent64;
2N/A char chars[MAXNAMLEN];
2N/A } buf;
2N/A
2N/A error = readdir64_r(dirp, (dirent64_t *)&buf, &dp64);
2N/A if (error != 0 || dp64 == NULL) {
2N/A *result = NULL;
2N/A return (error);
2N/A }
2N/A
2N/A if (dp64->d_ino > SIZE_MAX ||
2N/A (uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) {
2N/A *result = NULL;
2N/A return (EOVERFLOW);
2N/A }
2N/A
2N/A entry->d_ino = (ino_t)dp64->d_ino;
2N/A entry->d_off = (off_t)dp64->d_off;
2N/A entry->d_reclen = (unsigned short)((((char *)entry->d_name -
2N/A (char *)entry) + strlen(dp64->d_name) + 1 + 3) & ~3);
2N/A (void) strcpy(entry->d_name, dp64->d_name);
2N/A *result = entry;
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * POSIX.1c Draft-6 version of the function readdir_r.
2N/A * It was implemented by Solaris 2.3.
2N/A */
2N/A
2N/Adirent_t *
2N/Areaddir_r(DIR *dirp, dirent_t *entry)
2N/A{
2N/A int error;
2N/A dirent_t *result;
2N/A
2N/A if ((error = __posix_readdir_r(dirp, entry, &result)) != 0)
2N/A errno = error;
2N/A return (result);
2N/A}
2N/A
2N/A#endif /* _LP64 */