4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / lib / libc / gen / opendir.c
CommitLineData
bb0cfa24
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
d99e6414
KB
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
bb0cfa24
DF
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
74155b62 9static char sccsid[] = "@(#)opendir.c 8.1 (Berkeley) %G%";
d99e6414 10#endif /* LIBC_SCCS and not lint */
5f4e4c0f 11
13d423cf 12#include <sys/param.h>
8c6e64bc 13#include <dirent.h>
19f34e33 14#include <fcntl.h>
c5980113
DS
15#include <stdlib.h>
16#include <unistd.h>
5f4e4c0f 17
d1db0788 18long _rewinddir;
d46abbe3 19
5f4e4c0f
KM
20/*
21 * open a directory.
22 */
23DIR *
24opendir(name)
c5980113 25 const char *name;
5f4e4c0f 26{
79004ddf 27 register DIR *dirp;
5b80a1f6 28 register int fd;
5f4e4c0f 29
5b80a1f6 30 if ((fd = open(name, 0)) == -1)
5f4e4c0f 31 return NULL;
c995d4da 32 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
19f34e33 33 (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
5b80a1f6 34 close (fd);
79004ddf
KM
35 return NULL;
36 }
d46abbe3 37 /*
1bc8531a 38 * If CLBYTES is an exact multiple of DIRBLKSIZ, use a CLBYTES
d46abbe3
KM
39 * buffer that it cluster boundary aligned.
40 * Hopefully this can be a big win someday by allowing page trades
41 * to user space to be done by getdirentries()
42 */
1bc8531a
RC
43 if ((CLBYTES % DIRBLKSIZ) == 0) {
44 dirp->dd_buf = malloc(CLBYTES);
45 dirp->dd_len = CLBYTES;
d46abbe3
KM
46 } else {
47 dirp->dd_buf = malloc(DIRBLKSIZ);
48 dirp->dd_len = DIRBLKSIZ;
49 }
50 if (dirp->dd_buf == NULL) {
51 close (fd);
52 return NULL;
53 }
5b80a1f6 54 dirp->dd_fd = fd;
5f4e4c0f 55 dirp->dd_loc = 0;
d46abbe3 56 dirp->dd_seek = 0;
d1db0788
KM
57 /*
58 * Set up seek point for rewinddir.
59 */
60 _rewinddir = telldir(dirp);
5f4e4c0f
KM
61 return dirp;
62}