* nscd/connections.c (verify_persistent_db): Recognize circular lists.
[glibc.git] / sysdeps / unix / bsd / telldir.c
blob3d625c65d5ea50e1656e0a01e839fbb9e6f08e71
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
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stddef.h>
21 #include <dirent.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include "dirstream.h"
27 /* Internal data structure for telldir and seekdir. */
28 struct record
30 struct record *next; /* Link in chain. */
31 off_t cookie; /* Value returned by `telldir'. */
32 off_t pos;
33 size_t offset;
35 #define NBUCKETS 32
36 static struct record *records[32];
37 static off_t lastpos;
38 __libc_lock_define_initialized(static, lock) /* Locks above data. */
41 /* Return the current position of DIRP. */
42 long int
43 telldir (dirp)
44 DIR *dirp;
46 struct record *new;
47 long int pos;
49 new = malloc (sizeof *new);
50 if (new == NULL)
51 return -1l;
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;
61 pos = new->cookie;
63 __libc_lock_unlock (lock);
65 return pos;
70 /* Seek to position POS in DIRP. */
71 void
72 seekdir (dirp, pos)
73 DIR *dirp;
74 long int pos;
76 struct record *r, **prevr;
78 __libc_lock_lock (lock);
80 for (prevr = &records[pos % NBUCKETS], r = *prevr;
81 r != NULL;
82 prevr = &r->next, r = r->next)
83 if (r->cookie == pos)
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;
92 dirp->offset = 0;
93 /* Read entries until we reach the saved offset. */
94 while (dirp->offset < r->offset)
96 struct dirent *scan;
97 __libc_lock_unlock (dirp->__lock);
98 scan = readdir (dirp);
99 __libc_lock_lock (dirp->__lock);
100 if (! scan)
101 break;
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. */
108 *prevr = r->next;
109 free (r);
110 break;
113 __libc_lock_unlock (lock);
115 /* If we lost there is no way to indicate it. Oh well. */