Пример #1
0
int CLY_IsUNCShare(const char* path)
{
  static int nt = -1;
  char* path2 = (char*)alloca(strlen(path) + 1);
  char* p;
  const char *server, *share;

  strcpy(path2, path);

  if (!CLY_IsUNC(path2))
    return 0;
  server = path2 + 2;
  p = strchr(server, '/');
  if (!p)
    p = strchr(server, '\\');
  if (!p)
    return 0;
  if (p[1] == '\0')
    return 0;
  *p = '\0';

  share = p + 1;
  p = strchr(share, '/');
  if (!p)
    p = strchr(share, '\\');
  if (!p || p[1] == '\0' || (p[1] == '.' && p[2] == '\0'))
  {
    int ret;
    if (p) *p = '\0';
    if (nt == -1 || nt == 1)
    {
      ret = CLY_isUNC_helper_NT(server, share);
      nt = !(ret == -1);
    }
    if (nt == 0)
    {
      ret = CLY_isUNC_helper_95(server, share);
      if (ret == -1) nt = -1, ret = 0;
    }
    return ret;
  }

  return 0;
}
Пример #2
0
/* Takes as input an arbitrary path.  Fixes up the path by:
 1. Removing consecutive slashes
 2. Removing trailing slashes
 3. Making the path absolute if it wasn't already
 4. Removing "." in the path
 5. Removing ".." entries in the path (and the directory above them)
 6. Adding a drive specification if one wasn't there
 7. Converting all slashes to '/'
 */
void
_fixpath(const char *in, char *out)
{
    int drive_number = 0;
    const char *ip = in;
    char *op = out;
    int unc = CLY_IsUNC(in); /* is a UNC pathname */

    /* Add drive specification to output string */
    if (((*ip >= 'a' && *ip <= 'z') ||
            (*ip >= 'A' && *ip <= 'Z'))
            && (*(ip + 1) == ':'))
    {
        if (*ip >= 'a' && *ip <= 'z')
        {
            drive_number = *ip - 'a';
            *op++ = *ip++;
        }
        else
        {
            drive_number = *ip - 'A';
            *op++ = (char)(drive_number + 'a');
            ++ip;
        }
        *op++ = *ip++;
    }
    else if (!unc)
    {
        drive_number = __fp_getdisk();
        *op++ = (char)(drive_number + 'a');
        *op++ = ':';
    }

    /* Convert relative path to absolute */
    if (!is_slash(*ip))
    {
        *op++ = '/';
        op = __fp_getcurdir(op, drive_number);
    }

    /* Handle UNC path */
    if (unc)
    {
        *op++ = *ip++;
    }

    /* Step through the input path */
    while (*ip)
    {
        /* Skip input slashes */
        if (is_slash(*ip))
        {
            ip++;
            continue;
        }

        /* Skip "." and output nothing */
        if (*ip == '.' && is_term(*(ip + 1)))
        {
            ip++;
            continue;
        }

        /* Skip ".." and remove previous output directory */
        if (*ip == '.' && *(ip + 1) == '.' && is_term(*(ip + 2)))
        {
            ip += 2;
            /* Don't back up over drive spec */
            if (op > out + 2)
            {
                /* This requires "/" to follow drive spec */
                --op;
                while (!is_slash(*op)) --op;
            }
            continue;
        }

        /* Copy path component from in to out */
        *op++ = '/';
        while (!is_term(*ip)) *op++ = *ip++;
    }

    /* If root directory, insert trailing slash */
    if (op == out + 2) *op++ = '/';

    /* Null terminate the output */
    *op = '\0';

    /* Convert backslashes to slashes */
    for (op = out; *op; op++)
        if (*op == '\\') *op = '/';
}