Home | History | Annotate | Line # | Download | only in gen
telldir.c revision 1.3.2.2
      1  1.3.2.2  jtc /*
      2  1.3.2.2  jtc  * Copyright (c) 1983, 1993
      3  1.3.2.2  jtc  *	The Regents of the University of California.  All rights reserved.
      4  1.3.2.2  jtc  *
      5  1.3.2.2  jtc  * Redistribution and use in source and binary forms, with or without
      6  1.3.2.2  jtc  * modification, are permitted provided that the following conditions
      7  1.3.2.2  jtc  * are met:
      8  1.3.2.2  jtc  * 1. Redistributions of source code must retain the above copyright
      9  1.3.2.2  jtc  *    notice, this list of conditions and the following disclaimer.
     10  1.3.2.2  jtc  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.3.2.2  jtc  *    notice, this list of conditions and the following disclaimer in the
     12  1.3.2.2  jtc  *    documentation and/or other materials provided with the distribution.
     13  1.3.2.2  jtc  * 3. All advertising materials mentioning features or use of this software
     14  1.3.2.2  jtc  *    must display the following acknowledgement:
     15  1.3.2.2  jtc  *	This product includes software developed by the University of
     16  1.3.2.2  jtc  *	California, Berkeley and its contributors.
     17  1.3.2.2  jtc  * 4. Neither the name of the University nor the names of its contributors
     18  1.3.2.2  jtc  *    may be used to endorse or promote products derived from this software
     19  1.3.2.2  jtc  *    without specific prior written permission.
     20  1.3.2.2  jtc  *
     21  1.3.2.2  jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.3.2.2  jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.3.2.2  jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.3.2.2  jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.3.2.2  jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.3.2.2  jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.3.2.2  jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.3.2.2  jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.3.2.2  jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.3.2.2  jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.3.2.2  jtc  * SUCH DAMAGE.
     32  1.3.2.2  jtc  */
     33  1.3.2.2  jtc 
     34  1.3.2.2  jtc #if defined(LIBC_SCCS) && !defined(lint)
     35  1.3.2.2  jtc /*static char sccsid[] = "from: @(#)telldir.c	8.1 (Berkeley) 6/4/93";*/
     36  1.3.2.2  jtc static char rcsid[] = "$Id: telldir.c,v 1.3.2.2 1994/07/27 14:39:54 jtc Exp $";
     37  1.3.2.2  jtc #endif /* LIBC_SCCS and not lint */
     38  1.3.2.2  jtc 
     39  1.3.2.2  jtc #include <sys/param.h>
     40  1.3.2.2  jtc #include <dirent.h>
     41  1.3.2.2  jtc #include <stdlib.h>
     42  1.3.2.2  jtc #include <unistd.h>
     43  1.3.2.2  jtc 
     44  1.3.2.2  jtc /*
     45  1.3.2.2  jtc  * The option SINGLEUSE may be defined to say that a telldir
     46  1.3.2.2  jtc  * cookie may be used only once before it is freed. This option
     47  1.3.2.2  jtc  * is used to avoid having memory usage grow without bound.
     48  1.3.2.2  jtc  */
     49  1.3.2.2  jtc #define SINGLEUSE
     50  1.3.2.2  jtc 
     51  1.3.2.2  jtc /*
     52  1.3.2.2  jtc  * One of these structures is malloced to describe the current directory
     53  1.3.2.2  jtc  * position each time telldir is called. It records the current magic
     54  1.3.2.2  jtc  * cookie returned by getdirentries and the offset within the buffer
     55  1.3.2.2  jtc  * associated with that return value.
     56  1.3.2.2  jtc  */
     57  1.3.2.2  jtc struct ddloc {
     58  1.3.2.2  jtc 	struct	ddloc *loc_next;/* next structure in list */
     59  1.3.2.2  jtc 	long	loc_index;	/* key associated with structure */
     60  1.3.2.2  jtc 	long	loc_seek;	/* magic cookie returned by getdirentries */
     61  1.3.2.2  jtc 	long	loc_loc;	/* offset of entry in buffer */
     62  1.3.2.2  jtc };
     63  1.3.2.2  jtc 
     64  1.3.2.2  jtc #define	NDIRHASH	32	/* Num of hash lists, must be a power of 2 */
     65  1.3.2.2  jtc #define	LOCHASH(i)	((i)&(NDIRHASH-1))
     66  1.3.2.2  jtc 
     67  1.3.2.2  jtc static long	dd_loccnt;	/* Index of entry for sequential readdir's */
     68  1.3.2.2  jtc static struct	ddloc *dd_hash[NDIRHASH];   /* Hash list heads for ddlocs */
     69  1.3.2.2  jtc 
     70  1.3.2.2  jtc /*
     71  1.3.2.2  jtc  * return a pointer into a directory
     72  1.3.2.2  jtc  */
     73  1.3.2.2  jtc long
     74  1.3.2.2  jtc telldir(dirp)
     75  1.3.2.2  jtc 	const DIR *dirp;
     76  1.3.2.2  jtc {
     77  1.3.2.2  jtc 	register int index;
     78  1.3.2.2  jtc 	register struct ddloc *lp;
     79  1.3.2.2  jtc 
     80  1.3.2.2  jtc 	if ((lp = (struct ddloc *)malloc(sizeof(struct ddloc))) == NULL)
     81  1.3.2.2  jtc 		return (-1);
     82  1.3.2.2  jtc 	index = dd_loccnt++;
     83  1.3.2.2  jtc 	lp->loc_index = index;
     84  1.3.2.2  jtc 	lp->loc_seek = dirp->dd_seek;
     85  1.3.2.2  jtc 	lp->loc_loc = dirp->dd_loc;
     86  1.3.2.2  jtc 	lp->loc_next = dd_hash[LOCHASH(index)];
     87  1.3.2.2  jtc 	dd_hash[LOCHASH(index)] = lp;
     88  1.3.2.2  jtc 	return (index);
     89  1.3.2.2  jtc }
     90  1.3.2.2  jtc 
     91  1.3.2.2  jtc /*
     92  1.3.2.2  jtc  * seek to an entry in a directory.
     93  1.3.2.2  jtc  * Only values returned by "telldir" should be passed to seekdir.
     94  1.3.2.2  jtc  */
     95  1.3.2.2  jtc void
     96  1.3.2.2  jtc __seekdir(dirp, loc)
     97  1.3.2.2  jtc 	register DIR *dirp;
     98  1.3.2.2  jtc 	long loc;
     99  1.3.2.2  jtc {
    100  1.3.2.2  jtc 	register struct ddloc *lp;
    101  1.3.2.2  jtc 	register struct ddloc **prevlp;
    102  1.3.2.2  jtc 	struct dirent *dp;
    103  1.3.2.2  jtc 
    104  1.3.2.2  jtc 	prevlp = &dd_hash[LOCHASH(loc)];
    105  1.3.2.2  jtc 	lp = *prevlp;
    106  1.3.2.2  jtc 	while (lp != NULL) {
    107  1.3.2.2  jtc 		if (lp->loc_index == loc)
    108  1.3.2.2  jtc 			break;
    109  1.3.2.2  jtc 		prevlp = &lp->loc_next;
    110  1.3.2.2  jtc 		lp = lp->loc_next;
    111  1.3.2.2  jtc 	}
    112  1.3.2.2  jtc 	if (lp == NULL)
    113  1.3.2.2  jtc 		return;
    114  1.3.2.2  jtc 	if (lp->loc_loc == dirp->dd_loc && lp->loc_seek == dirp->dd_seek)
    115  1.3.2.2  jtc 		goto found;
    116  1.3.2.2  jtc 	(void) lseek(dirp->dd_fd, (off_t)lp->loc_seek, SEEK_SET);
    117  1.3.2.2  jtc 	dirp->dd_seek = lp->loc_seek;
    118  1.3.2.2  jtc 	dirp->dd_loc = 0;
    119  1.3.2.2  jtc 	while (dirp->dd_loc < lp->loc_loc) {
    120  1.3.2.2  jtc 		dp = readdir(dirp);
    121  1.3.2.2  jtc 		if (dp == NULL)
    122  1.3.2.2  jtc 			break;
    123  1.3.2.2  jtc 	}
    124  1.3.2.2  jtc found:
    125  1.3.2.2  jtc #ifdef SINGLEUSE
    126  1.3.2.2  jtc 	*prevlp = lp->loc_next;
    127  1.3.2.2  jtc 	free((caddr_t)lp);
    128  1.3.2.2  jtc #endif
    129  1.3.2.2  jtc }
    130