xref: /csrg-svn/lib/libc/gen/opendir.c (revision 39957)
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*39957Smckusick static char sccsid[] = "@(#)opendir.c	5.8 (Berkeley) 01/30/90";
2034790Sbostic #endif /* LIBC_SCCS and not lint */
215750Smckusick 
229142Smckusick #include <sys/param.h>
2336545Smckusick #include <dirent.h>
2438664Smckusick #include <fcntl.h>
255750Smckusick 
2638435Smckusick char *malloc();
27*39957Smckusick long _rewinddir;
2838435Smckusick 
295750Smckusick /*
305750Smckusick  * open a directory.
315750Smckusick  */
325750Smckusick DIR *
335750Smckusick opendir(name)
345750Smckusick 	char *name;
355750Smckusick {
366097Smckusic 	register DIR *dirp;
377659Smckusick 	register int fd;
3838435Smckusick 	register int i;
395750Smckusick 
407659Smckusick 	if ((fd = open(name, 0)) == -1)
415750Smckusick 		return NULL;
4238664Smckusick 	if (fcntl(fd, F_SETFD, 1) == -1 ||
4338664Smckusick 	    (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
447659Smckusick 		close (fd);
456097Smckusic 		return NULL;
466097Smckusic 	}
4738435Smckusick 	/*
4838435Smckusick 	 * If CLSIZE is an exact multiple of DIRBLKSIZ, use a CLSIZE
4938435Smckusick 	 * buffer that it cluster boundary aligned.
5038435Smckusick 	 * Hopefully this can be a big win someday by allowing page trades
5138435Smckusick 	 * to user space to be done by getdirentries()
5238435Smckusick 	 */
5338435Smckusick 	if ((CLSIZE % DIRBLKSIZ) == 0) {
5438435Smckusick 		dirp->dd_buf = malloc(CLSIZE);
5538435Smckusick 		dirp->dd_len = CLSIZE;
5638435Smckusick 	} else {
5738435Smckusick 		dirp->dd_buf = malloc(DIRBLKSIZ);
5838435Smckusick 		dirp->dd_len = DIRBLKSIZ;
5938435Smckusick 	}
6038435Smckusick 	if (dirp->dd_buf == NULL) {
6138435Smckusick 		close (fd);
6238435Smckusick 		return NULL;
6338435Smckusick 	}
647659Smckusick 	dirp->dd_fd = fd;
655750Smckusick 	dirp->dd_loc = 0;
6638435Smckusick 	dirp->dd_seek = 0;
67*39957Smckusick 	/*
68*39957Smckusick 	 * Set up seek point for rewinddir.
69*39957Smckusick 	 */
70*39957Smckusick 	_rewinddir = telldir(dirp);
715750Smckusick 	return dirp;
725750Smckusick }
73