14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 78462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * Glenn Fowler <gsf@research.att.com> * 184887Schin * David Korn <dgk@research.att.com> * 194887Schin * Phong Vo <kpv@research.att.com> * 204887Schin * * 214887Schin ***********************************************************************/ 224887Schin #pragma prototyped 234887Schin /* 244887Schin * opendir, closedir 254887Schin * 264887Schin * open|close directory stream 274887Schin * 284887Schin * POSIX compatible directory stream access routines: 294887Schin * 304887Schin * #include <sys/types.h> 314887Schin * #include <dirent.h> 324887Schin * 334887Schin * NOTE: readdir() returns a pointer to struct dirent 344887Schin */ 354887Schin 364887Schin #include "dirlib.h" 374887Schin 384887Schin #if _dir_ok 394887Schin 404887Schin NoN(opendir) 414887Schin 424887Schin #else 434887Schin 444887Schin static const char id_dir[] = "\n@(#)$Id: directory (AT&T Research) 1993-04-01 $\0\n"; 454887Schin 464887Schin static DIR* freedirp; /* always keep one dirp */ 474887Schin 484887Schin DIR* 494887Schin opendir(register const char* path) 504887Schin { 514887Schin register DIR* dirp = 0; 524887Schin register int fd; 534887Schin struct stat st; 544887Schin 554887Schin if ((fd = open(path, O_RDONLY)) < 0) return(0); 564887Schin if (fstat(fd, &st) < 0 || 574887Schin !S_ISDIR(st.st_mode) && (errno = ENOTDIR) || 584887Schin fcntl(fd, F_SETFD, FD_CLOEXEC) || 594887Schin !(dirp = freedirp ? freedirp : 604887Schin #if defined(_DIR_PRIVATE_) || _ptr_dd_buf 614887Schin newof(0, DIR, 1, DIRBLKSIZ) 624887Schin #else 634887Schin newof(0, DIR, 1, 0) 644887Schin #endif 654887Schin )) 664887Schin { 674887Schin close(fd); 684887Schin if (dirp) 694887Schin { 704887Schin if (!freedirp) freedirp = dirp; 714887Schin else free(dirp); 724887Schin } 734887Schin return(0); 744887Schin } 754887Schin freedirp = 0; 764887Schin dirp->dd_fd = fd; 774887Schin dirp->dd_loc = dirp->dd_size = 0; /* refill needed */ 784887Schin #if defined(_DIR_PRIVATE_) || _ptr_dd_buf 794887Schin dirp->dd_buf = (void*)((char*)dirp + sizeof(DIR)); 804887Schin #endif 814887Schin return(dirp); 824887Schin } 834887Schin 844887Schin void 854887Schin closedir(register DIR* dirp) 864887Schin { 874887Schin if (dirp) 884887Schin { 894887Schin close(dirp->dd_fd); 904887Schin if (!freedirp) freedirp = dirp; 914887Schin else free(dirp); 924887Schin } 934887Schin } 944887Schin 954887Schin #endif 96