xref: /csrg-svn/lib/libc/gen/readdir.c (revision 42625)
121355Sdist /*
221355Sdist  * Copyright (c) 1983 Regents of the University of California.
334790Sbostic  * All rights reserved.
434790Sbostic  *
5*42625Sbostic  * %sccs.include.redist.c%
621355Sdist  */
721355Sdist 
826579Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42625Sbostic static char sccsid[] = "@(#)readdir.c	5.7 (Berkeley) 06/01/90";
1034790Sbostic #endif /* LIBC_SCCS and not lint */
115751Smckusick 
126370Smckusic #include <sys/param.h>
1336545Smckusick #include <dirent.h>
145751Smckusick 
155751Smckusick /*
165751Smckusick  * get next entry in a directory.
175751Smckusick  */
1836545Smckusick struct dirent *
195751Smckusick readdir(dirp)
205751Smckusick 	register DIR *dirp;
215751Smckusick {
2236545Smckusick 	register struct dirent *dp;
235751Smckusick 
245751Smckusick 	for (;;) {
255751Smckusick 		if (dirp->dd_loc == 0) {
2638435Smckusick 			dirp->dd_size = getdirentries(dirp->dd_fd,
2738435Smckusick 			    dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
285751Smckusick 			if (dirp->dd_size <= 0)
295751Smckusick 				return NULL;
305751Smckusick 		}
315751Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
325751Smckusick 			dirp->dd_loc = 0;
335751Smckusick 			continue;
345751Smckusick 		}
3536545Smckusick 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
3634756Sbostic 		if ((int)dp & 03)	/* bogus pointer check */
3734756Sbostic 			return NULL;
386712Smckusick 		if (dp->d_reclen <= 0 ||
3938435Smckusick 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
406712Smckusick 			return NULL;
416712Smckusick 		dirp->dd_loc += dp->d_reclen;
425766Smckusic 		if (dp->d_ino == 0)
435766Smckusic 			continue;
446712Smckusick 		return (dp);
455751Smckusick 	}
465751Smckusick }
47