Back to index

glibc  2.9
Classes | Defines | Functions | Variables
bug-glob2.c File Reference
#include <errno.h>
#include <error.h>
#include <dirent.h>
#include <glob.h>
#include <mcheck.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "../test-skeleton.c"

Go to the source code of this file.

Classes

struct  my_DIR

Defines

#define PRINTF(fmt, args...)
#define nfiles   (sizeof (filesystem) / sizeof (filesystem[0]))
#define TEST_FUNCTION   do_test ()

Functions

static long int find_file (const char *s)
static void * my_opendir (const char *s)
static struct direntmy_readdir (void *gdir)
static void my_closedir (void *dir)
static int my_stat (const char *name, struct stat *st)
static void init_glob_altdirfuncs (glob_t *pglob)
int do_test (void)

Variables

struct {
const char * name
int level
int type
mode_t mode
filesystem []

Class Documentation

struct my_DIR

Definition at line 74 of file bug-glob2.c.

Class Members
int idx
int level
char room_for_dirent

Define Documentation

#define nfiles   (sizeof (filesystem) / sizeof (filesystem[0]))

Definition at line 71 of file bug-glob2.c.

#define PRINTF (   fmt,
  args... 
)

Definition at line 41 of file bug-glob2.c.

#define TEST_FUNCTION   do_test ()

Definition at line 302 of file bug-glob2.c.


Function Documentation

int do_test ( void  )

Definition at line 268 of file bug-glob2.c.

{
  mtrace ();

  glob_t gl;
  memset (&gl, 0, sizeof (gl));
  init_glob_altdirfuncs (&gl);

  if (glob ("dir/*able/*", GLOB_ERR | GLOB_ALTDIRFUNC, NULL, &gl)
      != GLOB_ABORTED)
    {
      puts ("glob did not fail with GLOB_ABORTED");
      exit (EXIT_FAILURE); 
    }

  globfree (&gl);

  memset (&gl, 0, sizeof (gl));
  init_glob_altdirfuncs (&gl);

  gl.gl_offs = 3;
  if (glob ("dir2/*", GLOB_DOOFFS, NULL, &gl) != GLOB_NOMATCH)
    {
      puts ("glob did not fail with GLOB_NOMATCH");
      exit (EXIT_FAILURE); 
    }

  globfree (&gl);

  muntrace ();

  return 0;
}

Here is the call graph for this function:

static long int find_file ( const char *  s) [static]

Definition at line 84 of file bug-glob2.c.

{
  int level = 1;
  long int idx = 0;

  if (strcmp (s, ".") == 0)
    return 0;

  if (s[0] == '.' && s[1] == '/')
    s += 2;

  while (*s != '\0')
    {
      char *endp = strchrnul (s, '/');

      PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);

      while (idx < nfiles && filesystem[idx].level >= level)
       {
         if (filesystem[idx].level == level
             && memcmp (s, filesystem[idx].name, endp - s) == 0
             && filesystem[idx].name[endp - s] == '\0')
           break;
         ++idx;
       }

      if (idx == nfiles || filesystem[idx].level < level)
       {
         errno = ENOENT;
         return -1;
       }

      if (*endp == '\0')
       return idx + 1;

      if (filesystem[idx].type != DT_DIR
         && (idx + 1 >= nfiles
             || filesystem[idx].level >= filesystem[idx + 1].level))
       {
         errno = ENOTDIR;
         return -1;
       }

      ++idx;

      s = endp + 1;
      ++level;
    }

  errno = ENOENT;
  return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void init_glob_altdirfuncs ( glob_t pglob) [static]

Definition at line 257 of file bug-glob2.c.

{
  pglob->gl_closedir = my_closedir;
  pglob->gl_readdir = my_readdir;
  pglob->gl_opendir = my_opendir;
  pglob->gl_lstat = my_stat;
  pglob->gl_stat = my_stat;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void my_closedir ( void *  dir) [static]

Definition at line 222 of file bug-glob2.c.

{
  PRINTF ("my_closedir ()\n");
  free (dir);
}

Here is the caller graph for this function:

static void* my_opendir ( const char *  s) [static]

Definition at line 139 of file bug-glob2.c.

{
  long int idx = find_file (s);
  my_DIR *dir;

  if (idx == -1)
    {
      PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
      return NULL;
    }

  if ((filesystem[idx].mode & 0400) == 0)
    {
      errno = EACCES;
      PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
      return NULL;
    }

  dir = (my_DIR *) malloc (sizeof (my_DIR));
  if (dir == NULL)
    {
      printf ("cannot allocate directory handle: %m\n");
      exit (EXIT_FAILURE);
    }

  dir->level = filesystem[idx].level;
  dir->idx = idx;

  PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
         s, filesystem[idx].level, idx);

  return dir;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct dirent* my_readdir ( void *  gdir) [static, read]

Definition at line 175 of file bug-glob2.c.

{
  my_DIR *dir = gdir;

  if (dir->idx == -1)
    {
      PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
             dir->level, (long int) dir->idx);
      return NULL;
    }

  while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
    ++dir->idx;

  if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
    {
      dir->idx = -1;
      PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
             dir->level, (long int) dir->idx);
      return NULL;
    }

  dir->d.d_ino = dir->idx;

#ifdef _DIRENT_HAVE_D_TYPE
  dir->d.d_type = filesystem[dir->idx].type;
#endif

  strcpy (dir->d.d_name, filesystem[dir->idx].name);

#ifdef _DIRENT_HAVE_D_TYPE
  PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
         dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
         dir->d.d_name);
#else
  PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
         dir->level, (long int) dir->idx, dir->d.d_ino,
         dir->d.d_name);
#endif

  ++dir->idx;

  return &dir->d;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int my_stat ( const char *  name,
struct stat st 
) [static]

Definition at line 231 of file bug-glob2.c.

{
  long int idx = find_file (name);

  if (idx == -1)
    {
      PRINTF ("my_stat (\"%s\", ...) = -1 (%m)\n", name);
      return -1;
    }

  memset (st, '\0', sizeof (*st));

  if (filesystem[idx].type == DT_UNKNOWN)
    st->st_mode = DTTOIF (idx + 1 < nfiles
                       && filesystem[idx].level < filesystem[idx + 1].level
                       ? DT_DIR : DT_REG) | filesystem[idx].mode;
  else
    st->st_mode = DTTOIF (filesystem[idx].type) | filesystem[idx].mode;

  PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

struct { ... } filesystem[] [static]