static int do_glob()

in libs/libc/misc/lib_glob.c [99:364]


static int do_glob(FAR char *buf, size_t pos, int type, FAR char *pat,
                   int flags,
                   CODE int (*errfunc)(FAR const char *path, int err),
                   FAR struct match_s **tail)
{
  ptrdiff_t i = 0;
  ptrdiff_t j = 0;
  int in_bracket = 0;
  int overflow = 0;
  FAR char *p2;
  char saved_sep = '/';
  FAR DIR *dir;
  int old_errno;
  FAR struct dirent *de;
  int readerr;

  /* If GLOB_MARK is unused, we don't care about type. */

  if (!type && !(flags & GLOB_MARK))
    {
      type = DT_REG;
    }

  /* Special-case the remaining pattern being all slashes, in
   * which case we can use caller-passed type if it's a dir.
   */

  if (*pat && type != DT_DIR)
    {
      type = 0;
    }

  while (pos + 1 < PATH_MAX && *pat == '/')
    {
      buf[pos++] = *pat++;
    }

  /* Consume maximal [escaped-]literal prefix of pattern, copying
   * and un-escaping it to the running buffer as we go.
   */

  for (; pat[i] != '*' && pat[i] != '?'
       && (!in_bracket || pat[i] != ']'); i++)
    {
      if (!pat[i])
        {
          if (overflow)
            {
              return 0;
            }

          pat += i;
          pos += j;
          i = j = 0;
          break;
        }
      else if (pat[i] == '[')
        {
          in_bracket = 1;
        }
      else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE))
        {
          /* Backslashes inside a bracket are (at least by
           * our interpretation) non-special, so if next
           * char is ']' we have a complete expression.
           */

          if (in_bracket && pat[i + 1] == ']')
            {
              break;
            }

          /* Unpaired final backslash never matches. */

          if (!pat[i + 1])
            {
              return 0;
            }

          i++;
        }

      if (pat[i] == '/')
        {
          if (overflow)
            {
              return 0;
            }

          in_bracket = 0;
          pat += i + 1;
          i = -1;
          pos += j + 1;
          j = -1;
        }

      /* Only store a character if it fits in the buffer, but if
       * a potential bracket expression is open, the overflow
       * must be remembered and handled later only if the bracket
       * is unterminated (and thereby a literal), so as not to
       * disallow long bracket expressions with short matches.
       */

      if (pos + (j + 1) < PATH_MAX)
        {
          buf[pos + j++] = pat[i];
        }
      else if (in_bracket)
        {
          overflow = 1;
        }
      else
        {
          return 0;
        }

      /* If we consume any new components, the caller-passed type
       * or dummy type from above is no longer valid.
       */

      type = 0;
    }

  buf[pos] = 0;
  if (!*pat)
    {
      /* If we consumed any components above, or if GLOB_MARK is
       * requested and we don't yet know if the match is a dir,
       * we must confirm the file exists and/or determine its type.
       *
       * If marking dirs, symlink type is inconclusive; we need the
       * type for the symlink target, and therefore must try stat
       * first unless type is known not to be a symlink. Otherwise,
       * or if that fails, use lstat for determining existence to
       * avoid false negatives in the case of broken symlinks.
       */

      struct stat st;
      if ((flags & GLOB_MARK) && (!type || type == DT_LNK)
           && !stat(buf, &st))
        {
          if (S_ISDIR(st.st_mode))
            {
              type = DT_DIR;
            }
          else
            {
              type = DT_REG;
            }
        }

      if (!type && lstat(buf, &st))
        {
          if (get_errno() != ENOENT &&
              (errfunc(buf, get_errno()) || (flags & GLOB_ERR) != 0))
            {
              return GLOB_ABORTED;
            }

          return 0;
        }

      if (append(tail, buf, pos, (flags & GLOB_MARK) && type == DT_DIR))
        {
          return GLOB_NOSPACE;
        }

      return 0;
    }

  p2 = strchr(pat, '/');

  /* Check if the '/' was escaped and, if so, remove the escape char
   * so that it will not be unpaired when passed to fnmatch.
   */

  if (p2 && !(flags & GLOB_NOESCAPE))
    {
      FAR char *p;
      const int prev_index = -1;
      for (p = p2; p > pat && p[prev_index] == '\\'; p--)
        {
        }

      if ((p2 - p) % 2)
        {
          p2--;
          saved_sep = '\\';
        }
    }

  dir = opendir(pos ? buf : ".");
  if (!dir)
    {
      if (errfunc(buf, get_errno()) || (flags & GLOB_ERR) != 0)
        {
          return GLOB_ABORTED;
        }

      return 0;
    }

  old_errno = get_errno();
  while (get_errno() = 0, de = readdir(dir))
    {
      size_t l;
      int fnm_flags;
      int r;

      /* Quickly skip non-directories when there's pattern left. */

      if (p2 && de->d_type && de->d_type != DT_DIR && de->d_type != DT_LNK)
        {
          continue;
        }

      l = strlen(de->d_name);
      if (l >= PATH_MAX - pos)
        {
          continue;
        }

      if (p2)
        {
          *p2 = 0;
        }

      fnm_flags = ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
                  | FNM_PERIOD;

      if (fnmatch(pat, de->d_name, fnm_flags))
        {
          continue;
        }

      strlcpy(buf + pos, de->d_name, l + 1);

      if (p2)
        {
          *p2 = saved_sep;
        }

      r = do_glob(buf, pos + l, de->d_type, p2 ? p2 : "",
                      flags, errfunc, tail);
      if (r)
        {
          closedir(dir);
          return r;
        }
    }

  readerr = get_errno();
  if (p2)
    {
      *p2 = saved_sep;
    }

  closedir(dir);
  if (readerr && (errfunc(buf, get_errno()) || (flags & GLOB_ERR) != 0))
    {
      return GLOB_ABORTED;
    }

  set_errno(old_errno);
  return 0;
}