fdopendir.c revision 2
ca6d7ba96138ee0d42364692a5e413bb220c4675jl/*
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * CDDL HEADER START
ca6d7ba96138ee0d42364692a5e413bb220c4675jl *
940d71d237794874e18a0eb72f6564821a823517eschrock * The contents of this file are subject to the terms of the
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * Common Development and Distribution License (the "License").
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * You may not use this file except in compliance with the License.
ca6d7ba96138ee0d42364692a5e413bb220c4675jl *
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * or http://www.opensolaris.org/os/licensing.
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * See the License for the specific language governing permissions
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * and limitations under the License.
ca6d7ba96138ee0d42364692a5e413bb220c4675jl *
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * When distributing Covered Code, include this CDDL HEADER in each
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * If applicable, add the following below this CDDL HEADER, with the
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * fields enclosed by brackets "[]" replaced with your own identifying
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * information: Portions Copyright [yyyy] [name of copyright owner]
ca6d7ba96138ee0d42364692a5e413bb220c4675jl *
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * CDDL HEADER END
ca6d7ba96138ee0d42364692a5e413bb220c4675jl */
ca6d7ba96138ee0d42364692a5e413bb220c4675jl
ca6d7ba96138ee0d42364692a5e413bb220c4675jl/*
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
ca6d7ba96138ee0d42364692a5e413bb220c4675jl */
ca6d7ba96138ee0d42364692a5e413bb220c4675jl
ca6d7ba96138ee0d42364692a5e413bb220c4675jl/*
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * fdopendir, dirfd -- C library extension routines
ca6d7ba96138ee0d42364692a5e413bb220c4675jl *
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * We use lmalloc()/lfree() rather than malloc()/free() in
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * order to allow opendir()/readdir()/closedir() to be called
ca6d7ba96138ee0d42364692a5e413bb220c4675jl * while holding internal libc locks.
ca6d7ba96138ee0d42364692a5e413bb220c4675jl */
ca6d7ba96138ee0d42364692a5e413bb220c4675jl
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#pragma weak _fdopendir = fdopendir
ca6d7ba96138ee0d42364692a5e413bb220c4675jl
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include "lint.h"
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include <mtlib.h>
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include <dirent.h>
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include <sys/stat.h>
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include <fcntl.h>
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include <stdlib.h>
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include <unistd.h>
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include <errno.h>
ca6d7ba96138ee0d42364692a5e413bb220c4675jl#include "libc.h"
ca6d7ba96138ee0d42364692a5e413bb220c4675jl
940d71d237794874e18a0eb72f6564821a823517eschrockDIR *
940d71d237794874e18a0eb72f6564821a823517eschrockfdopendir(int fd)
940d71d237794874e18a0eb72f6564821a823517eschrock{
ca6d7ba96138ee0d42364692a5e413bb220c4675jl private_DIR *pdirp = lmalloc(sizeof (*pdirp));
DIR *dirp = (DIR *)pdirp;
void *buf = lmalloc(DIRBUF);
int error = 0;
struct stat64 sbuf;
if (pdirp == NULL || buf == NULL)
goto fail;
/*
* POSIX mandated behavior
* close on exec if using file descriptor
*/
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
goto fail;
if (fstat64(fd, &sbuf) < 0)
goto fail;
if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
error = ENOTDIR;
goto fail;
}
dirp->d_buf = buf;
dirp->d_fd = fd;
dirp->d_loc = 0;
dirp->d_size = 0;
(void) mutex_init(&pdirp->d_lock, USYNC_THREAD, NULL);
return (dirp);
fail:
if (pdirp != NULL)
lfree(pdirp, sizeof (*pdirp));
if (buf != NULL)
lfree(buf, DIRBUF);
if (error)
errno = error;
return (NULL);
}
int
dirfd(DIR *dirp)
{
return (dirp->d_fd);
}