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 /*
23a574db85Sraf * 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 #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
30289b68b2Sraf * fdopendir, dirfd -- C library extension routines
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * We use lmalloc()/lfree() rather than malloc()/free() in
337c478bd9Sstevel@tonic-gate * order to allow opendir()/readdir()/closedir() to be called
347c478bd9Sstevel@tonic-gate * while holding internal libc locks.
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
37*7257d1b4Sraf #pragma weak _fdopendir = fdopendir
387c478bd9Sstevel@tonic-gate
39*7257d1b4Sraf #include "lint.h"
407c478bd9Sstevel@tonic-gate #include <mtlib.h>
417c478bd9Sstevel@tonic-gate #include <dirent.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include "libc.h"
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate DIR *
fdopendir(int fd)507c478bd9Sstevel@tonic-gate fdopendir(int fd)
517c478bd9Sstevel@tonic-gate {
52494a4c51Sraf private_DIR *pdirp = lmalloc(sizeof (*pdirp));
53494a4c51Sraf DIR *dirp = (DIR *)pdirp;
547c478bd9Sstevel@tonic-gate void *buf = lmalloc(DIRBUF);
557c478bd9Sstevel@tonic-gate int error = 0;
567c478bd9Sstevel@tonic-gate struct stat64 sbuf;
577c478bd9Sstevel@tonic-gate
58494a4c51Sraf if (pdirp == NULL || buf == NULL)
597c478bd9Sstevel@tonic-gate goto fail;
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * POSIX mandated behavior
627c478bd9Sstevel@tonic-gate * close on exec if using file descriptor
637c478bd9Sstevel@tonic-gate */
64a574db85Sraf if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
657c478bd9Sstevel@tonic-gate goto fail;
667c478bd9Sstevel@tonic-gate if (fstat64(fd, &sbuf) < 0)
677c478bd9Sstevel@tonic-gate goto fail;
687c478bd9Sstevel@tonic-gate if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
697c478bd9Sstevel@tonic-gate error = ENOTDIR;
707c478bd9Sstevel@tonic-gate goto fail;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate dirp->dd_buf = buf;
737c478bd9Sstevel@tonic-gate dirp->dd_fd = fd;
74494a4c51Sraf dirp->dd_loc = 0;
75494a4c51Sraf dirp->dd_size = 0;
76494a4c51Sraf (void) mutex_init(&pdirp->dd_lock, USYNC_THREAD, NULL);
777c478bd9Sstevel@tonic-gate return (dirp);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate fail:
80494a4c51Sraf if (pdirp != NULL)
81494a4c51Sraf lfree(pdirp, sizeof (*pdirp));
827c478bd9Sstevel@tonic-gate if (buf != NULL)
837c478bd9Sstevel@tonic-gate lfree(buf, DIRBUF);
847c478bd9Sstevel@tonic-gate if (error)
857c478bd9Sstevel@tonic-gate errno = error;
867c478bd9Sstevel@tonic-gate return (NULL);
877c478bd9Sstevel@tonic-gate }
88289b68b2Sraf
89289b68b2Sraf int
dirfd(DIR * dirp)90289b68b2Sraf dirfd(DIR *dirp)
91289b68b2Sraf {
92289b68b2Sraf return (dirp->dd_fd);
93289b68b2Sraf }
94