xref: /csrg-svn/lib/libc/gen/readdir.c (revision 67575)
121355Sdist /*
261111Sbostic  * Copyright (c) 1983, 1993
361111Sbostic  *	The Regents of the University of California.  All rights reserved.
434790Sbostic  *
542625Sbostic  * %sccs.include.redist.c%
621355Sdist  */
721355Sdist 
826579Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*67575Spendry static char sccsid[] = "@(#)readdir.c	8.2 (Berkeley) 07/28/94";
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 (;;) {
25*67575Spendry 		if (dirp->dd_loc >= dirp->dd_size) {
26*67575Spendry 			if (dirp->dd_flags & __DTF_READALL)
27*67575Spendry 				return (NULL);
28*67575Spendry 			dirp->dd_loc = 0;
29*67575Spendry 		}
30*67575Spendry 		if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) {
3138435Smckusick 			dirp->dd_size = getdirentries(dirp->dd_fd,
3238435Smckusick 			    dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
335751Smckusick 			if (dirp->dd_size <= 0)
34*67575Spendry 				return (NULL);
355751Smckusick 		}
3636545Smckusick 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
3734756Sbostic 		if ((int)dp & 03)	/* bogus pointer check */
38*67575Spendry 			return (NULL);
396712Smckusick 		if (dp->d_reclen <= 0 ||
4038435Smckusick 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
41*67575Spendry 			return (NULL);
426712Smckusick 		dirp->dd_loc += dp->d_reclen;
435766Smckusic 		if (dp->d_ino == 0)
445766Smckusic 			continue;
456712Smckusick 		return (dp);
465751Smckusick 	}
475751Smckusick }
48