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