xref: /original-bsd/lib/libc/gen/readdir.c (revision 7a4e9f34)
1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)readdir.c 4.4 05/07/82";
4 
5 #include <sys/param.h>
6 #include <dir.h>
7 
8 /*
9  * get next entry in a directory.
10  */
11 struct direct *
12 readdir(dirp)
13 	register DIR *dirp;
14 {
15 	register struct direct *dp;
16 
17 	for (;;) {
18 		if (dirp->dd_loc == 0) {
19 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
20 			    DIRBLKSIZ);
21 			if (dirp->dd_size <= 0)
22 				return NULL;
23 		}
24 		if (dirp->dd_loc >= dirp->dd_size) {
25 			dirp->dd_loc = 0;
26 			continue;
27 		}
28 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
29 		if (dp->d_reclen <= 0 ||
30 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc)
31 			return NULL;
32 		dirp->dd_loc += dp->d_reclen;
33 		if (dp->d_ino == 0)
34 			continue;
35 		return (dp);
36 	}
37 }
38