1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)opendir.c 8.1 (Berkeley) 06/08/93"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/param.h> 13 #include <dirent.h> 14 #include <fcntl.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 18 /* 19 * open a directory. 20 */ 21 DIR * 22 opendir(name) 23 const char *name; 24 { 25 register DIR *dirp; 26 register int fd; 27 28 if ((fd = open(name, 0)) == -1) 29 return NULL; 30 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || 31 (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { 32 close (fd); 33 return NULL; 34 } 35 /* 36 * If CLBYTES is an exact multiple of DIRBLKSIZ, use a CLBYTES 37 * buffer that it cluster boundary aligned. 38 * Hopefully this can be a big win someday by allowing page trades 39 * to user space to be done by getdirentries() 40 */ 41 if ((CLBYTES % DIRBLKSIZ) == 0) { 42 dirp->dd_buf = malloc(CLBYTES); 43 dirp->dd_len = CLBYTES; 44 } else { 45 dirp->dd_buf = malloc(DIRBLKSIZ); 46 dirp->dd_len = DIRBLKSIZ; 47 } 48 if (dirp->dd_buf == NULL) { 49 close (fd); 50 return NULL; 51 } 52 dirp->dd_fd = fd; 53 dirp->dd_loc = 0; 54 dirp->dd_seek = 0; 55 /* 56 * Set up seek point for rewinddir. 57 */ 58 dirp->dd_rewind = telldir(dirp); 59 return dirp; 60 } 61