Home | History | Annotate | Line # | Download | only in gen
telldir.c revision 1.19.8.2
      1  1.19.8.2  tonnerre /*	$NetBSD: telldir.c,v 1.19.8.2 2008/05/04 18:53:27 tonnerre Exp $	*/
      2  1.19.8.2  tonnerre 
      3  1.19.8.2  tonnerre /*
      4  1.19.8.2  tonnerre  * Copyright (c) 1983, 1993
      5  1.19.8.2  tonnerre  *	The Regents of the University of California.  All rights reserved.
      6  1.19.8.2  tonnerre  *
      7  1.19.8.2  tonnerre  * Redistribution and use in source and binary forms, with or without
      8  1.19.8.2  tonnerre  * modification, are permitted provided that the following conditions
      9  1.19.8.2  tonnerre  * are met:
     10  1.19.8.2  tonnerre  * 1. Redistributions of source code must retain the above copyright
     11  1.19.8.2  tonnerre  *    notice, this list of conditions and the following disclaimer.
     12  1.19.8.2  tonnerre  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.19.8.2  tonnerre  *    notice, this list of conditions and the following disclaimer in the
     14  1.19.8.2  tonnerre  *    documentation and/or other materials provided with the distribution.
     15  1.19.8.2  tonnerre  * 3. Neither the name of the University nor the names of its contributors
     16  1.19.8.2  tonnerre  *    may be used to endorse or promote products derived from this software
     17  1.19.8.2  tonnerre  *    without specific prior written permission.
     18  1.19.8.2  tonnerre  *
     19  1.19.8.2  tonnerre  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.19.8.2  tonnerre  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.19.8.2  tonnerre  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.19.8.2  tonnerre  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.19.8.2  tonnerre  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.19.8.2  tonnerre  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.19.8.2  tonnerre  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.19.8.2  tonnerre  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.19.8.2  tonnerre  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.19.8.2  tonnerre  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.19.8.2  tonnerre  * SUCH DAMAGE.
     30  1.19.8.2  tonnerre  */
     31  1.19.8.2  tonnerre 
     32  1.19.8.2  tonnerre #include <sys/cdefs.h>
     33  1.19.8.2  tonnerre #if defined(LIBC_SCCS) && !defined(lint)
     34  1.19.8.2  tonnerre #if 0
     35  1.19.8.2  tonnerre static char sccsid[] = "@(#)telldir.c	8.1 (Berkeley) 6/4/93";
     36  1.19.8.2  tonnerre #else
     37  1.19.8.2  tonnerre __RCSID("$NetBSD: telldir.c,v 1.19.8.2 2008/05/04 18:53:27 tonnerre Exp $");
     38  1.19.8.2  tonnerre #endif
     39  1.19.8.2  tonnerre #endif /* LIBC_SCCS and not lint */
     40  1.19.8.2  tonnerre 
     41  1.19.8.2  tonnerre #include "namespace.h"
     42  1.19.8.2  tonnerre #include "reentrant.h"
     43  1.19.8.2  tonnerre #include "extern.h"
     44  1.19.8.2  tonnerre #include <sys/param.h>
     45  1.19.8.2  tonnerre 
     46  1.19.8.2  tonnerre #include <assert.h>
     47  1.19.8.2  tonnerre #include <dirent.h>
     48  1.19.8.2  tonnerre #include <stdlib.h>
     49  1.19.8.2  tonnerre #include <unistd.h>
     50  1.19.8.2  tonnerre 
     51  1.19.8.2  tonnerre #include "dirent_private.h"
     52  1.19.8.2  tonnerre 
     53  1.19.8.2  tonnerre #ifdef __weak_alias
     54  1.19.8.2  tonnerre __weak_alias(telldir,_telldir)
     55  1.19.8.2  tonnerre #endif
     56  1.19.8.2  tonnerre 
     57  1.19.8.2  tonnerre long
     58  1.19.8.2  tonnerre telldir(DIR *dirp)
     59  1.19.8.2  tonnerre {
     60  1.19.8.2  tonnerre 	long rv;
     61  1.19.8.2  tonnerre #ifdef _REENTRANT
     62  1.19.8.2  tonnerre 	if (__isthreaded) {
     63  1.19.8.2  tonnerre 		mutex_lock((mutex_t *)dirp->dd_lock);
     64  1.19.8.2  tonnerre 		rv = (intptr_t)_telldir_unlocked(dirp);
     65  1.19.8.2  tonnerre 		mutex_unlock((mutex_t *)dirp->dd_lock);
     66  1.19.8.2  tonnerre 	} else
     67  1.19.8.2  tonnerre #endif
     68  1.19.8.2  tonnerre 		rv = (intptr_t)_telldir_unlocked(dirp);
     69  1.19.8.2  tonnerre 	return rv;
     70  1.19.8.2  tonnerre }
     71  1.19.8.2  tonnerre 
     72  1.19.8.2  tonnerre /*
     73  1.19.8.2  tonnerre  * return a pointer into a directory
     74  1.19.8.2  tonnerre  */
     75  1.19.8.2  tonnerre long
     76  1.19.8.2  tonnerre _telldir_unlocked(DIR *dirp)
     77  1.19.8.2  tonnerre {
     78  1.19.8.2  tonnerre 	struct dirpos *lp;
     79  1.19.8.2  tonnerre 
     80  1.19.8.2  tonnerre 	for (lp = dirp->dd_internal; lp; lp = lp->dp_next)
     81  1.19.8.2  tonnerre 		if (lp->dp_seek == dirp->dd_seek &&
     82  1.19.8.2  tonnerre 		    lp->dp_loc == dirp->dd_loc)
     83  1.19.8.2  tonnerre 			return (intptr_t)lp;
     84  1.19.8.2  tonnerre 
     85  1.19.8.2  tonnerre 	if ((lp = malloc(sizeof(*lp))) == NULL)
     86  1.19.8.2  tonnerre 		return (-1);
     87  1.19.8.2  tonnerre 
     88  1.19.8.2  tonnerre 	lp->dp_seek = dirp->dd_seek;
     89  1.19.8.2  tonnerre 	lp->dp_loc = dirp->dd_loc;
     90  1.19.8.2  tonnerre 	lp->dp_next = dirp->dd_internal;
     91  1.19.8.2  tonnerre 	dirp->dd_internal = lp;
     92  1.19.8.2  tonnerre 
     93  1.19.8.2  tonnerre 	return (intptr_t)lp;
     94  1.19.8.2  tonnerre }
     95  1.19.8.2  tonnerre 
     96  1.19.8.2  tonnerre /*
     97  1.19.8.2  tonnerre  * seek to an entry in a directory.
     98  1.19.8.2  tonnerre  * Only values returned by "telldir" should be passed to seekdir.
     99  1.19.8.2  tonnerre  */
    100  1.19.8.2  tonnerre void
    101  1.19.8.2  tonnerre _seekdir_unlocked(DIR *dirp, long loc)
    102  1.19.8.2  tonnerre {
    103  1.19.8.2  tonnerre 	struct dirpos *lp;
    104  1.19.8.2  tonnerre 
    105  1.19.8.2  tonnerre 	_DIAGASSERT(dirp != NULL);
    106  1.19.8.2  tonnerre 
    107  1.19.8.2  tonnerre 	for (lp = dirp->dd_internal; lp; lp = lp->dp_next)
    108  1.19.8.2  tonnerre 		if ((intptr_t)lp == loc)
    109  1.19.8.2  tonnerre 			break;
    110  1.19.8.2  tonnerre 
    111  1.19.8.2  tonnerre 	if (lp == NULL)
    112  1.19.8.2  tonnerre 		return;
    113  1.19.8.2  tonnerre 
    114  1.19.8.2  tonnerre 	if (lp->dp_loc == dirp->dd_loc && lp->dp_seek == dirp->dd_seek)
    115  1.19.8.2  tonnerre 		return;
    116  1.19.8.2  tonnerre 
    117  1.19.8.2  tonnerre 	dirp->dd_seek = lseek(dirp->dd_fd, lp->dp_seek, SEEK_SET);
    118  1.19.8.2  tonnerre 	dirp->dd_loc = 0;
    119  1.19.8.2  tonnerre 	while (dirp->dd_loc < lp->dp_loc)
    120  1.19.8.2  tonnerre 		if (_readdir_unlocked(dirp, 0) == NULL)
    121  1.19.8.2  tonnerre 			break;
    122  1.19.8.2  tonnerre }
    123