1 /* Copyright (C) 1994, 1995, 1996, 1997, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <sys/types.h>
25 #include "dirstream.h"
27 /* Internal data structure for telldir and seekdir. */
30 struct record *next; /* Link in chain. */
31 off_t cookie; /* Value returned by `telldir'. */
36 static struct record *records[32];
38 __libc_lock_define_initialized(static, lock) /* Locks above data. */
41 /* Return the current position of DIRP. */
49 new = malloc (sizeof *new);
53 __libc_lock_lock (lock);
55 new->pos = dirp->filepos;
56 new->offset = dirp->offset;
57 new->cookie = ++lastpos;
58 new->next = records[new->cookie % NBUCKETS];
59 records[new->cookie % NBUCKETS] = new;
63 __libc_lock_unlock (lock);
70 /* Seek to position POS in DIRP. */
76 struct record *r, **prevr;
78 __libc_lock_lock (lock);
80 for (prevr = &records[pos % NBUCKETS], r = *prevr;
82 prevr = &r->next, r = r->next)
85 __libc_lock_lock (dirp->__lock);
86 if (dirp->filepos != r->pos || dirp->offset != r->offset)
88 dirp->size = 0; /* Must read a fresh buffer. */
89 /* Move to the saved position. */
90 __lseek (dirp->fd, r->pos, SEEK_SET);
91 dirp->filepos = r->pos;
93 /* Read entries until we reach the saved offset. */
94 while (dirp->offset < r->offset)
97 __libc_lock_unlock (dirp->__lock);
98 scan = readdir (dirp);
99 __libc_lock_lock (dirp->__lock);
104 __libc_lock_unlock (dirp->__lock);
106 /* To prevent leaking memory, cookies returned from telldir
107 can only be used once. So free this one's record now. */
113 __libc_lock_unlock (lock);
115 /* If we lost there is no way to indicate it. Oh well. */