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 /*
28289b68b2Sraf * fdopendir, dirfd -- C library extension routines
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * We use lmalloc()/lfree() rather than malloc()/free() in
317c478bd9Sstevel@tonic-gate * order to allow opendir()/readdir()/closedir() to be called
327c478bd9Sstevel@tonic-gate * while holding internal libc locks.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
35*7257d1b4Sraf #pragma weak _fdopendir = fdopendir
367c478bd9Sstevel@tonic-gate
37*7257d1b4Sraf #include "lint.h"
387c478bd9Sstevel@tonic-gate #include <mtlib.h>
397c478bd9Sstevel@tonic-gate #include <dirent.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include "libc.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate DIR *
fdopendir(int fd)487c478bd9Sstevel@tonic-gate fdopendir(int fd)
497c478bd9Sstevel@tonic-gate {
50494a4c51Sraf private_DIR *pdirp = lmalloc(sizeof (*pdirp));
51494a4c51Sraf DIR *dirp = (DIR *)pdirp;
527c478bd9Sstevel@tonic-gate void *buf = lmalloc(DIRBUF);
537c478bd9Sstevel@tonic-gate int error = 0;
547c478bd9Sstevel@tonic-gate struct stat64 sbuf;
557c478bd9Sstevel@tonic-gate
56494a4c51Sraf if (pdirp == NULL || buf == NULL)
577c478bd9Sstevel@tonic-gate goto fail;
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * POSIX mandated behavior
607c478bd9Sstevel@tonic-gate * close on exec if using file descriptor
617c478bd9Sstevel@tonic-gate */
62a574db85Sraf if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
637c478bd9Sstevel@tonic-gate goto fail;
647c478bd9Sstevel@tonic-gate if (fstat64(fd, &sbuf) < 0)
657c478bd9Sstevel@tonic-gate goto fail;
667c478bd9Sstevel@tonic-gate if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
677c478bd9Sstevel@tonic-gate error = ENOTDIR;
687c478bd9Sstevel@tonic-gate goto fail;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate dirp->dd_buf = buf;
717c478bd9Sstevel@tonic-gate dirp->dd_fd = fd;
72494a4c51Sraf dirp->dd_loc = 0;
73494a4c51Sraf dirp->dd_size = 0;
74494a4c51Sraf (void) mutex_init(&pdirp->dd_lock, USYNC_THREAD, NULL);
757c478bd9Sstevel@tonic-gate return (dirp);
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate fail:
78494a4c51Sraf if (pdirp != NULL)
79494a4c51Sraf lfree(pdirp, sizeof (*pdirp));
807c478bd9Sstevel@tonic-gate if (buf != NULL)
817c478bd9Sstevel@tonic-gate lfree(buf, DIRBUF);
827c478bd9Sstevel@tonic-gate if (error)
837c478bd9Sstevel@tonic-gate errno = error;
847c478bd9Sstevel@tonic-gate return (NULL);
857c478bd9Sstevel@tonic-gate }
86289b68b2Sraf
87289b68b2Sraf int
dirfd(DIR * dirp)88289b68b2Sraf dirfd(DIR *dirp)
89289b68b2Sraf {
90289b68b2Sraf return (dirp->dd_fd);
91289b68b2Sraf }
92