EBADFORMAT -> EFTYPE
[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)
269a7923 9static char sccsid[] = "@(#)opendir.c 5.10 (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>
5f4e4c0f 15
d46abbe3 16char *malloc();
d1db0788 17long _rewinddir;
d46abbe3 18
5f4e4c0f
KM
19/*
20 * open a directory.
21 */
22DIR *
23opendir(name)
24 char *name;
25{
79004ddf 26 register DIR *dirp;
5b80a1f6 27 register int fd;
5f4e4c0f 28
5b80a1f6 29 if ((fd = open(name, 0)) == -1)
5f4e4c0f 30 return NULL;
19f34e33
KM
31 if (fcntl(fd, F_SETFD, 1) == -1 ||
32 (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
5b80a1f6 33 close (fd);
79004ddf
KM
34 return NULL;
35 }
d46abbe3
KM
36 /*
37 * If CLSIZE is an exact multiple of DIRBLKSIZ, use a CLSIZE
38 * buffer that it cluster boundary aligned.
39 * Hopefully this can be a big win someday by allowing page trades
40 * to user space to be done by getdirentries()
41 */
42 if ((CLSIZE % DIRBLKSIZ) == 0) {
43 dirp->dd_buf = malloc(CLSIZE);
44 dirp->dd_len = CLSIZE;
45 } else {
46 dirp->dd_buf = malloc(DIRBLKSIZ);
47 dirp->dd_len = DIRBLKSIZ;
48 }
49 if (dirp->dd_buf == NULL) {
50 close (fd);
51 return NULL;
52 }
5b80a1f6 53 dirp->dd_fd = fd;
5f4e4c0f 54 dirp->dd_loc = 0;
d46abbe3 55 dirp->dd_seek = 0;
d1db0788
KM
56 /*
57 * Set up seek point for rewinddir.
58 */
59 _rewinddir = telldir(dirp);
5f4e4c0f
KM
60 return dirp;
61}