xref: /original-bsd/lib/libc/gen/readdir.c (revision 27393bdf)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)readdir.c	8.3 (Berkeley) 09/29/94";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <dirent.h>
14 
15 /*
16  * get next entry in a directory.
17  */
18 struct dirent *
19 readdir(dirp)
20 	register DIR *dirp;
21 {
22 	register struct dirent *dp;
23 
24 	for (;;) {
25 		if (dirp->dd_loc >= dirp->dd_size) {
26 			if (dirp->dd_flags & __DTF_READALL)
27 				return (NULL);
28 			dirp->dd_loc = 0;
29 		}
30 		if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) {
31 			dirp->dd_size = getdirentries(dirp->dd_fd,
32 			    dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
33 			if (dirp->dd_size <= 0)
34 				return (NULL);
35 		}
36 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
37 		if ((int)dp & 03)	/* bogus pointer check */
38 			return (NULL);
39 		if (dp->d_reclen <= 0 ||
40 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
41 			return (NULL);
42 		dirp->dd_loc += dp->d_reclen;
43 		if (dp->d_ino == 0)
44 			continue;
45 		if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW))
46 			continue;
47 		return (dp);
48 	}
49 }
50