telldir.c revision 5958
1118611Snjl/* 2118611Snjl * Copyright (c) 1983, 1993 3207344Sjkim * The Regents of the University of California. All rights reserved. 4118611Snjl * 5118611Snjl * Redistribution and use in source and binary forms, with or without 6118611Snjl * modification, are permitted provided that the following conditions 7217365Sjkim * are met: 8245582Sjkim * 1. Redistributions of source code must retain the above copyright 9118611Snjl * notice, this list of conditions and the following disclaimer. 10118611Snjl * 2. Redistributions in binary form must reproduce the above copyright 11217365Sjkim * notice, this list of conditions and the following disclaimer in the 12217365Sjkim * documentation and/or other materials provided with the distribution. 13217365Sjkim * 3. All advertising materials mentioning features or use of this software 14217365Sjkim * must display the following acknowledgement: 15217365Sjkim * This product includes software developed by the University of 16217365Sjkim * California, Berkeley and its contributors. 17217365Sjkim * 4. Neither the name of the University nor the names of its contributors 18217365Sjkim * may be used to endorse or promote products derived from this software 19217365Sjkim * without specific prior written permission. 20217365Sjkim * 21217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22217365Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23217365Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24217365Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25118611Snjl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29118611Snjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30217365Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31217365Sjkim * SUCH DAMAGE. 32217365Sjkim */ 33217365Sjkim 34217365Sjkim#if defined(LIBC_SCCS) && !defined(lint) 35217365Sjkimstatic char sccsid[] = "@(#)telldir.c 8.1 (Berkeley) 6/4/93"; 36217365Sjkim#endif /* LIBC_SCCS and not lint */ 37217365Sjkim 38217365Sjkim#include <sys/param.h> 39217365Sjkim#include <dirent.h> 40217365Sjkim#include <stdlib.h> 41217365Sjkim#include <unistd.h> 42217365Sjkim 43118611Snjl/* 44118611Snjl * The option SINGLEUSE may be defined to say that a telldir 45151937Sjkim * cookie may be used only once before it is freed. This option 46118611Snjl * is used to avoid having memory usage grow without bound. 47207344Sjkim */ 48118611Snjl#define SINGLEUSE 49118611Snjl 50118611Snjl/* 51118611Snjl * One of these structures is malloced to describe the current directory 52207344Sjkim * position each time telldir is called. It records the current magic 53207344Sjkim * cookie returned by getdirentries and the offset within the buffer 54118611Snjl * associated with that return value. 55207344Sjkim */ 56207344Sjkimstruct ddloc { 57207344Sjkim struct ddloc *loc_next;/* next structure in list */ 58207344Sjkim long loc_index; /* key associated with structure */ 59118611Snjl long loc_seek; /* magic cookie returned by getdirentries */ 60118611Snjl long loc_loc; /* offset of entry in buffer */ 61118611Snjl DIR* loc_dirp; /* directory which used this entry */ 62207344Sjkim}; 63118611Snjl 64118611Snjl#define NDIRHASH 32 /* Num of hash lists, must be a power of 2 */ 65118611Snjl#define LOCHASH(i) ((i)&(NDIRHASH-1)) 66118611Snjl 67118611Snjlstatic long dd_loccnt; /* Index of entry for sequential readdir's */ 68118611Snjlstatic struct ddloc *dd_hash[NDIRHASH]; /* Hash list heads for ddlocs */ 69118611Snjl 70207344Sjkim/* 71118611Snjl * return a pointer into a directory 72118611Snjl */ 73118611Snjllong 74118611Snjltelldir(dirp) 75207344Sjkim const DIR *dirp; 76118611Snjl{ 77118611Snjl register int index; 78118611Snjl register struct ddloc *lp; 79151937Sjkim 80118611Snjl if ((lp = (struct ddloc *)malloc(sizeof(struct ddloc))) == NULL) 81118611Snjl return (-1); 82118611Snjl index = dd_loccnt++; 83118611Snjl lp->loc_index = index; 84118611Snjl lp->loc_seek = dirp->dd_seek; 85118611Snjl lp->loc_loc = dirp->dd_loc; 86207344Sjkim lp->loc_dirp = dirp; 87118611Snjl lp->loc_next = dd_hash[LOCHASH(index)]; 88118611Snjl dd_hash[LOCHASH(index)] = lp; 89207344Sjkim return (index); 90207344Sjkim} 91118611Snjl 92151937Sjkim/* 93151937Sjkim * seek to an entry in a directory. 94118611Snjl * Only values returned by "telldir" should be passed to seekdir. 95118611Snjl */ 96118611Snjlvoid 97118611Snjl_seekdir(dirp, loc) 98207344Sjkim register DIR *dirp; 99118611Snjl long loc; 100207344Sjkim{ 101207344Sjkim register struct ddloc *lp; 102207344Sjkim register struct ddloc **prevlp; 103151937Sjkim struct dirent *dp; 104151937Sjkim 105207344Sjkim prevlp = &dd_hash[LOCHASH(loc)]; 106151937Sjkim lp = *prevlp; 107207344Sjkim while (lp != NULL) { 108207344Sjkim if (lp->loc_index == loc) 109207344Sjkim break; 110151937Sjkim prevlp = &lp->loc_next; 111151937Sjkim lp = lp->loc_next; 112207344Sjkim } 113151937Sjkim if (lp == NULL) 114207344Sjkim return; 115207344Sjkim if (lp->loc_loc == dirp->dd_loc && lp->loc_seek == dirp->dd_seek) 116207344Sjkim goto found; 117151937Sjkim (void) lseek(dirp->dd_fd, (off_t)lp->loc_seek, SEEK_SET); 118151937Sjkim dirp->dd_seek = lp->loc_seek; 119207344Sjkim dirp->dd_loc = 0; 120151937Sjkim while (dirp->dd_loc < lp->loc_loc) { 121207344Sjkim dp = readdir(dirp); 122228110Sjkim if (dp == NULL) 123207344Sjkim break; 124151937Sjkim } 125151937Sjkimfound: 126207344Sjkim#ifdef SINGLEUSE 127151937Sjkim *prevlp = lp->loc_next; 128207344Sjkim free((caddr_t)lp); 129207344Sjkim#endif 130207344Sjkim} 131151937Sjkim 132207344Sjkim/* 133118611Snjl * Reclaim memory for telldir cookies which weren't used. 134207344Sjkim */ 135151937Sjkimvoid 136151937Sjkim_reclaim_telldir(dirp) 137118611Snjl register DIR *dirp; 138118611Snjl{ 139207344Sjkim register struct ddloc *lp; 140118611Snjl register struct ddloc **prevlp; 141118611Snjl int i; 142118611Snjl 143118611Snjl for (i = 0; i < NDIRHASH; i++) { 144118611Snjl prevlp = &dd_hash[i]; 145118611Snjl lp = *prevlp; 146118611Snjl while (lp != NULL) { 147118611Snjl if (lp->loc_dirp == dirp) { 148118611Snjl *prevlp = lp->loc_next; 149118611Snjl free((caddr_t)lp); 150118611Snjl lp = *prevlp; 151118611Snjl continue; 152118611Snjl } 153118611Snjl prevlp = &lp->loc_next; 154118611Snjl lp = lp->loc_next; 155118611Snjl } 156118611Snjl } 157118611Snjl} 158118611Snjl