xref: /csrg-svn/lib/libc/gen/readdir.c (revision 26579)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)readdir.c	5.2 (Berkeley) 03/09/86";
9 #endif LIBC_SCCS and not lint
10 
11 #include <sys/param.h>
12 #include <sys/dir.h>
13 
14 /*
15  * get next entry in a directory.
16  */
17 struct direct *
18 readdir(dirp)
19 	register DIR *dirp;
20 {
21 	register struct direct *dp;
22 
23 	for (;;) {
24 		if (dirp->dd_loc == 0) {
25 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
26 			    DIRBLKSIZ);
27 			if (dirp->dd_size <= 0)
28 				return NULL;
29 		}
30 		if (dirp->dd_loc >= dirp->dd_size) {
31 			dirp->dd_loc = 0;
32 			continue;
33 		}
34 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
35 		if (dp->d_reclen <= 0 ||
36 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc)
37 			return NULL;
38 		dirp->dd_loc += dp->d_reclen;
39 		if (dp->d_ino == 0)
40 			continue;
41 		return (dp);
42 	}
43 }
44