src/windows/native/java/io/canonicalize_md.c
int
canonicalize(char *orig_path, char *result, int size)
{
    WIN32_FIND_DATA fd;
    HANDLE h;
    char path[1024];    /* Working copy of path */
    char *src, *dst, *dend;

    /* Reject paths that contain wildcards */
    if (wild(orig_path)) {
        errno = EINVAL;
        return -1;
    }

    /* Collapse instances of "foo\.." and ensure absoluteness.  Note that
       contrary to the documentation, the _fullpath procedure does not require
       the drive to be available.  It also does not reliably change all
       occurrences of '/' to '\\' on Win95, so now JVM_NativePath does that. */
    if(!_fullpath(path, orig_path, sizeof(path))) {
        return -1;
    }

    /* Correction for Win95: _fullpath may leave a trailing "\\"
       on a UNC pathname */
    if ((path[0] == '\\') && (path[1] == '\\')) {
        char *p = path + strlen(path);
        if ((p[-1] == '\\') && !islb(p[-2])) {
            p[-1] = '\0';
        }
    }

    if (dots(path)) /* Check for prohibited combinations of dots */
        return -1;

    src = path;            /* Start scanning here */
    dst = result;        /* Place results here */
    dend = dst + size;        /* Don't go to or past here */

    /* Copy prefix, assuming path is absolute */
    if (isalpha(src[0]) && (src[1] == ':') && (src[2] == '\\')) {
        /* Drive specifier */
        *src = toupper(*src);    /* Canonicalize drive letter */
        if (!(dst = cp(dst, dend, '\0', src, src + 2))) {
            return -1;
        }
        src += 2;
    } else if ((src[0] == '\\') && (src[1] == '\\')) {
        /* UNC pathname */
        char *p;
        p = nextsep(src + 2);    /* Skip past host name */
        if (!*p) {
        /* A UNC pathname must begin with "\\\\host\\share",
           so reject this path as invalid if there is no share name */
            errno = EINVAL;
            return -1;
    }
    p = nextsep(p + 1);    /* Skip past share name */
    if (!(dst = cp(dst, dend, '\0', src, p))) {
        return -1;
    }
    src = p;
    } else {
        /* Invalid path */
        errno = EINVAL;
        return -1;
    }

    /* Windows 95/98/Me bug - FindFirstFile fails on network mounted drives */
    /* for root pathes like "E:\" . If the path has this form, we should  */
    /* simply return it, it is already canonicalized. */
    if (strlen(path) == 3 && path[1] == ':' && path[2] == '\\') {
        /* At this point we have already copied the drive specifier ("z:")*/
        /* so we need to copy "\" and the null character. */
        result[2] = '\\';
        result[3] = '\0';
        return 0;
    }

    /* At this point we have copied either a drive specifier ("z:") or a UNC
       prefix ("\\\\host\\share") to the result buffer, and src points to the
       first byte of the remainder of the path.  We now scan through the rest
       of the path, looking up each prefix in order to find the true name of
       the last element of each prefix, thereby computing the full true name of
       the original path. */
    while (*src) {
        char *p = nextsep(src + 1);    /* Find next separator */
        char c = *p;
        assert(*src == '\\');        /* Invariant */
        *p = '\0';            /* Temporarily clear separator */
        h = FindFirstFile(path, &fd);    /* Look up prefix */
        *p = c;                /* Restore separator */
        if (h != INVALID_HANDLE_VALUE) {
            /* Lookup succeeded; append true name to result and continue */
            FindClose(h);
            if (!(dst = cp(dst, dend, '\\',
                           fd.cFileName,
                           fd.cFileName + strlen(fd.cFileName)))) {
                return -1;
            }
            src = p;
            continue;
        } else {
            if (!lastErrorReportable()) {
                if (!(dst = cp(dst, dend, '\0', src, src + strlen(src)))) {
                    return -1;
                }
                break;
            } else {
                return -1;
            }
        }
    }

    if (dst >= dend) {
    errno = ENAMETOOLONG;
    return -1;
    }
    *dst = '\0';
    return 0;

}
/* Convert a pathname to canonical form.  The input orig_path is assumed to
   have been converted to native form already, via JVM_NativePath().  This is
   necessary because _fullpath() rejects duplicate separator characters on
   Win95, though it accepts them on NT. */
