Utilities/Stat_Path [ Functions ]

FUNCTION

Check the given path against required type. d -- directory, f -- file, e -- exists

RETURN VALUE

TRUE if path is of the given type, otherwise FALSE.

BUGS

Should check if symbolic link points to a directory or to a file.

SOURCE

int Stat_Path(
    char required,
    char *path )
{
    struct stat         st;
    int                 res = FALSE;

    if ( stat( path, &st ) < 0 )
    {
        if ( required == 'e' )
        {
            res = FALSE;
        }
        else
        {
            if ( ( strcmp( path, "./" ) == 0 ) && ( required == 'd' ) )
            {
                /* This fixes a bug in Mingw, where ./ can not be
                   stat-ed under windows2000 and above,
                   we just assume that ./ always exists. */
                res = TRUE;
            }
            else
            {
                RB_Panic( "Stat_Path: can not stat '%s'\n", path );
            }
        }
    }
    else
    {
        switch ( ( ( st.st_mode ) & S_IFMT ) )
        {
        case S_IFDIR:
            if ( ( required == 'd' ) || ( required == 'e' ) )
            {
                res = TRUE;
            }
            break;
        case S_IFREG:
            if ( ( required == 'f' ) || ( required == 'e' ) )
            {
                res = TRUE;
            }
            break;
            /* TODO case S_IFLNK: chdir() */
        default:
            break;
        }                       /* end switch */
    }
    return res;
}