Commit | Line | Data |
---|---|---|
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) |
c5980113 | 9 | static char sccsid[] = "@(#)opendir.c 5.11 (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 | 18 | long _rewinddir; |
d46abbe3 | 19 | |
5f4e4c0f KM |
20 | /* |
21 | * open a directory. | |
22 | */ | |
23 | DIR * | |
24 | opendir(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; |
19f34e33 KM |
32 | if (fcntl(fd, F_SETFD, 1) == -1 || |
33 | (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { | |
5b80a1f6 | 34 | close (fd); |
79004ddf KM |
35 | return NULL; |
36 | } | |
d46abbe3 KM |
37 | /* |
38 | * If CLSIZE is an exact multiple of DIRBLKSIZ, use a CLSIZE | |
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 | */ | |
43 | if ((CLSIZE % DIRBLKSIZ) == 0) { | |
44 | dirp->dd_buf = malloc(CLSIZE); | |
45 | dirp->dd_len = CLSIZE; | |
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 | } |