xref: /csrg-svn/lib/libc/gen/opendir.c (revision 38435)
121350Sdist /*
221350Sdist  * Copyright (c) 1983 Regents of the University of California.
334790Sbostic  * All rights reserved.
434790Sbostic  *
534790Sbostic  * Redistribution and use in source and binary forms are permitted
634790Sbostic  * provided that the above copyright notice and this paragraph are
734790Sbostic  * duplicated in all such forms and that any documentation,
834790Sbostic  * advertising materials, and other materials related to such
934790Sbostic  * distribution and use acknowledge that the software was developed
1034790Sbostic  * by the University of California, Berkeley.  The name of the
1134790Sbostic  * University may not be used to endorse or promote products derived
1234790Sbostic  * from this software without specific prior written permission.
1334790Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434790Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534790Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621350Sdist  */
1721350Sdist 
1826573Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*38435Smckusick static char sccsid[] = "@(#)opendir.c	5.5 (Berkeley) 07/10/89";
2034790Sbostic #endif /* LIBC_SCCS and not lint */
215750Smckusick 
229142Smckusick #include <sys/param.h>
2336545Smckusick #include <dirent.h>
245750Smckusick 
25*38435Smckusick char *malloc();
26*38435Smckusick 
275750Smckusick /*
285750Smckusick  * open a directory.
295750Smckusick  */
305750Smckusick DIR *
315750Smckusick opendir(name)
325750Smckusick 	char *name;
335750Smckusick {
346097Smckusic 	register DIR *dirp;
357659Smckusick 	register int fd;
36*38435Smckusick 	register int i;
375750Smckusick 
387659Smckusick 	if ((fd = open(name, 0)) == -1)
395750Smckusick 		return NULL;
409142Smckusick 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
417659Smckusick 		close (fd);
426097Smckusic 		return NULL;
436097Smckusic 	}
44*38435Smckusick 	/*
45*38435Smckusick 	 * If CLSIZE is an exact multiple of DIRBLKSIZ, use a CLSIZE
46*38435Smckusick 	 * buffer that it cluster boundary aligned.
47*38435Smckusick 	 * Hopefully this can be a big win someday by allowing page trades
48*38435Smckusick 	 * to user space to be done by getdirentries()
49*38435Smckusick 	 */
50*38435Smckusick 	if ((CLSIZE % DIRBLKSIZ) == 0) {
51*38435Smckusick 		dirp->dd_buf = malloc(CLSIZE);
52*38435Smckusick 		dirp->dd_len = CLSIZE;
53*38435Smckusick 	} else {
54*38435Smckusick 		dirp->dd_buf = malloc(DIRBLKSIZ);
55*38435Smckusick 		dirp->dd_len = DIRBLKSIZ;
56*38435Smckusick 	}
57*38435Smckusick 	if (dirp->dd_buf == NULL) {
58*38435Smckusick 		close (fd);
59*38435Smckusick 		return NULL;
60*38435Smckusick 	}
617659Smckusick 	dirp->dd_fd = fd;
625750Smckusick 	dirp->dd_loc = 0;
63*38435Smckusick 	dirp->dd_seek = 0;
64*38435Smckusick 	dirp->dd_loccnt = 1;
65*38435Smckusick 	for (i = 0; i < NDIRHASH; i++)
66*38435Smckusick 		dirp->dd_hash[i] = NULL;
675750Smckusick 	return dirp;
685750Smckusick }
69