xref: /csrg-svn/lib/libc/gen/readdir.c (revision 34790)
121355Sdist /*
221355Sdist  * Copyright (c) 1983 Regents of the University of California.
3*34790Sbostic  * All rights reserved.
4*34790Sbostic  *
5*34790Sbostic  * Redistribution and use in source and binary forms are permitted
6*34790Sbostic  * provided that the above copyright notice and this paragraph are
7*34790Sbostic  * duplicated in all such forms and that any documentation,
8*34790Sbostic  * advertising materials, and other materials related to such
9*34790Sbostic  * distribution and use acknowledge that the software was developed
10*34790Sbostic  * by the University of California, Berkeley.  The name of the
11*34790Sbostic  * University may not be used to endorse or promote products derived
12*34790Sbostic  * from this software without specific prior written permission.
13*34790Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34790Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34790Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621355Sdist  */
1721355Sdist 
1826579Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34790Sbostic static char sccsid[] = "@(#)readdir.c	5.4 (Berkeley) 06/18/88";
20*34790Sbostic #endif /* LIBC_SCCS and not lint */
215751Smckusick 
226370Smckusic #include <sys/param.h>
2313582Ssam #include <sys/dir.h>
245751Smckusick 
255751Smckusick /*
265751Smckusick  * get next entry in a directory.
275751Smckusick  */
285751Smckusick struct direct *
295751Smckusick readdir(dirp)
305751Smckusick 	register DIR *dirp;
315751Smckusick {
326712Smckusick 	register struct direct *dp;
335751Smckusick 
345751Smckusick 	for (;;) {
355751Smckusick 		if (dirp->dd_loc == 0) {
365756Smckusic 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
375872Smckusic 			    DIRBLKSIZ);
385751Smckusick 			if (dirp->dd_size <= 0)
395751Smckusick 				return NULL;
405751Smckusick 		}
415751Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
425751Smckusick 			dirp->dd_loc = 0;
435751Smckusick 			continue;
445751Smckusick 		}
456712Smckusick 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
4634756Sbostic 		if ((int)dp & 03)	/* bogus pointer check */
4734756Sbostic 			return NULL;
486712Smckusick 		if (dp->d_reclen <= 0 ||
496712Smckusick 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc)
506712Smckusick 			return NULL;
516712Smckusick 		dirp->dd_loc += dp->d_reclen;
525766Smckusic 		if (dp->d_ino == 0)
535766Smckusic 			continue;
546712Smckusick 		return (dp);
555751Smckusick 	}
565751Smckusick }
57