xref: /csrg-svn/lib/libc/gen/readdir.c (revision 67783)
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*67783Spendry static char sccsid[] = "@(#)readdir.c	8.3 (Berkeley) 09/29/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 *
readdir(dirp)195751Smckusick readdir(dirp)
205751Smckusick 	register DIR *dirp;
215751Smckusick {
2236545Smckusick 	register struct dirent *dp;
235751Smckusick 
245751Smckusick 	for (;;) {
2567575Spendry 		if (dirp->dd_loc >= dirp->dd_size) {
2667575Spendry 			if (dirp->dd_flags & __DTF_READALL)
2767575Spendry 				return (NULL);
2867575Spendry 			dirp->dd_loc = 0;
2967575Spendry 		}
3067575Spendry 		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)
3467575Spendry 				return (NULL);
355751Smckusick 		}
3636545Smckusick 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
3734756Sbostic 		if ((int)dp & 03)	/* bogus pointer check */
3867575Spendry 			return (NULL);
396712Smckusick 		if (dp->d_reclen <= 0 ||
4038435Smckusick 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
4167575Spendry 			return (NULL);
426712Smckusick 		dirp->dd_loc += dp->d_reclen;
435766Smckusic 		if (dp->d_ino == 0)
445766Smckusic 			continue;
45*67783Spendry 		if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW))
46*67783Spendry 			continue;
476712Smckusick 		return (dp);
485751Smckusick 	}
495751Smckusick }
50