]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/bsd/telldir.c
Update copyright notices with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / bsd / telldir.c
1 /* Copyright (C) 1994-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #include <errno.h>
19 #include <stddef.h>
20 #include <dirent.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <stdlib.h>
24 #include "dirstream.h"
25
26 /* Internal data structure for telldir and seekdir. */
27 struct record
28 {
29 struct record *next; /* Link in chain. */
30 off_t cookie; /* Value returned by `telldir'. */
31 off_t pos;
32 size_t offset;
33 };
34 #define NBUCKETS 32
35 static struct record *records[32];
36 static off_t lastpos;
37 __libc_lock_define_initialized(static, lock) /* Locks above data. */
38
39
40 /* Return the current position of DIRP. */
41 long int
42 telldir (dirp)
43 DIR *dirp;
44 {
45 struct record *new;
46 long int pos;
47
48 new = malloc (sizeof *new);
49 if (new == NULL)
50 return -1l;
51
52 __libc_lock_lock (lock);
53
54 new->pos = dirp->filepos;
55 new->offset = dirp->offset;
56 new->cookie = ++lastpos;
57 new->next = records[new->cookie % NBUCKETS];
58 records[new->cookie % NBUCKETS] = new;
59
60 pos = new->cookie;
61
62 __libc_lock_unlock (lock);
63
64 return pos;
65 }
66
67 \f
68
69 /* Seek to position POS in DIRP. */
70 void
71 seekdir (dirp, pos)
72 DIR *dirp;
73 long int pos;
74 {
75 struct record *r, **prevr;
76
77 __libc_lock_lock (lock);
78
79 for (prevr = &records[pos % NBUCKETS], r = *prevr;
80 r != NULL;
81 prevr = &r->next, r = r->next)
82 if (r->cookie == pos)
83 {
84 __libc_lock_lock (dirp->__lock);
85 if (dirp->filepos != r->pos || dirp->offset != r->offset)
86 {
87 dirp->size = 0; /* Must read a fresh buffer. */
88 /* Move to the saved position. */
89 __lseek (dirp->fd, r->pos, SEEK_SET);
90 dirp->filepos = r->pos;
91 dirp->offset = 0;
92 /* Read entries until we reach the saved offset. */
93 while (dirp->offset < r->offset)
94 {
95 struct dirent *scan;
96 __libc_lock_unlock (dirp->__lock);
97 scan = readdir (dirp);
98 __libc_lock_lock (dirp->__lock);
99 if (! scan)
100 break;
101 }
102 }
103 __libc_lock_unlock (dirp->__lock);
104
105 /* To prevent leaking memory, cookies returned from telldir
106 can only be used once. So free this one's record now. */
107 *prevr = r->next;
108 free (r);
109 break;
110 }
111
112 __libc_lock_unlock (lock);
113
114 /* If we lost there is no way to indicate it. Oh well. */
115 }