xref: /original-bsd/lib/libc/gen/readdir.c (revision 7a8f01dc)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)readdir.c	5.7 (Berkeley) 06/01/90";
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 == 0) {
26 			dirp->dd_size = getdirentries(dirp->dd_fd,
27 			    dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
28 			if (dirp->dd_size <= 0)
29 				return NULL;
30 		}
31 		if (dirp->dd_loc >= dirp->dd_size) {
32 			dirp->dd_loc = 0;
33 			continue;
34 		}
35 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
36 		if ((int)dp & 03)	/* bogus pointer check */
37 			return NULL;
38 		if (dp->d_reclen <= 0 ||
39 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
40 			return NULL;
41 		dirp->dd_loc += dp->d_reclen;
42 		if (dp->d_ino == 0)
43 			continue;
44 		return (dp);
45 	}
46 }
47