Home | History | Annotate | Line # | Download | only in gen
readdir.c revision 1.2.2.2
      1  1.2.2.2  jtc /*
      2  1.2.2.2  jtc  * Copyright (c) 1983, 1993
      3  1.2.2.2  jtc  *	The Regents of the University of California.  All rights reserved.
      4  1.2.2.2  jtc  *
      5  1.2.2.2  jtc  * Redistribution and use in source and binary forms, with or without
      6  1.2.2.2  jtc  * modification, are permitted provided that the following conditions
      7  1.2.2.2  jtc  * are met:
      8  1.2.2.2  jtc  * 1. Redistributions of source code must retain the above copyright
      9  1.2.2.2  jtc  *    notice, this list of conditions and the following disclaimer.
     10  1.2.2.2  jtc  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.2.2.2  jtc  *    notice, this list of conditions and the following disclaimer in the
     12  1.2.2.2  jtc  *    documentation and/or other materials provided with the distribution.
     13  1.2.2.2  jtc  * 3. All advertising materials mentioning features or use of this software
     14  1.2.2.2  jtc  *    must display the following acknowledgement:
     15  1.2.2.2  jtc  *	This product includes software developed by the University of
     16  1.2.2.2  jtc  *	California, Berkeley and its contributors.
     17  1.2.2.2  jtc  * 4. Neither the name of the University nor the names of its contributors
     18  1.2.2.2  jtc  *    may be used to endorse or promote products derived from this software
     19  1.2.2.2  jtc  *    without specific prior written permission.
     20  1.2.2.2  jtc  *
     21  1.2.2.2  jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.2.2.2  jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.2.2.2  jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.2.2.2  jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.2.2.2  jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.2.2.2  jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.2.2.2  jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.2.2.2  jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.2.2.2  jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.2.2.2  jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.2.2.2  jtc  * SUCH DAMAGE.
     32  1.2.2.2  jtc  */
     33  1.2.2.2  jtc 
     34  1.2.2.2  jtc #if defined(LIBC_SCCS) && !defined(lint)
     35  1.2.2.2  jtc /*static char sccsid[] = "from: @(#)readdir.c	8.1 (Berkeley) 6/4/93";*/
     36  1.2.2.2  jtc static char rcsid[] = "$Id: readdir.c,v 1.2.2.2 1994/07/27 14:39:49 jtc Exp $";
     37  1.2.2.2  jtc #endif /* LIBC_SCCS and not lint */
     38  1.2.2.2  jtc 
     39  1.2.2.2  jtc #include <sys/param.h>
     40  1.2.2.2  jtc #include <dirent.h>
     41  1.2.2.2  jtc 
     42  1.2.2.2  jtc /*
     43  1.2.2.2  jtc  * get next entry in a directory.
     44  1.2.2.2  jtc  */
     45  1.2.2.2  jtc struct dirent *
     46  1.2.2.2  jtc readdir(dirp)
     47  1.2.2.2  jtc 	register DIR *dirp;
     48  1.2.2.2  jtc {
     49  1.2.2.2  jtc 	register struct dirent *dp;
     50  1.2.2.2  jtc 
     51  1.2.2.2  jtc 	for (;;) {
     52  1.2.2.2  jtc 		if (dirp->dd_loc == 0) {
     53  1.2.2.2  jtc 			dirp->dd_size = getdirentries(dirp->dd_fd,
     54  1.2.2.2  jtc 			    dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
     55  1.2.2.2  jtc 			if (dirp->dd_size <= 0)
     56  1.2.2.2  jtc 				return NULL;
     57  1.2.2.2  jtc 		}
     58  1.2.2.2  jtc 		if (dirp->dd_loc >= dirp->dd_size) {
     59  1.2.2.2  jtc 			dirp->dd_loc = 0;
     60  1.2.2.2  jtc 			continue;
     61  1.2.2.2  jtc 		}
     62  1.2.2.2  jtc 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
     63  1.2.2.2  jtc 		if ((int)dp & 03)	/* bogus pointer check */
     64  1.2.2.2  jtc 			return NULL;
     65  1.2.2.2  jtc 		if (dp->d_reclen <= 0 ||
     66  1.2.2.2  jtc 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
     67  1.2.2.2  jtc 			return NULL;
     68  1.2.2.2  jtc 		dirp->dd_loc += dp->d_reclen;
     69  1.2.2.2  jtc 		if (dp->d_ino == 0)
     70  1.2.2.2  jtc 			continue;
     71  1.2.2.2  jtc 		return (dp);
     72  1.2.2.2  jtc 	}
     73  1.2.2.2  jtc }
     74