Back to index

glibc  2.9
Classes | Defines | Functions | Variables
telldir.c File Reference
#include <errno.h>
#include <stddef.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include "dirstream.h"

Go to the source code of this file.


struct  record


#define NBUCKETS   32


 __libc_lock_define_initialized (static, lock)
void seekdir (DIR *dirp, long int pos)


static struct recordrecords [32]
static off_t lastpos

Class Documentation

struct record

Definition at line 28 of file telldir.c.

Collaboration diagram for record:
Class Members
off_t cookie
struct record * next
size_t offset
off_t pos

Define Documentation

#define NBUCKETS   32

Definition at line 35 of file telldir.c.

Function Documentation

__libc_lock_define_initialized ( static  ,

Definition at line 38 of file telldir.c.

  struct record *new;
  long int pos;

  new = malloc (sizeof *new);
  if (new == NULL)
    return -1l;

  __libc_lock_lock (lock);

  new->pos = dirp->filepos;
  new->offset = dirp->offset;
  new->cookie = ++lastpos;
  new->next = records[new->cookie % NBUCKETS];
  records[new->cookie % NBUCKETS] = new;

  pos = new->cookie;

  __libc_lock_unlock (lock);

  return pos;
void seekdir ( DIR *  dirp,
long int  pos 

Definition at line 72 of file telldir.c.

  struct record *r, **prevr;

  __libc_lock_lock (lock);

  for (prevr = &records[pos % NBUCKETS], r = *prevr;
       r != NULL;
       prevr = &r->next, r = r->next)
    if (r->cookie == pos)
       __libc_lock_lock (dirp->__lock);
       if (dirp->filepos != r->pos || dirp->offset != r->offset)
           dirp->size = 0;  /* Must read a fresh buffer.  */
           /* Move to the saved position.  */
           __lseek (dirp->fd, r->pos, SEEK_SET);
           dirp->filepos = r->pos;
           dirp->offset = 0;
           /* Read entries until we reach the saved offset.  */
           while (dirp->offset < r->offset)
              struct dirent *scan;
              __libc_lock_unlock (dirp->__lock);
              scan = readdir (dirp);
              __libc_lock_lock (dirp->__lock);
              if (! scan)
       __libc_lock_unlock (dirp->__lock);

       /* To prevent leaking memory, cookies returned from telldir
          can only be used once.  So free this one's record now.  */
       *prevr = r->next;
       free (r);

  __libc_lock_unlock (lock);

  /* If we lost there is no way to indicate it.  Oh well.  */

Here is the call graph for this function:

Variable Documentation

off_t lastpos [static]

Definition at line 37 of file telldir.c.

struct record* records[32] [static]

Definition at line 36 of file telldir.c.