chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / unix / bsd / telldir.c
1 /* Copyright (C) 1994, 1995, 1996, 1997, 1999 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <errno.h>
20 #include <stddef.h>
21 #include <dirent.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include "dirstream.h"
26
27 /* Internal data structure for telldir and seekdir.  */
28 struct record
29   {
30     struct record *next; /* Link in chain.  */
31     off_t cookie;               /* Value returned by `telldir'.  */
32     off_t pos;
33     size_t offset;
34   };
35 #define NBUCKETS 32
36 static struct record *records[32];
37 static off_t lastpos;
38 __libc_lock_define_initialized(static, lock) /* Locks above data.  */
39
40
41 /* Return the current position of DIRP.  */
42 long int
43 telldir (dirp)
44      DIR *dirp;
45 {
46   struct record *new;
47   long int pos;
48
49   new = malloc (sizeof *new);
50   if (new == NULL)
51     return -1l;
52
53   __libc_lock_lock (lock);
54
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;
60
61   pos = new->cookie;
62
63   __libc_lock_unlock (lock);
64
65   return pos;
66 }
67
68 \f
69
70 /* Seek to position POS in DIRP.  */
71 void
72 seekdir (dirp, pos)
73      DIR *dirp;
74      long int pos;
75 {
76   struct record *r, **prevr;
77
78   __libc_lock_lock (lock);
79
80   for (prevr = &records[pos % NBUCKETS], r = *prevr;
81        r != NULL;
82        prevr = &r->next, r = r->next)
83     if (r->cookie == pos)
84       {
85         __libc_lock_lock (dirp->__lock);
86         if (dirp->filepos != r->pos || dirp->offset != r->offset)
87           {
88             dirp->size = 0;     /* Must read a fresh buffer.  */
89             /* Move to the saved position.  */
90             __lseek (dirp->fd, r->pos, SEEK_SET);
91             dirp->filepos = r->pos;
92             dirp->offset = 0;
93             /* Read entries until we reach the saved offset.  */
94             while (dirp->offset < r->offset)
95               {
96                 struct dirent *scan;
97                 __libc_lock_unlock (dirp->__lock);
98                 scan = readdir (dirp);
99                 __libc_lock_lock (dirp->__lock);
100                 if (! scan)
101                   break;
102               }
103           }
104         __libc_lock_unlock (dirp->__lock);
105
106         /* To prevent leaking memory, cookies returned from telldir
107            can only be used once.  So free this one's record now.  */
108         *prevr = r->next;
109         free (r);
110         break;
111       }
112
113   __libc_lock_unlock (lock);
114
115   /* If we lost there is no way to indicate it.  Oh well.  */
116 }