xref: /openbsd-src/lib/libc/gen/readdir.c (revision 209e395206c9b2564b5720b7e81f09b0eb38e552)
1*209e3952Sguenther /*	$OpenBSD: readdir.c,v 1.22 2015/09/12 13:34:22 guenther Exp $ */
2df930be7Sderaadt /*
3df930be7Sderaadt  * Copyright (c) 1983, 1993
4df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
5df930be7Sderaadt  *
6df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
7df930be7Sderaadt  * modification, are permitted provided that the following conditions
8df930be7Sderaadt  * are met:
9df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
10df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
11df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
13df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
146580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
15df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
16df930be7Sderaadt  *    without specific prior written permission.
17df930be7Sderaadt  *
18df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df930be7Sderaadt  * SUCH DAMAGE.
29df930be7Sderaadt  */
30df930be7Sderaadt 
31df930be7Sderaadt #include <dirent.h>
326ca253daSguenther #include <errno.h>
33e55576caSmatthew #include "telldir.h"
3492efb735Sd #include "thread_private.h"
35df930be7Sderaadt 
36df930be7Sderaadt /*
37df930be7Sderaadt  * get next entry in a directory.
38df930be7Sderaadt  */
39b1daa819Sokan int
_readdir_unlocked(DIR * dirp,struct dirent ** result)401c8956b9Sschwarze _readdir_unlocked(DIR *dirp, struct dirent **result)
41df930be7Sderaadt {
4280647cb2Sotto 	struct dirent *dp;
43df930be7Sderaadt 
44b1daa819Sokan 	*result = NULL;
45df930be7Sderaadt 	for (;;) {
4606373755Sschwarze 		if (dirp->dd_loc >= dirp->dd_size) {
47df930be7Sderaadt 			dirp->dd_loc = 0;
480bc54f26Sguenther 			dirp->dd_size = getdents(dirp->dd_fd, dirp->dd_buf,
490bc54f26Sguenther 			    dirp->dd_len);
50b1daa819Sokan 			if (dirp->dd_size == 0)
51b1daa819Sokan 				return (0);
52b1daa819Sokan 			if (dirp->dd_size < 0)
53b1daa819Sokan 				return (-1);
54df930be7Sderaadt 		}
55df930be7Sderaadt 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
566ca253daSguenther 		if ((long)dp & 03 ||	/* bogus pointer check */
576ca253daSguenther 		    dp->d_reclen <= 0 ||
586ca253daSguenther 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) {
596ca253daSguenther 			errno = EINVAL;
60b1daa819Sokan 			return (-1);
616ca253daSguenther 		}
62df930be7Sderaadt 		dirp->dd_loc += dp->d_reclen;
631c8956b9Sschwarze 		if (dp->d_ino == 0)
64df930be7Sderaadt 			continue;
6591a535ffSguenther 		dirp->dd_curpos = dp->d_off;
66b1daa819Sokan 		*result = dp;
67b1daa819Sokan 		return (0);
68df930be7Sderaadt 	}
69df930be7Sderaadt }
7092efb735Sd 
7145e14c38Skurt struct dirent *
readdir(DIR * dirp)7245e14c38Skurt readdir(DIR *dirp)
7345e14c38Skurt {
7445e14c38Skurt 	struct dirent *dp;
7545e14c38Skurt 
7645e14c38Skurt 	_MUTEX_LOCK(&dirp->dd_lock);
771c8956b9Sschwarze 	_readdir_unlocked(dirp, &dp);
7845e14c38Skurt 	_MUTEX_UNLOCK(&dirp->dd_lock);
7945e14c38Skurt 
8045e14c38Skurt 	return (dp);
8145e14c38Skurt }
82*209e3952Sguenther DEF_WEAK(readdir);
83