Update.
[glibc/history.git] / sysdeps / unix / bsd / telldir.c
bloba01b30248b9932f1ab36be70186698bc2752f858
1 /* Copyright (C) 1994, 1995, 1996 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <stddef.h>
22 #include <dirent.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <stdlib.h>
26 #include "dirstream.h"
28 /* Internal data structure for telldir and seekdir. */
29 struct record
31 struct record *next; /* Link in chain. */
32 off_t cookie; /* Value returned by `telldir'. */
33 off_t pos;
34 size_t offset;
36 #define NBUCKETS 32
37 static struct record *records[32];
38 static off_t lastpos;
39 __libc_lock_define_initialized(static, lock) /* Locks above data. */
42 /* Return the current position of DIRP. */
43 off_t
44 DEFUN(telldir, (dirp), DIR *dirp)
46 struct record *new;
47 off_t pos;
49 __libc_lock_lock (lock);
51 new = malloc (sizeof *new);
52 if (new == NULL)
53 return (off_t) -1;
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 DEFUN(seekdir, (dirp, pos), DIR *dirp AND __off_t pos)
74 struct record *r, **prevr;
76 __libc_lock_lock (lock);
78 for (prevr = &records[pos % NBUCKETS], r = *prevr;
79 r != NULL;
80 prevr = &r->next, r = r->next)
81 if (r->cookie == pos)
83 __libc_lock_lock (dirp->__lock);
84 if (dirp->filepos != r->pos || dirp->offset != r->offset)
86 dirp->size = 0; /* Must read a fresh buffer. */
87 /* Move to the saved position. */
88 __lseek (dirp->fd, r->pos, SEEK_SET);
89 dirp->filepos = r->pos;
90 dirp->offset = 0;
91 /* Read entries until we reach the saved offset. */
92 while (dirp->offset < r->offset)
94 struct dirent *scan;
95 __libc_lock_unlock (dirp->__lock);
96 scan = readdir (dirp);
97 __libc_lock_lock (dirp->__lock);
98 if (! scan)
99 break;
102 __libc_lock_unlock (dirp->__lock);
104 /* To prevent leaking memory, cookies returned from telldir
105 can only be used once. So free this one's record now. */
106 *prevr = r->next;
107 free (r);
108 break;
111 __libc_lock_unlock (lock);
113 /* If we lost there is no way to indicate it. Oh well. */