xref: /csrg-svn/lib/libc/gen/readdir.c (revision 38435)
121355Sdist /*
221355Sdist  * 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.
1621355Sdist  */
1721355Sdist 
1826579Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*38435Smckusick static char sccsid[] = "@(#)readdir.c	5.6 (Berkeley) 07/10/89";
2034790Sbostic #endif /* LIBC_SCCS and not lint */
215751Smckusick 
226370Smckusic #include <sys/param.h>
2336545Smckusick #include <dirent.h>
245751Smckusick 
255751Smckusick /*
265751Smckusick  * get next entry in a directory.
275751Smckusick  */
2836545Smckusick struct dirent *
295751Smckusick readdir(dirp)
305751Smckusick 	register DIR *dirp;
315751Smckusick {
3236545Smckusick 	register struct dirent *dp;
335751Smckusick 
345751Smckusick 	for (;;) {
355751Smckusick 		if (dirp->dd_loc == 0) {
36*38435Smckusick 			dirp->dd_size = getdirentries(dirp->dd_fd,
37*38435Smckusick 			    dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
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 		}
4536545Smckusick 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
4634756Sbostic 		if ((int)dp & 03)	/* bogus pointer check */
4734756Sbostic 			return NULL;
486712Smckusick 		if (dp->d_reclen <= 0 ||
49*38435Smckusick 		    dp->d_reclen > dirp->dd_len + 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