Example #1
0
_WCRTLINK int fstat( int handle, struct stat *buf )
#endif
{
    APIRET          error;
    OS_UINT         hand_type;
    OS_UINT         device_attr;
    FF_BUFFER       info;
    unsigned        iomode_flags;

    __handle_check( handle, -1 );

    buf->st_mode = 0;
    iomode_flags = __GetIOMode( handle );
    if( iomode_flags & _READ ) {
        buf->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
    }
    if( iomode_flags & _WRITE ) {
        buf->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
    }

    error = DosQHandType( handle, &hand_type, &device_attr );
    if( error ) {
        return( __set_errno_dos( error ) );
    }
    if( ( hand_type & ~HANDTYPE_NETWORK ) == HANDTYPE_FILE ) {
        /* handle file */
        error = DosQFileInfo( handle, FF_LEVEL, (PBYTE)&info, sizeof( info ) );
        if( error ) {
            return( __set_errno_dos( error ) );
        }
        buf->st_dev = buf->st_rdev = 0;
        /* handle timestamps */
        buf->st_ctime = _d2ttime( TODDATE( info.fdateCreation ),
                                  TODTIME( info.ftimeCreation ) );
        buf->st_atime = _d2ttime( TODDATE( info.fdateLastAccess ),
                                  TODTIME( info.ftimeLastAccess ) );
        buf->st_mtime = _d2ttime( TODDATE( info.fdateLastWrite ),
                                  TODTIME( info.ftimeLastWrite ) );
        buf->st_btime = buf->st_mtime;
#if defined( __INT64__ ) && !defined( _M_I86 )
        if( _FILEAPI64() ) {
#endif
            buf->st_attr = info.attrFile;
            buf->st_mode |= at2mode( info.attrFile );
            buf->st_size = info.cbFile;
#if defined( __INT64__ ) && !defined( _M_I86 )
        } else {
            buf->st_attr = ((FF_BUFFER_32 *)&info)->attrFile;
            buf->st_mode |= at2mode( ((FF_BUFFER_32 *)&info)->attrFile );
            buf->st_size = ((FF_BUFFER_32 *)&info)->cbFile;
        }
#endif
    } else {
        /* handle device */
        /* handle attributes */
        buf->st_attr = 0;
        buf->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
        if( ( hand_type & ~HANDTYPE_NETWORK ) == HANDTYPE_DEVICE ) {
            buf->st_mode |= S_IFCHR;
        } else if( ( hand_type & ~HANDTYPE_NETWORK ) == HANDTYPE_PIPE ) {
            buf->st_mode |= S_IFIFO;
        }
        buf->st_dev = buf->st_rdev = 1;
        /* handle timestamps */
        buf->st_atime = 0;
        buf->st_ctime = 0;
        buf->st_mtime = 0;
        buf->st_btime = 0;
        /* handle size */
        buf->st_size = 0;
    }
    buf->st_nlink = 1;
    buf->st_ino = handle;
    buf->st_uid = buf->st_gid = 0;

    buf->st_archivedID = 0;
    buf->st_updatedID = 0;
    buf->st_inheritedRightsMask = 0;
    buf->st_originatingNameSpace = 0;
    return( 0 );
}
Example #2
0
_WCRTLINK int stat( CHAR_TYPE const *path, struct stat *buf )
{
    const CHAR_TYPE *   ptr;
    CHAR_TYPE           fullpath[_MAX_PATH];
    CHAR_TYPE       *   fullp;
    int                 isrootdir = 0;
    int                 handle;
    int                 RdosAttrib;
    int                 ok;
    CHAR_TYPE           name[_MAX_PATH];
    int                 attrib;
    unsigned long       wr_msb;
    unsigned long       wr_lsb;
    long                size;
    int                 ms;
    int                 us;
    struct tm           tm;
    int                 i;

    /* reject null string and names that has wildcard */
    if( *path == '\0' || _mbspbrk( path, "*?" ) != NULL )
        return( -1 );

    /*** Determine if 'path' refers to a root directory ***/
    if( _fullpath( fullpath, path, _MAX_PATH ) != NULL ) {
        if( isalpha( fullpath[0] )  &&  fullpath[1] == ':'  &&
            fullpath[2] == '\\'  &&  fullpath[3] == '\0' )
        {
            isrootdir = 1;
        }
    }
    else
        return( -1 );

    ptr = path;
    if( *_mbsinc(path ) )  ptr += 2;
    if( ( (ptr[0] == '\\' || ptr[0] == '/') && ptr[1] == '\0' )  ||  isrootdir )
    {
        /* handle root directory */
        CHAR_TYPE       cwd[_MAX_PATH];

        /* save current directory */
        getcwd( cwd, _MAX_PATH );

        /* try to change to specified root */
        if( chdir( path ) != 0 )  return( -1 );

        /* restore current directory */
        chdir( cwd );

        attrib   = _A_SUBDIR;    
        wr_msb   = 0;
        wr_lsb   = 0;
        size     = 0;
        name[0] = NULLCHAR;
    } else {                            /* not a root directory */
        fullp = fullpath + strlen( fullpath ) - 1;
        while( *fullp != '\\' && *fullp != '/' && fullp != fullpath )
            fullp--;
        *fullp = 0;
        fullp++;
        if( strlen( fullpath ) == 0 )
            strcpy( fullpath, "*" );
                        
        handle = RdosOpenDir( fullpath );

        ok = 0;
        i = 0;
        strlwr( fullp );
        while( RdosReadDir( handle, i, _MAX_PATH, name, &size, &RdosAttrib, &wr_msb, &wr_lsb ) ) {
            strlwr( name );        
            if( !strcmp( name, fullp ) ) {
                ok = 1;
                break;
            }
            i++;
        }

        RdosCloseDir( handle );

        if( !ok )
            return( -1 );

        attrib = 0;
        if( RdosAttrib & FILE_ATTRIBUTE_ARCHIVE ) {
            attrib |= _A_ARCH;
        }
        if( RdosAttrib & FILE_ATTRIBUTE_DIRECTORY ) {
            attrib |= _A_SUBDIR;
        }
        if( RdosAttrib & FILE_ATTRIBUTE_HIDDEN ) {
            attrib |= _A_HIDDEN;
        }
        if( RdosAttrib & FILE_ATTRIBUTE_NORMAL ) {
            attrib |= _A_NORMAL;
        }
        if( RdosAttrib & FILE_ATTRIBUTE_READONLY ) {
            attrib |= _A_RDONLY;
        }
        if( RdosAttrib & FILE_ATTRIBUTE_SYSTEM ) {
            attrib |= _A_SYSTEM;
        }
    }

    /* process drive number */
    if( *_mbsinc(path) ) {
        buf->st_dev = tolower( fullpath[0] ) - 'a';
    } else {
        buf->st_dev = RdosGetCurDrive();
    }
    buf->st_rdev = buf->st_dev;

    buf->st_size = size;
    buf->st_mode = at2mode( attrib, name );

    RdosDecodeMsbTics( wr_msb, 
                       &tm.tm_year, 
                       &tm.tm_mon,
                       &tm.tm_mday,
                       &tm.tm_hour );

    RdosDecodeLsbTics( wr_lsb,
                       &tm.tm_min,
                       &tm.tm_sec,
                       &ms,
                       &us );
                           
    tm.tm_year -= 1900;
    tm.tm_mon--;
    tm.tm_isdst = -1;
    tm.tm_wday = -1;
    tm.tm_yday = -1;

    buf->st_mtime = mktime( &tm );
    buf->st_atime = buf->st_ctime = buf->st_btime = buf->st_mtime;

    buf->st_nlink = 1;
    buf->st_ino = buf->st_uid = buf->st_gid = 0;

    buf->st_attr = attrib;
    buf->st_archivedID = 0;
    buf->st_updatedID = 0;
    buf->st_inheritedRightsMask = 0;
    buf->st_originatingNameSpace = 0;

    return( 0 );
}