xref: /csrg-svn/lib/libc/gen/opendir.c (revision 38435)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)opendir.c	5.5 (Berkeley) 07/10/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <sys/param.h>
23 #include <dirent.h>
24 
25 char *malloc();
26 
27 /*
28  * open a directory.
29  */
30 DIR *
31 opendir(name)
32 	char *name;
33 {
34 	register DIR *dirp;
35 	register int fd;
36 	register int i;
37 
38 	if ((fd = open(name, 0)) == -1)
39 		return NULL;
40 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
41 		close (fd);
42 		return NULL;
43 	}
44 	/*
45 	 * If CLSIZE is an exact multiple of DIRBLKSIZ, use a CLSIZE
46 	 * buffer that it cluster boundary aligned.
47 	 * Hopefully this can be a big win someday by allowing page trades
48 	 * to user space to be done by getdirentries()
49 	 */
50 	if ((CLSIZE % DIRBLKSIZ) == 0) {
51 		dirp->dd_buf = malloc(CLSIZE);
52 		dirp->dd_len = CLSIZE;
53 	} else {
54 		dirp->dd_buf = malloc(DIRBLKSIZ);
55 		dirp->dd_len = DIRBLKSIZ;
56 	}
57 	if (dirp->dd_buf == NULL) {
58 		close (fd);
59 		return NULL;
60 	}
61 	dirp->dd_fd = fd;
62 	dirp->dd_loc = 0;
63 	dirp->dd_seek = 0;
64 	dirp->dd_loccnt = 1;
65 	for (i = 0; i < NDIRHASH; i++)
66 		dirp->dd_hash[i] = NULL;
67 	return dirp;
68 }
69