/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2012 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* opendir, closedir24*25* open|close directory stream26*27* POSIX compatible directory stream access routines:28*29* #include <sys/types.h>30* #include <dirent.h>31*32* NOTE: readdir() returns a pointer to struct dirent33*/3435#include "dirlib.h"3637#if _dir_ok3839NoN(opendir)4041#else4243static const char id_dir[] = "\n@(#)$Id: directory (AT&T Research) 1993-04-01 $\0\n";4445static DIR* freedirp; /* always keep one dirp */4647DIR*48opendir(register const char* path)49{50register DIR* dirp = 0;51register int fd;52struct stat st;5354if ((fd = open(path, O_RDONLY|O_cloexec)) < 0) return(0);55if (fstat(fd, &st) < 0 ||56!S_ISDIR(st.st_mode) && (errno = ENOTDIR) ||57#if !O_cloexec58fcntl(fd, F_SETFD, FD_CLOEXEC) ||59#endif60!(dirp = freedirp ? freedirp :61#if defined(_DIR_PRIVATE_) || _ptr_dd_buf62newof(0, DIR, 1, DIRBLKSIZ)63#else64newof(0, DIR, 1, 0)65#endif66))67{68close(fd);69if (dirp)70{71if (!freedirp) freedirp = dirp;72else free(dirp);73}74return(0);75}76freedirp = 0;77dirp->dd_fd = fd;78dirp->dd_loc = dirp->dd_size = 0; /* refill needed */79#if defined(_DIR_PRIVATE_) || _ptr_dd_buf80dirp->dd_buf = (void*)((char*)dirp + sizeof(DIR));81#endif82return(dirp);83}8485void86closedir(register DIR* dirp)87{88if (dirp)89{90close(dirp->dd_fd);91if (!freedirp) freedirp = dirp;92else free(dirp);93}94}9596#endif979899