xref: /openbsd-src/lib/libc/gen/readdir_r.c (revision f54f769fe431f6590c22d8795b002c3ea2e2d668)
1*f54f769fSderaadt /*	$OpenBSD: readdir_r.c,v 1.6 2013/11/12 20:19:23 deraadt Exp $ */
223599751Sguenther /*
323599751Sguenther  * Copyright (c) 1983, 1993
423599751Sguenther  *	The Regents of the University of California.  All rights reserved.
523599751Sguenther  *
623599751Sguenther  * Redistribution and use in source and binary forms, with or without
723599751Sguenther  * modification, are permitted provided that the following conditions
823599751Sguenther  * are met:
923599751Sguenther  * 1. Redistributions of source code must retain the above copyright
1023599751Sguenther  *    notice, this list of conditions and the following disclaimer.
1123599751Sguenther  * 2. Redistributions in binary form must reproduce the above copyright
1223599751Sguenther  *    notice, this list of conditions and the following disclaimer in the
1323599751Sguenther  *    documentation and/or other materials provided with the distribution.
1423599751Sguenther  * 3. Neither the name of the University nor the names of its contributors
1523599751Sguenther  *    may be used to endorse or promote products derived from this software
1623599751Sguenther  *    without specific prior written permission.
1723599751Sguenther  *
1823599751Sguenther  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1923599751Sguenther  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2023599751Sguenther  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2123599751Sguenther  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2223599751Sguenther  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2323599751Sguenther  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2423599751Sguenther  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2523599751Sguenther  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2623599751Sguenther  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2723599751Sguenther  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2823599751Sguenther  * SUCH DAMAGE.
2923599751Sguenther  */
3023599751Sguenther 
3123599751Sguenther #include <dirent.h>
3223599751Sguenther #include <errno.h>
3369245ebdSmillert #include <limits.h>
3469245ebdSmillert #include <string.h>
35e55576caSmatthew #include "telldir.h"
3623599751Sguenther #include "thread_private.h"
3723599751Sguenther 
3823599751Sguenther int
readdir_r(DIR * dirp,struct dirent * entry,struct dirent ** result)3923599751Sguenther readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
4023599751Sguenther {
4123599751Sguenther 	struct dirent *dp;
4223599751Sguenther 
4323599751Sguenther 	_MUTEX_LOCK(&dirp->dd_lock);
441c8956b9Sschwarze 	if (_readdir_unlocked(dirp, &dp) != 0) {
4523599751Sguenther 		_MUTEX_UNLOCK(&dirp->dd_lock);
4623599751Sguenther 		return errno;
4723599751Sguenther 	}
4823599751Sguenther 	if (dp != NULL)
4923599751Sguenther 		memcpy(entry, dp,
5069245ebdSmillert 		    sizeof (struct dirent) - NAME_MAX + dp->d_namlen);
5123599751Sguenther 	_MUTEX_UNLOCK(&dirp->dd_lock);
5223599751Sguenther 	if (dp != NULL)
5323599751Sguenther 		*result = entry;
5423599751Sguenther 	else
5523599751Sguenther 		*result = NULL;
5623599751Sguenther 	return 0;
5723599751Sguenther }
58