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. */
24 #include <sys/types.h>
26 #include "dirstream.h"
28 /* Internal data structure for telldir and seekdir. */
31 struct record
*next
; /* Link in chain. */
32 off_t cookie
; /* Value returned by `telldir'. */
37 static struct record
*records
[32];
39 __libc_lock_define_initialized(static, lock
) /* Locks above data. */
42 /* Return the current position of DIRP. */
44 DEFUN(telldir
, (dirp
), DIR *dirp
)
49 __libc_lock_lock (lock
);
51 new = malloc (sizeof *new);
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;
63 __libc_lock_unlock (lock
);
70 /* Seek to position POS in DIRP. */
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
;
80 prevr
= &r
->next
, r
= r
->next
)
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
;
91 /* Read entries until we reach the saved offset. */
92 while (dirp
->offset
< r
->offset
)
95 __libc_lock_unlock (dirp
->__lock
);
96 scan
= readdir (dirp
);
97 __libc_lock_lock (dirp
->__lock
);
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. */
111 __libc_lock_unlock (lock
);
113 /* If we lost there is no way to indicate it. Oh well. */