121350Sdist /* 2*62736Sbostic * Copyright (c) 1983, 1993 3*62736Sbostic * The Regents of the University of California. All rights reserved. 434790Sbostic * 542625Sbostic * %sccs.include.redist.c% 621350Sdist */ 721350Sdist 826573Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*62736Sbostic static char sccsid[] = "@(#)opendir.c 8.1 (Berkeley) 06/08/93"; 1034790Sbostic #endif /* LIBC_SCCS and not lint */ 115750Smckusick 129142Smckusick #include <sys/param.h> 1336545Smckusick #include <dirent.h> 1438664Smckusick #include <fcntl.h> 1546597Sdonn #include <stdlib.h> 1646597Sdonn #include <unistd.h> 175750Smckusick 185750Smckusick /* 195750Smckusick * open a directory. 205750Smckusick */ 215750Smckusick DIR * 225750Smckusick opendir(name) 2346597Sdonn const char *name; 245750Smckusick { 256097Smckusic register DIR *dirp; 267659Smckusick register int fd; 275750Smckusick 287659Smckusick if ((fd = open(name, 0)) == -1) 295750Smckusick return NULL; 3057854Sralph if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || 3138664Smckusick (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { 327659Smckusick close (fd); 336097Smckusic return NULL; 346097Smckusic } 3538435Smckusick /* 3652940Sralph * If CLBYTES is an exact multiple of DIRBLKSIZ, use a CLBYTES 3738435Smckusick * buffer that it cluster boundary aligned. 3838435Smckusick * Hopefully this can be a big win someday by allowing page trades 3938435Smckusick * to user space to be done by getdirentries() 4038435Smckusick */ 4152940Sralph if ((CLBYTES % DIRBLKSIZ) == 0) { 4252940Sralph dirp->dd_buf = malloc(CLBYTES); 4352940Sralph dirp->dd_len = CLBYTES; 4438435Smckusick } else { 4538435Smckusick dirp->dd_buf = malloc(DIRBLKSIZ); 4638435Smckusick dirp->dd_len = DIRBLKSIZ; 4738435Smckusick } 4838435Smckusick if (dirp->dd_buf == NULL) { 4938435Smckusick close (fd); 5038435Smckusick return NULL; 5138435Smckusick } 527659Smckusick dirp->dd_fd = fd; 535750Smckusick dirp->dd_loc = 0; 5438435Smckusick dirp->dd_seek = 0; 5539957Smckusick /* 5639957Smckusick * Set up seek point for rewinddir. 5739957Smckusick */ 5862720Smckusick dirp->dd_rewind = telldir(dirp); 595750Smckusick return dirp; 605750Smckusick } 61