Convert a pathname to canonical form.  The input orig_path is assumed to
have been converted to native form already, via JVM_NativePath().  This is
necessary because _fullpath() rejects duplicate separator characters on
Win95, though it accepts them on NT.
Convert a pathname to canonical form . The input orig_path is assumed to have been converted to native form already via JVM_NativePath () . This is necessary because _fullpath () rejects duplicate separator characters on Win95 though it accepts them on NT .
int
canonicalize(char *orig_path, char *result, int size)
{
    WIN32_FIND_DATA fd;
    HANDLE h;
    char path[1024];    
    char *src, *dst, *dend;
    if (wild(orig_path)) {
        errno = EINVAL;
        return -1;
    }
    if(!_fullpath(path, orig_path, sizeof(path))) {
        return -1;
    }
    if ((path[0] == '\\') && (path[1] == '\\')) {
        char *p = path + strlen(path);
        if ((p[-1] == '\\') && !islb(p[-2])) {
            p[-1] = '\0';
        }
    }
    if (dots(path)) 
        return -1;
    src = path;            
    dst = result;        
    dend = dst + size;        
    if (isalpha(src[0]) && (src[1] == ':') && (src[2] == '\\')) {
        *src = toupper(*src);    
        if (!(dst = cp(dst, dend, '\0', src, src + 2))) {
            return -1;
        }
        src += 2;
    } else if ((src[0] == '\\') && (src[1] == '\\')) {
        char *p;
        p = nextsep(src + 2);    
        if (!*p) {
            errno = EINVAL;
            return -1;
    }
    p = nextsep(p + 1);    
    if (!(dst = cp(dst, dend, '\0', src, p))) {
        return -1;
    }
    src = p;
    } else {
        errno = EINVAL;
        return -1;
    }
    if (strlen(path) == 3 && path[1] == ':' && path[2] == '\\') {
        result[2] = '\\';
        result[3] = '\0';
        return 0;
    }
    while (*src) {
        char *p = nextsep(src + 1);    
        char c = *p;
        assert(*src == '\\');        
        *p = '\0';            
        h = FindFirstFile(path, &fd);    
        *p = c;                
        if (h != INVALID_HANDLE_VALUE) {
            FindClose(h);
            if (!(dst = cp(dst, dend, '\\',
                           fd.cFileName,
                           fd.cFileName + strlen(fd.cFileName)))) {
                return -1;
            }
            src = p;
            continue;
        } else {
            if (!lastErrorReportable()) {
                if (!(dst = cp(dst, dend, '\0', src, src + strlen(src)))) {
                    return -1;
                }
                break;
            } else {
                return -1;
            }
        }
    }
    if (dst >= dend) {
    errno = ENAMETOOLONG;
    return -1;
    }
    *dst = '\0';
    return 0;
}
int canonicalize ( char * orig_path , char * result , int size ) { WIN32_FIND_DATA fd ; HANDLE h ; char path [ 1024 ] ; char * src , * dst , * dend ; if ( wild ( orig_path ) ) { errno = EINVAL ; return -1 ; } if ( ! _fullpath ( path , orig_path , sizeof ( path ) ) ) { return -1 ; } if ( ( path [ 0 ] == ' \\ ' ) && ( path [ 1 ] == ' \\ ' ) ) { char * p = path + strlen ( path ) ; if ( ( p [ -1 ] == ' \\ ' ) && ! islb ( p [ -2 ] ) ) { p [ -1 ] = ' \0 ' ; } } if ( dots ( path ) ) return -1 ; src = path ; dst = result ; dend = dst + size ; if ( isalpha ( src [ 0 ] ) && ( src [ 1 ] == ' ' ) && ( src [ 2 ] == ' \\ ' ) ) { * src = toupper ( * src ) ; if ( ! ( dst = cp ( dst , dend , ' \0 ' , src , src + 2 ) ) ) { return -1 ; } src += 2 ; } else if ( ( src [ 0 ] == ' \\ ' ) && ( src [ 1 ] == ' \\ ' ) ) { char * p ; p = nextsep ( src + 2 ) ; if ( ! * p ) { errno = EINVAL ; return -1 ; } p = nextsep ( p + 1 ) ; if ( ! ( dst = cp ( dst , dend , ' \0 ' , src , p ) ) ) { return -1 ; } src = p ; } else { errno = EINVAL ; return -1 ; } if ( strlen ( path ) == 3 && path [ 1 ] == ' ' && path [ 2 ] == ' \\ ' ) { result [ 2 ] = ' \\ ' ; result [ 3 ] = ' \0 ' ; return 0 ; } while ( * src ) { char * p = nextsep ( src + 1 ) ; char c = * p ; assert ( * src == ' \\ ' ) ; * p = ' \0 ' ; h = FindFirstFile ( path , & fd ) ; * p = c ; if ( h != INVALID_HANDLE_VALUE ) { FindClose ( h ) ; if ( ! ( dst = cp ( dst , dend , ' \\ ' , fd . cFileName , fd . cFileName + strlen ( fd . cFileName ) ) ) ) { return -1 ; } src = p ; continue ; } else { if ( ! lastErrorReportable ( ) ) { if ( ! ( dst = cp ( dst , dend , ' \0 ' , src , src + strlen ( src ) ) ) ) { return -1 ; } break ; } else { return -1 ; } } } if ( dst >= dend ) { errno = ENAMETOOLONG ; return -1 ; } * dst = ' \0 ' ; return 0 ; }
Convert a pathname to canonical form.
Convert a pathname to canonical form .
/* Working copy of path */ /* Reject paths that contain wildcards */ /* Collapse instances of "foo\.." and ensure absoluteness.  Note that
       contrary to the documentation, the _fullpath procedure does not require
       the drive to be available.  It also does not reliably change all
       occurrences of '/' to '\\' on Win95, so now JVM_NativePath does that. */ /* Correction for Win95: _fullpath may leave a trailing "\\"
       on a UNC pathname */ /* Check for prohibited combinations of dots */ /* Start scanning here */ /* Place results here */ /* Don't go to or past here */ /* Copy prefix, assuming path is absolute */ /* Drive specifier */ /* Canonicalize drive letter */ /* UNC pathname */ /* Skip past host name */ /* A UNC pathname must begin with "\\\\host\\share",
           so reject this path as invalid if there is no share name */ /* Skip past share name */ /* Invalid path */ /* Windows 95/98/Me bug - FindFirstFile fails on network mounted drives */ /* for root pathes like "E:\" . If the path has this form, we should  */ /* simply return it, it is already canonicalized. */ /* At this point we have already copied the drive specifier ("z:")*/ /* so we need to copy "\" and the null character. */ /* At this point we have copied either a drive specifier ("z:") or a UNC
       prefix ("\\\\host\\share") to the result buffer, and src points to the
       first byte of the remainder of the path.  We now scan through the rest
       of the path, looking up each prefix in order to find the true name of
       the last element of each prefix, thereby computing the full true name of
       the original path. */ /* Find next separator */ /* Invariant */ /* Temporarily clear separator */ /* Look up prefix */ /* Restore separator */ /* Lookup succeeded; append true name to result and continue */