initial import
[glibc.git] / sysdeps / unix / bsd / telldir.c
blob0321f954b8daddf2a495534407b14011f0ceac1d
1 /* Copyright (C) 1994 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>
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;
40 /* Return the current position of DIRP. */
41 off_t
42 DEFUN(telldir, (dirp), DIR *dirp)
44 struct record *new;
46 new = malloc (sizeof *new);
47 if (new == NULL)
48 return (off_t) -1;
50 new->pos = dirp->__pos;
51 new->offset = dirp->__offset;
52 new->cookie = ++lastpos;
53 new->next = records[new->cookie % NBUCKETS];
54 records[new->cookie % NBUCKETS] = new;
56 return new->cookie;
61 /* Seek to position POS in DIRP. */
62 void
63 DEFUN(seekdir, (dirp, pos), DIR *dirp AND __off_t pos)
65 struct record *r, **prevr;
67 for (prevr = &records[pos % NBUCKETS], r = *prevr;
68 r != NULL;
69 prevr = &r->next, r = r->next)
70 if (r->cookie == pos)
72 if (dirp->__pos != r->pos || dirp->__offset != r->offset)
74 dirp->__size = 0; /* Must read a fresh buffer. */
75 /* Move to the saved position. */
76 __lseek (dirp->__fd, r->pos, SEEK_SET);
77 dirp->__pos = r->pos;
78 dirp->__offset = 0;
79 /* Read entries until we reach the saved offset. */
80 while (dirp->__offset < r->offset)
81 if (readdir (dirp) == NULL)
82 break;
85 /* To prevent leaking memory, cookies returned from telldir
86 can only be used once. So free this one's record now. */
87 *prevr = r->next;
88 free (r);
89 return;
92 /* We lost, but have no way to indicate it. Oh well. */