]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/bsd/telldir.c
1 /* Copyright (C) 1994-2013 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, see
16 <http://www.gnu.org/licenses/>. */
22 #include <sys/types.h>
24 #include "dirstream.h"
26 /* Internal data structure for telldir and seekdir. */
29 struct record
*next
; /* Link in chain. */
30 off_t cookie
; /* Value returned by `telldir'. */
35 static struct record
*records
[32];
37 __libc_lock_define_initialized(static, lock
) /* Locks above data. */
40 /* Return the current position of DIRP. */
48 new = malloc (sizeof *new);
52 __libc_lock_lock (lock
);
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;
62 __libc_lock_unlock (lock
);
69 /* Seek to position POS in DIRP. */
75 struct record
*r
, **prevr
;
77 __libc_lock_lock (lock
);
79 for (prevr
= &records
[pos
% NBUCKETS
], r
= *prevr
;
81 prevr
= &r
->next
, r
= r
->next
)
84 __libc_lock_lock (dirp
->__lock
);
85 if (dirp
->filepos
!= r
->pos
|| dirp
->offset
!= r
->offset
)
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
;
92 /* Read entries until we reach the saved offset. */
93 while (dirp
->offset
< r
->offset
)
96 __libc_lock_unlock (dirp
->__lock
);
97 scan
= readdir (dirp
);
98 __libc_lock_lock (dirp
->__lock
);
103 __libc_lock_unlock (dirp
->__lock
);
105 /* To prevent leaking memory, cookies returned from telldir
106 can only be used once. So free this one's record now. */
112 __libc_lock_unlock (lock
);
114 /* If we lost there is no way to indicate it. Oh well. */