예제 #1
0
파일: fssize.c 프로젝트: AmericoBalboa/core
HB_FOFFSET hb_fsFSize( const char * pszFileName, HB_BOOL bUseDirEntry )
{
   if( bUseDirEntry )
   {
#if defined( HB_OS_WIN )
      PHB_FFIND ffind = hb_fsFindFirst( pszFileName, HB_FA_ALL );
      hb_fsSetIOError( ffind != NULL, 0 );
      if( ffind )
      {
         HB_FOFFSET size = ffind->size;
         hb_fsFindClose( ffind );
         return size;
      }
#elif defined( HB_USE_LARGEFILE64 )
      char * pszFree;
      HB_BOOL fResult;
      struct stat64 statbuf;
      pszFileName = hb_fsNameConv( pszFileName, &pszFree );
      statbuf.st_size = 0;
      hb_vmUnlock();
      fResult = stat64( pszFileName, &statbuf ) == 0;
      hb_fsSetIOError( fResult, 0 );
      hb_vmLock();
      if( pszFree )
         hb_xfree( pszFree );
      if( fResult )
         return ( HB_FOFFSET ) statbuf.st_size;
#else
      char * pszFree;
      HB_BOOL fResult;
      struct stat statbuf;
      pszFileName = hb_fsNameConv( pszFileName, &pszFree );
      statbuf.st_size = 0;
      hb_vmUnlock();
      fResult = stat( ( char * ) pszFileName, &statbuf ) == 0;
      hb_fsSetIOError( fResult, 0 );
      hb_vmLock();
      if( pszFree )
         hb_xfree( pszFree );
      if( fResult )
         return ( HB_FOFFSET ) statbuf.st_size;
#endif
   }
   else
   {
      HB_FHANDLE hFileHandle = hb_fsOpen( pszFileName, FO_READ | FO_COMPAT );

      if( hFileHandle != FS_ERROR )
      {
         HB_FOFFSET nPos = hb_fsSeekLarge( hFileHandle, 0, FS_END );
         hb_fsClose( hFileHandle );
         return nPos;
      }
   }
   return 0;
}
예제 #2
0
파일: hbfsapi.c 프로젝트: jiangxilong/core
HB_BOOL hb_fsDirExists( const char * pszDirName )
{
   HB_BOOL fExist = HB_FALSE;

   HB_TRACE( HB_TR_DEBUG, ( "hb_fsDirExists(%p)", pszDirName ) );

   if( pszDirName != NULL )
   {
#if defined( HB_OS_WIN )
      LPTSTR lpFree;
      LPCTSTR lpDirName = HB_FSNAMECONV( pszDirName, &lpFree );
      DWORD dwAttr;

      dwAttr = GetFileAttributes( lpDirName );
      fExist = ( dwAttr != INVALID_FILE_ATTRIBUTES ) &&
               ( dwAttr & FILE_ATTRIBUTE_DIRECTORY );

      if( lpFree )
         hb_xfree( lpFree );
#elif defined( HB_OS_OS2 )
      HB_FATTR nAttr;
      fExist = hb_fsOS2QueryPathInfo( pszDirName, NULL, &nAttr, NULL, NULL ) &&
               ( nAttr & HB_FA_DIRECTORY ) != 0;
#else
      char * pszFree = NULL;

      pszDirName = hb_fsNameConv( pszDirName, &pszFree );

      {
#  if defined( HB_OS_DOS )
#     if defined( __DJGPP__ ) || defined( __BORLANDC__ )
         int iAttr = _chmod( pszDirName, 0, 0 );
         fExist = iAttr != -1 && ( iAttr & 0x10 ) != 0;
#     else
         unsigned int iAttr = 0;
         fExist = _dos_getfileattr( pszDirName, &iAttr ) == 0 &&
                  ( iAttr & 0x10 ) != 0;
#     endif
#  elif defined( HB_OS_UNIX )
#     if defined( HB_USE_LARGEFILE64 )
         struct stat64 statbuf;
         fExist = stat64( pszDirName, &statbuf ) == 0 &&
                  S_ISDIR( statbuf.st_mode );
#     else
         struct stat statbuf;
         fExist = stat( pszDirName, &statbuf ) == 0 &&
                  S_ISDIR( statbuf.st_mode );
#     endif
#  else
         int iTODO; /* To force warning */
#  endif
      }

      if( pszFree )
         hb_xfree( pszFree );
#endif
   }

   return fExist;
}
예제 #3
0
파일: hbfsapi.c 프로젝트: jiangxilong/core
HB_BOOL hb_fsOS2QueryPathInfo( const char * pszPathName,
                               HB_FOFFSET * pnSize, HB_FATTR * pnAttr,
                               long * plJulian, long * plMillisec )
{
   HDIR hdirFindHandle = HDIR_CREATE;
   HB_FILEFINDBUF3L findBuffer;
   ULONG ulFindCount = 1;
   char * pszFree;
   APIRET ret;
   HB_BOOL fIsWSeB = hb_isWSeB();

   pszPathName = hb_fsNameConv( pszPathName, &pszFree );
   ret = DosFindFirst( ( PCSZ ) pszPathName, &hdirFindHandle,
                       FILE_ARCHIVED | FILE_DIRECTORY |
                       FILE_SYSTEM | FILE_HIDDEN | FILE_READONLY,
                       &findBuffer, sizeof( findBuffer ), &ulFindCount,
                       fIsWSeB ? FIL_STANDARDL : FIL_STANDARD );
   hb_fsSetError( ( HB_ERRCODE ) ret );
   if( hdirFindHandle != HDIR_CREATE )
      DosFindClose( hdirFindHandle );
   if( pszFree )
      hb_xfree( pszFree );

   if( ret == NO_ERROR )
   {
      if( fIsWSeB )
      {
         if( pnSize )
            *pnSize = ( HB_FOFFSET ) findBuffer.ffbl.cbFile;
         if( pnAttr )
            *pnAttr = hb_fsAttrFromRaw( ( HB_FATTR ) findBuffer.ffbl.attrFile );
         if( plJulian )
            *plJulian = hb_dateEncode( findBuffer.ffbl.fdateLastWrite.year + 1980,
                                       findBuffer.ffbl.fdateLastWrite.month,
                                       findBuffer.ffbl.fdateLastWrite.day );
         if( plMillisec )
            *plMillisec = hb_timeEncode( findBuffer.ffbl.ftimeLastWrite.hours,
                                         findBuffer.ffbl.ftimeLastWrite.minutes,
                                         findBuffer.ffbl.ftimeLastWrite.twosecs * 2, 0 );
      }
      else
      {
         if( pnSize )
            *pnSize = ( HB_FOFFSET ) findBuffer.ffb.cbFile;
         if( pnAttr )
            *pnAttr = hb_fsAttrFromRaw( ( HB_FATTR ) findBuffer.ffb.attrFile );
         if( plJulian )
            *plJulian = hb_dateEncode( findBuffer.ffb.fdateLastWrite.year + 1980,
                                       findBuffer.ffb.fdateLastWrite.month,
                                       findBuffer.ffb.fdateLastWrite.day );
         if( plMillisec )
            *plMillisec = hb_timeEncode( findBuffer.ffb.ftimeLastWrite.hours,
                                         findBuffer.ffb.ftimeLastWrite.minutes,
                                         findBuffer.ffb.ftimeLastWrite.twosecs * 2, 0 );
      }
      return HB_TRUE;
   }
   return HB_FALSE;
}
예제 #4
0
파일: hbfsapi.c 프로젝트: CsBela/core
HB_BOOL hb_fsFileExists( const char * pszFileName )
{
   HB_BOOL fExist = HB_FALSE;

   HB_TRACE( HB_TR_DEBUG, ( "hb_fsFileExists(%p)", pszFileName ) );

   if( pszFileName != NULL )
   {
#if defined( HB_OS_WIN )
      LPTSTR lpFree;
      LPCTSTR lpFileName = HB_FSNAMECONV( pszFileName, &lpFree );
      DWORD dwAttr;

      dwAttr = GetFileAttributes( lpFileName );
      fExist = ( dwAttr != INVALID_FILE_ATTRIBUTES ) &&
               ( dwAttr & ( FILE_ATTRIBUTE_DIRECTORY |
                            FILE_ATTRIBUTE_DEVICE ) ) == 0;

      if( lpFree )
         hb_xfree( lpFree );
#else
      char * pszFree = NULL;

      pszFileName = hb_fsNameConv( pszFileName, &pszFree );

      {
#  if defined( HB_OS_DOS )
#     if defined( __DJGPP__ ) || defined( __BORLANDC__ )
         int iAttr = _chmod( pszFileName, 0, 0 );
         fExist = iAttr != -1 && ( iAttr & 0x10 ) == 0;
#     else
         unsigned int iAttr = 0;
         fExist = _dos_getfileattr( pszFileName, &iAttr ) == 0 &&
                  ( iAttr & 0x10 ) == 0;
#     endif
#  elif defined( HB_OS_OS2 )
         FILESTATUS3 fs3;
         fExist = DosQueryPathInfo( ( PCSZ ) pszFileName, FIL_STANDARD,
                                    &fs3, sizeof( fs3 ) ) == NO_ERROR &&
                  ( fs3.attrFile & FILE_DIRECTORY ) == 0;
#  elif defined( HB_OS_UNIX )
         struct stat statbuf;

         fExist = stat( pszFileName, &statbuf ) == 0 &&
                  S_ISREG( statbuf.st_mode );
#  else
         int iTODO; /* To force warning */
#  endif
      }

      if( pszFree )
         hb_xfree( pszFree );
#endif
   }

   return fExist;
}
예제 #5
0
HB_BOOL hb_fsNameExists( const char * pszFileName )
{
   HB_BOOL fExist = HB_FALSE;

   HB_TRACE( HB_TR_DEBUG, ( "hb_fsNameExists(%p)", pszFileName ) );

   if( pszFileName != NULL )
   {
#if defined( HB_OS_WIN )
      LPTSTR lpFree;
      LPCTSTR lpFileName = HB_FSNAMECONV( pszFileName, &lpFree );

      fExist = ( GetFileAttributes( lpFileName ) != INVALID_FILE_ATTRIBUTES );

      if( lpFree )
         hb_xfree( lpFree );
#else
      char * pszFree = NULL;

      pszFileName = hb_fsNameConv( pszFileName, &pszFree );

      {
#  if defined( HB_OS_DOS )
#     if defined( __DJGPP__ ) || defined( __BORLANDC__ )
         fExist = _chmod( pszFileName, 0, 0 ) != -1;
#     else
         unsigned int iAttr = 0;
         fExist = _dos_getfileattr( pszFileName, &iAttr ) == 0;
#     endif
#  elif defined( HB_OS_OS2 )
         FILESTATUS3 fs3;
         fExist = DosQueryPathInfo( ( PCSZ ) pszFileName, FIL_STANDARD,
                                    &fs3, sizeof( fs3 ) ) == NO_ERROR;
#  elif defined( HB_OS_UNIX )
#     if defined( HB_USE_LARGEFILE64 )
         struct stat64 statbuf;
         fExist = stat64( pszFileName, &statbuf ) == 0;
#     else
         struct stat statbuf;
         fExist = stat( pszFileName, &statbuf ) == 0;
#     endif
#  else
         int iTODO; /* To force warning */
#  endif
      }

      if( pszFree )
         hb_xfree( pszFree );
#endif
   }

   return fExist;
}
예제 #6
0
파일: file.c 프로젝트: xharbour/core
BOOL hb_fsIsDirectory( const char * pszFilename )
{
   BOOL     bResult  = FALSE;
   char *   pszFree  = NULL;
   HB_SIZE  iLen;

   HB_TRACE( HB_TR_DEBUG, ( "hb_fsIsDirectory(%s)", pszFilename ) );

   pszFilename = hb_fsNameConv( pszFilename, &pszFree );

   iLen        = strlen( pszFilename );
   while( iLen && strchr( HB_OS_PATH_DELIM_CHR_LIST, pszFilename[ iLen - 1 ] ) )
      --iLen;

   if( iLen && iLen <= ( HB_PATH_MAX - 1 ) )
   {
      #if defined( HB_OS_WIN )
      {
         DWORD dAttr = GetFileAttributes( ( LPCTSTR ) pszFilename );
         bResult = ( dAttr == INVALID_FILE_ATTRIBUTES ? FALSE : ( dAttr & FILE_ATTRIBUTE_DIRECTORY ) );
      }
      #else
      {
         PHB_FFIND ffind;

         if( pszFilename[ iLen ] )
         {
            if( pszFree )
               pszFree[ iLen ] = '\0';
            else
               pszFilename = pszFree = hb_strndup( pszFilename, iLen );
         }

         if( ( ffind = hb_fsFindFirst( pszFilename, HB_FA_DIRECTORY ) ) != NULL )
         {
            if( ( ffind->attr & HB_FA_DIRECTORY ) == HB_FA_DIRECTORY )
               bResult = TRUE;
            hb_fsFindClose( ffind );
         }
      }
      #endif
   }
   hb_fsSetError( 0 );

   if( pszFree )
      hb_xfree( pszFree );

   return bResult;
}
예제 #7
0
FILE * hb_fopen( const char * path, const char * mode )
{
   char * pszFree = NULL;
   FILE * file;

   path = hb_fsNameConv( path, &pszFree );
#if defined( _MSC_VER ) && _MSC_VER >= 1400 && ! defined( _CRT_SECURE_NO_WARNINGS )
   fopen_s( &file, path, mode );
#else
   file = fopen( path, mode );
#endif
   if( pszFree )
      hb_xfree( pszFree );

   return file;
}
예제 #8
0
파일: file.c 프로젝트: xharbour/core
BOOL hb_fsFile( const char * pszFilename )
{
   PHB_FFIND   ffind;
   char *      pszFree;
   BOOL        bResult = FALSE;
   HB_SIZE     iFileName;

   HB_TRACE( HB_TR_DEBUG, ( "hb_fsFile(%s)", pszFilename ) );

   pszFilename = hb_fsNameConv( pszFilename, &pszFree );

   iFileName   = strlen( pszFilename );
   if( iFileName && pszFilename[ iFileName - 1 ] != HB_OS_PATH_DELIM_CHR ) // A directory cannot possibly be a FILE
   {                                                                    // so only do this is the last char is not
                                                                           // a directory separator character
      if( ( ffind = hb_fsFindFirst( pszFilename, HB_FA_ALL ) ) != NULL )
      {
         if( ( ffind->attr & HB_FA_DIRECTORY ) != HB_FA_DIRECTORY ) // If it's not a directory it's a file
         {
            bResult = TRUE;
         }
         else if( strchr( pszFilename, '*' ) || strchr( pszFilename, '?' ) ) // Clipper compatibility
         {                                                                  // FindFirst may have found a directory first
            while( ! bResult && hb_fsFindNext( ffind ) )
            {
               bResult = ( ( ffind->attr & HB_FA_DIRECTORY ) != HB_FA_DIRECTORY );
            }
         }
         hb_fsFindClose( ffind );
      }
   }

   hb_fsSetError( 0 );
   if( pszFree )
      hb_xfree( pszFree );

   return bResult;
}
예제 #9
0
파일: hbfsapi.c 프로젝트: jiangxilong/core
HB_ULONG hb_fsOS2DosOpenL( const char * pszFileName,
                           HB_FHANDLE * pHFile, HB_ULONG * pulAction,
                           HB_FOFFSET nInitSize, HB_ULONG ulAttribute,
                           HB_ULONG fsOpenFlags, HB_ULONG fsOpenMode )
{
   char * pszFree;
   HFILE hFile = ( HFILE ) -1;
   APIRET ret;

   pszFileName = hb_fsNameConv( pszFileName, &pszFree );
   if( hb_isWSeB() )
      /* if other process open file using DosOpen() then it will block
         long file support for us, we can block other processes against
         using DosOpen() by setting OPEN_SHARE_DENYLEGACY in fsOpenMode.
         Is it good idea? [druzus] */
      ret = s_DosOpenL( ( PSZ ) pszFileName, &hFile, pulAction,
                        ( LONGLONG ) nInitSize, ulAttribute,
                        fsOpenFlags, fsOpenMode, NULL );
   else
      ret = DosOpen( ( PSZ ) pszFileName, &hFile, pulAction,
                     ( ULONG ) nInitSize, ulAttribute,
                     fsOpenFlags, fsOpenMode, NULL );

   /* Hack to make error reporting more DOS compatible, anyhow I'm
      not sure it's good idea to have it [druzus] */
   if( ret == ERROR_OPEN_FAILED )
      ret = ERROR_FILE_NOT_FOUND;

   hb_fsSetError( ( HB_ERRCODE ) ret );
   if( pszFree )
      hb_xfree( pszFree );

   *pHFile = ret == NO_ERROR ? ( HB_FHANDLE ) hFile : FS_ERROR;

   return ret;
}
예제 #10
0
HB_FOFFSET hb_fsFSize( const char * pszFileName, HB_BOOL bUseDirEntry )
{
    if( bUseDirEntry )
    {
#if defined( HB_OS_WIN )
        typedef BOOL ( WINAPI * _HB_GETFILEATTRIBUTESEX )( LPCTSTR, GET_FILEEX_INFO_LEVELS, LPVOID );
        static _HB_GETFILEATTRIBUTESEX s_pGetFileAttributesEx = ( _HB_GETFILEATTRIBUTESEX ) -1;

        if( s_pGetFileAttributesEx == ( _HB_GETFILEATTRIBUTESEX ) -1 )
        {
            HMODULE hModule = GetModuleHandle( TEXT( "kernel32.dll" ) );
            if( hModule )
                s_pGetFileAttributesEx = ( _HB_GETFILEATTRIBUTESEX )
                                         HB_WINAPI_GETPROCADDRESST( hModule, "GetFileAttributesEx" );
            else
                s_pGetFileAttributesEx = NULL;
        }

        if( s_pGetFileAttributesEx )
        {
            LPCTSTR lpFileName;
            LPTSTR lpFree;
            WIN32_FILE_ATTRIBUTE_DATA attrex;
            HB_BOOL fResult;

            lpFileName = HB_FSNAMECONV( pszFileName, &lpFree );
            memset( &attrex, 0, sizeof( attrex ) );
            fResult = s_pGetFileAttributesEx( lpFileName, GetFileExInfoStandard, &attrex ) &&
                      ( attrex.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0;
            hb_fsSetIOError( fResult, 0 );
            if( lpFree )
                hb_xfree( lpFree );
            if( fResult )
                return ( HB_FOFFSET ) attrex.nFileSizeLow +
                       ( ( HB_FOFFSET ) attrex.nFileSizeHigh << 32 );
        }
        else
        {
            PHB_FFIND ffind = hb_fsFindFirst( pszFileName, HB_FA_ALL );
            hb_fsSetIOError( ffind != NULL, 0 );
            if( ffind )
            {
                HB_FOFFSET size = ffind->size;
                hb_fsFindClose( ffind );
                return size;
            }
        }
#elif defined( HB_USE_LARGEFILE64 )
        char * pszFree;
        HB_BOOL fResult;
        struct stat64 statbuf;
        pszFileName = hb_fsNameConv( pszFileName, &pszFree );
        statbuf.st_size = 0;
        hb_vmUnlock();
        fResult = stat64( pszFileName, &statbuf ) == 0;
        hb_fsSetIOError( fResult, 0 );
        hb_vmLock();
        if( pszFree )
            hb_xfree( pszFree );
        if( fResult )
            return ( HB_FOFFSET ) statbuf.st_size;
#else
        char * pszFree;
        HB_BOOL fResult;
        struct stat statbuf;
        pszFileName = hb_fsNameConv( pszFileName, &pszFree );
        statbuf.st_size = 0;
        hb_vmUnlock();
        fResult = stat( ( char * ) pszFileName, &statbuf ) == 0;
        hb_fsSetIOError( fResult, 0 );
        hb_vmLock();
        if( pszFree )
            hb_xfree( pszFree );
        if( fResult )
            return ( HB_FOFFSET ) statbuf.st_size;
#endif
    }
    else
    {
        HB_FHANDLE hFileHandle = hb_fsOpen( pszFileName, FO_READ | FO_COMPAT );

        if( hFileHandle != FS_ERROR )
        {
            HB_FOFFSET nPos = hb_fsSeekLarge( hFileHandle, 0, FS_END );
            hb_fsClose( hFileHandle );
            return nPos;
        }
    }

    return 0;
}
예제 #11
0
파일: mzip.c 프로젝트: ggargano/hbtest2
static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, const char * szPassword )
{
   char          szName[ HB_PATH_MAX ];
   HB_SIZE       nPos, nLen;
   char          cSep, * pString;
   unz_file_info ufi;
   int        iResult;
   HB_FHANDLE hFile;

   iResult = unzGetCurrentFileInfo( hUnzip, &ufi, szName, HB_PATH_MAX - 1,
                                    NULL, 0, NULL, 0 );
   if( iResult != UNZ_OK )
      return iResult;

   iResult = unzOpenCurrentFilePassword( hUnzip, szPassword );

   if( iResult != UNZ_OK )
      return iResult;

   if( szFileName )
      hb_strncpy( szName, szFileName, sizeof( szName ) - 1 );

   nLen = strlen( szName );

   /* Test shows that files in subfolders can be stored to zip file without
      explicitly adding folder. So, let's create a required path */

   nPos = 1;
   while( nPos < nLen )
   {
      cSep = szName[ nPos ];

      /* allow both path separators, ignore terminating path separator */
      if( ( cSep == '\\' || cSep == '/' ) && nPos < nLen - 1 )
      {
         szName[ nPos ] = '\0';
         hb_fsMkDir( szName );
         szName[ nPos ] = cSep;
      }
      nPos++;
   }

   if( ufi.external_fa & 0x40000000 ) /* DIRECTORY */
   {
      hb_fsMkDir( szName );
      iResult = UNZ_OK;
   }
   else
   {
      hFile = hb_fsCreate( szName, FC_NORMAL );

      if( hFile != FS_ERROR )
      {
         pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE );

         while( ( iResult = unzReadCurrentFile( hUnzip, pString, HB_Z_IOBUF_SIZE ) ) > 0 )
            hb_fsWriteLarge( hFile, pString, ( HB_SIZE ) iResult );

         hb_xfree( pString );

#if defined( HB_OS_WIN )
         {
            FILETIME   ftutc, ft;
            SYSTEMTIME st;

            st.wSecond       = ( WORD ) ufi.tmu_date.tm_sec;
            st.wMinute       = ( WORD ) ufi.tmu_date.tm_min;
            st.wHour         = ( WORD ) ufi.tmu_date.tm_hour;
            st.wDay          = ( WORD ) ufi.tmu_date.tm_mday;
            st.wMonth        = ( WORD ) ufi.tmu_date.tm_mon + 1;
            st.wYear         = ( WORD ) ufi.tmu_date.tm_year;
            st.wMilliseconds = 0;

            if( SystemTimeToFileTime( &st, &ft ) &&
                LocalFileTimeToFileTime( &ft, &ftutc ) )
            {
               SetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), &ftutc, &ftutc, &ftutc );
            }
         }
#endif

         hb_fsClose( hFile );
      }
      else
         iResult = -200 - hb_fsError();
   }
   unzCloseCurrentFile( hUnzip );

#if defined( HB_OS_WIN )
   {
      LPTSTR  lpFileNameFree;
      LPCTSTR lpFileName = HB_FSNAMECONV( szName, &lpFileNameFree );

      SetFileAttributes( ( LPCTSTR ) lpFileName, ufi.external_fa & 0xFF );

      if( lpFileNameFree )
         hb_xfree( lpFileNameFree );
   }
#elif defined( HB_OS_UNIX ) || defined( __DJGPP__ )
   {
      struct utimbuf utim;
      struct tm      st;
      time_t         tim;

      char *       pszFree;
      const char * szNameOS = hb_fsNameConv( szName, &pszFree );

#  if defined( __DJGPP__ )
      _chmod( szNameOS, 1, ufi.external_fa & 0xFF );
#  else
      HB_FATTR ulAttr = ufi.external_fa;

      if( ( ulAttr & 0xFFFF0000 ) == 0 )
         ulAttr = hb_translateExtAttr( szName, ulAttr );

      chmod( szNameOS,
             ( ( ulAttr & 0x00010000 ) ? S_IXOTH : 0 ) |
             ( ( ulAttr & 0x00020000 ) ? S_IWOTH : 0 ) |
             ( ( ulAttr & 0x00040000 ) ? S_IROTH : 0 ) |
             ( ( ulAttr & 0x00080000 ) ? S_IXGRP : 0 ) |
             ( ( ulAttr & 0x00100000 ) ? S_IWGRP : 0 ) |
             ( ( ulAttr & 0x00200000 ) ? S_IRGRP : 0 ) |
             ( ( ulAttr & 0x00400000 ) ? S_IXUSR : 0 ) |
             ( ( ulAttr & 0x00800000 ) ? S_IWUSR : 0 ) |
             ( ( ulAttr & 0x01000000 ) ? S_IRUSR : 0 ) );
#  endif
      memset( &st, 0, sizeof( st ) );

      st.tm_sec  = ufi.tmu_date.tm_sec;
      st.tm_min  = ufi.tmu_date.tm_min;
      st.tm_hour = ufi.tmu_date.tm_hour;
      st.tm_mday = ufi.tmu_date.tm_mday;
      st.tm_mon  = ufi.tmu_date.tm_mon;
      st.tm_year = ufi.tmu_date.tm_year - 1900;

      tim = mktime( &st );
#  if defined( HB_HAS_LOCALTIME_R )
      gmtime_r( &tim, &st );
#  else
      st = *gmtime( &tim );
#  endif
      utim.actime = utim.modtime = mktime( &st );

      utime( szNameOS, &utim );

      if( pszFree )
         hb_xfree( pszFree );
   }
#elif defined( HB_OS_DOS )
   {
#  if defined( __RSX32__ ) || defined( __GNUC__ )
      char * pszFree;

      _chmod( hb_fsNameConv( szName, &pszFree ), 1, ufi.external_fa & 0xFF );

      if( pszFree )
         hb_xfree( pszFree );
#  else
      hb_fsSetAttr( szName, ufi.external_fa & 0xFF );
#  endif
   }

#elif defined( HB_OS_OS2 )
   {
      FILESTATUS3 fs3;
      APIRET      ulrc;
      HB_FATTR    ulAttr = FILE_NORMAL;
      int         iAttr  = ufi.external_fa & 0xFF;

      char *       pszFree;
      const char * szNameOS = hb_fsNameConv( szName, &pszFree );

      if( iAttr & HB_FA_READONLY )
         ulAttr |= FILE_READONLY;
      if( iAttr & HB_FA_HIDDEN )
         ulAttr |= FILE_HIDDEN;
      if( iAttr & HB_FA_SYSTEM )
         ulAttr |= FILE_SYSTEM;
      if( iAttr & HB_FA_ARCHIVE )
         ulAttr |= FILE_ARCHIVED;

      ulrc = DosQueryPathInfo( ( PCSZ ) szNameOS, FIL_STANDARD, &fs3, sizeof( fs3 ) );

      if( ulrc == NO_ERROR )
      {
         FDATE fdate;
         FTIME ftime;

         fdate.year    = ufi.tmu_date.tm_year - 1980;
         fdate.month   = ufi.tmu_date.tm_mon;
         fdate.day     = ufi.tmu_date.tm_mday;
         ftime.hours   = ufi.tmu_date.tm_hour;
         ftime.minutes = ufi.tmu_date.tm_min;
         ftime.twosecs = ufi.tmu_date.tm_sec / 2;

         fs3.attrFile = ulAttr;

         fs3.fdateCreation = fs3.fdateLastAccess = fs3.fdateLastWrite = fdate;
         fs3.ftimeCreation = fs3.ftimeLastAccess = fs3.ftimeLastWrite = ftime;
         ulrc = DosSetPathInfo( ( PCSZ ) szNameOS, FIL_STANDARD,
                                &fs3, sizeof( fs3 ), DSPI_WRTTHRU );
      }

      if( pszFree )
         hb_xfree( pszFree );
   }
#else
   {
      hb_fsSetAttr( szName, ufi.external_fa );
   }
#endif

   return iResult;
}
예제 #12
0
파일: disksphb.c 프로젝트: jiangxilong/core
double hb_fsDiskSpace( const char * pszPath, HB_USHORT uiType )
{
   char szPathBuf[ 2 ];
   double dSpace = 0.0;

   if( uiType > HB_DISK_TOTAL )
      uiType = HB_DISK_AVAIL;

   if( ! pszPath )
   {
      szPathBuf[ 0 ] = HB_OS_PATH_DELIM_CHR;
      szPathBuf[ 1 ] = '\0';
      pszPath = szPathBuf;
   }

#if defined( HB_OS_WIN )
   {
      LPCTSTR lpPath;
      LPTSTR lpFree;

      lpPath = HB_FSNAMECONV( pszPath, &lpFree );

      {
         UINT uiErrMode = SetErrorMode( SEM_FAILCRITICALERRORS );
         HB_BOOL fResult;

#if ! defined( HB_OS_WIN_CE ) && ! defined( HB_OS_WIN_64 )
         /* NOTE: We need to call this function dynamically to maintain support
                  Win95 first edition. It was introduced in Win95B (aka OSR2) [vszakats] */
         typedef BOOL ( WINAPI * P_GDFSE )( LPCTSTR, PULARGE_INTEGER,
                                            PULARGE_INTEGER, PULARGE_INTEGER );
         static P_GDFSE s_pGetDiskFreeSpaceEx = NULL;
         static HB_BOOL s_fInit = HB_FALSE;

         if( ! s_fInit )
         {
            HMODULE hModule = GetModuleHandle( HB_WINAPI_KERNEL32_DLL() );
            if( hModule )
               s_pGetDiskFreeSpaceEx = ( P_GDFSE )
                  HB_WINAPI_GETPROCADDRESST( hModule, "GetDiskFreeSpaceEx" );
            s_fInit = HB_TRUE;
         }

         if( ! s_pGetDiskFreeSpaceEx )
         {
            DWORD dwSectorsPerCluster;
            DWORD dwBytesPerSector;
            DWORD dwNumberOfFreeClusters;
            DWORD dwTotalNumberOfClusters;

            fResult = GetDiskFreeSpace( lpPath,
                                        &dwSectorsPerCluster,
                                        &dwBytesPerSector,
                                        &dwNumberOfFreeClusters,
                                        &dwTotalNumberOfClusters ) ? HB_TRUE : HB_FALSE;
            hb_fsSetIOError( fResult, 0 );

            if( fResult )
            {
               switch( uiType )
               {
                  case HB_DISK_AVAIL:
                  case HB_DISK_FREE:
                     dSpace = ( double ) dwNumberOfFreeClusters *
                              ( double ) dwSectorsPerCluster *
                              ( double ) dwBytesPerSector;
                     break;

                  case HB_DISK_USED:
                  case HB_DISK_TOTAL:
                     dSpace = ( double ) dwTotalNumberOfClusters *
                              ( double ) dwSectorsPerCluster *
                              ( double ) dwBytesPerSector;

                     if( uiType == HB_DISK_USED )
                        dSpace -= ( double ) dwNumberOfFreeClusters *
                                  ( double ) dwSectorsPerCluster *
                                  ( double ) dwBytesPerSector;
                     break;
               }
            }
         }
         else
#endif
         {
#if defined( _MSC_VER ) || defined( __LCC__ ) || \
    ( defined( __GNUC__ ) && ! defined( __RSXNT__ ) )

#  define HB_GET_LARGE_UINT( v )  ( ( double ) (v).LowPart + \
                                    ( double ) (v).HighPart * \
                                    ( ( ( double ) 0xFFFFFFFF ) + 1 ) )

#else
   /* NOTE: Borland doesn't seem to deal with the un-named
            struct that is part of ULARGE_INTEGER
            [pt] */
#  define HB_GET_LARGE_UINT( v )  ( ( double ) (v).u.LowPart + \
                                    ( double ) (v).u.HighPart * \
                                    ( ( ( double ) 0xFFFFFFFF ) + 1 ) )
#endif

            ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;

#if ! defined( HB_OS_WIN_CE ) && ! defined( HB_OS_WIN_64 )
            fResult = s_pGetDiskFreeSpaceEx( lpPath,
                                             ( PULARGE_INTEGER ) &i64FreeBytesToCaller,
                                             ( PULARGE_INTEGER ) &i64TotalBytes,
                                             ( PULARGE_INTEGER ) &i64FreeBytes );
#else
            fResult = GetDiskFreeSpaceEx( lpPath,
                                          ( PULARGE_INTEGER ) &i64FreeBytesToCaller,
                                          ( PULARGE_INTEGER ) &i64TotalBytes,
                                          ( PULARGE_INTEGER ) &i64FreeBytes );
#endif
            hb_fsSetIOError( fResult, 0 );

            if( fResult )
            {
               switch( uiType )
               {
                  case HB_DISK_AVAIL:
                     dSpace = HB_GET_LARGE_UINT( i64FreeBytesToCaller );
                     break;

                  case HB_DISK_FREE:
                     dSpace = HB_GET_LARGE_UINT( i64FreeBytes );
                     break;

                  case HB_DISK_TOTAL:
                     dSpace = HB_GET_LARGE_UINT( i64TotalBytes );
                     break;

                  case HB_DISK_USED:
                     dSpace = HB_GET_LARGE_UINT( i64TotalBytes ) -
                              HB_GET_LARGE_UINT( i64FreeBytes );
                     break;
               }
            }
         }
         SetErrorMode( uiErrMode );
      }
      if( lpFree )
         hb_xfree( lpFree );
   }
#elif defined( HB_OS_DOS ) || defined( HB_OS_OS2 )
   {
      HB_USHORT uiDrive;

      uiDrive = pszPath == NULL || pszPath[ 0 ] == 0 ||
                pszPath[ 1 ] != HB_OS_DRIVE_DELIM_CHR ? 0 :
                ( pszPath[ 0 ] >= 'A' && pszPath[ 0 ] <= 'Z' ?
                  pszPath[ 0 ] - 'A' + 1 :
                ( pszPath[ 0 ] >= 'a' && pszPath[ 0 ] <= 'z' ?
                  pszPath[ 0 ] - 'a' + 1 : 0 ) );
#if defined( HB_OS_DOS )
      for( ;; )
      {
         union REGS regs;

         regs.HB_XREGS.dx = uiDrive;
         regs.h.ah = 0x36;
         HB_DOS_INT86( 0x21, &regs, &regs );

         if( regs.HB_XREGS.ax != 0xFFFF )
         {
            HB_USHORT uiClusterTotal  = regs.HB_XREGS.dx;
            HB_USHORT uiClusterFree   = regs.HB_XREGS.bx;
            HB_USHORT uiSecPerCluster = regs.HB_XREGS.ax;
            HB_USHORT uiSectorSize    = regs.HB_XREGS.cx;

            switch( uiType )
            {
               case HB_DISK_AVAIL:
               case HB_DISK_FREE:
                  dSpace = ( double ) uiClusterFree *
                           ( double ) uiSecPerCluster *
                           ( double ) uiSectorSize;
                  break;

               case HB_DISK_USED:
               case HB_DISK_TOTAL:
                  dSpace = ( double ) uiClusterTotal *
                           ( double ) uiSecPerCluster *
                           ( double ) uiSectorSize;

                  if( uiType == HB_DISK_USED )
                     dSpace -= ( double ) uiClusterFree *
                               ( double ) uiSecPerCluster *
                               ( double ) uiSectorSize;
                  break;
            }
         }
         else
         {
            if( hb_errRT_BASE_Ext1( EG_OPEN, 2018, NULL, NULL, 0, ( EF_CANDEFAULT | EF_CANRETRY ), HB_ERR_ARGS_BASEPARAMS ) == E_RETRY )
               continue;
         }
         break;
      }
#else /* HB_OS_OS2 */
      {
         struct _FSALLOCATE fsa;
         APIRET rc;
         /* Query level 1 info from filesystem */
         while( ( rc = DosQueryFSInfo( uiDrive, 1, &fsa, sizeof( fsa ) ) ) != NO_ERROR )
         {
            if( hb_errRT_BASE_Ext1( EG_OPEN, 2018, NULL, NULL, 0, ( EF_CANDEFAULT | EF_CANRETRY ), HB_ERR_ARGS_BASEPARAMS ) != E_RETRY )
               break;
         }

         hb_fsSetError( ( HB_ERRCODE ) rc );

         if( rc == NO_ERROR )
         {
            switch( uiType )
            {
               case HB_DISK_AVAIL:
               case HB_DISK_FREE:
                  dSpace = ( double ) fsa.cUnitAvail *
                           ( double ) fsa.cSectorUnit *
                           ( double ) fsa.cbSector;
                  break;

               case HB_DISK_USED:
               case HB_DISK_TOTAL:
                  dSpace = ( double ) fsa.cUnit *
                           ( double ) fsa.cSectorUnit *
                           ( double ) fsa.cbSector;

                  if( uiType == HB_DISK_USED )
                     dSpace -= ( double ) fsa.cUnitAvail *
                               ( double ) fsa.cSectorUnit *
                               ( double ) fsa.cbSector;
                  break;
            }
         }
      }
#endif
   }

#elif defined( HB_OS_UNIX ) && \
      !( defined( __WATCOMC__ ) || defined( __CEGCC__ ) || defined( HB_OS_SYMBIAN ) )
   {
#if defined( HB_OS_DARWIN ) || defined( HB_OS_ANDROID ) || \
    defined( HB_OS_VXWORKS )
      struct statfs sf;
#else
      struct statvfs sf;
#endif
      char * pszFree;

      pszPath = hb_fsNameConv( pszPath, &pszFree );

#if defined( HB_OS_DARWIN ) || defined( HB_OS_ANDROID ) || \
    defined( HB_OS_VXWORKS )
      if( statfs( pszPath, &sf ) == 0 )
#else
      if( statvfs( pszPath, &sf ) == 0 )
#endif
      {
         switch( uiType )
         {
            case HB_DISK_AVAIL:
               dSpace = ( double ) sf.f_bavail * ( double ) sf.f_bsize;
               break;

            case HB_DISK_FREE:
               dSpace = ( double ) sf.f_bfree * ( double ) sf.f_bsize;
               break;

            case HB_DISK_USED:
               dSpace = ( double ) ( sf.f_blocks - sf.f_bfree ) *
                        ( double ) sf.f_bsize;
               break;

            case HB_DISK_TOTAL:
               dSpace = ( double ) sf.f_blocks * ( double ) sf.f_bsize;
               break;
         }
         hb_fsSetIOError( HB_TRUE, 0 );
      }
      else
         hb_fsSetIOError( HB_FALSE, 0 );

      if( pszFree )
         hb_xfree( pszFree );
   }
#else
   {
      int iTODO;

      HB_SYMBOL_UNUSED( uiType );
   }
#endif

   return dSpace;
}
예제 #13
0
파일: mzip.c 프로젝트: ggargano/hbtest2
static int hb_zipDeleteFile( const char * szZipFile, const char * szFileMask )
{
   char            szTempFile[ HB_PATH_MAX ];
   char            szCurrFile[ HB_PATH_MAX ];
   PHB_FNAME       pFileName;
   HB_FHANDLE      hFile;
   unzFile         hUnzip;
   zipFile         hZip;
   unz_global_info ugi;
   unz_file_info   ufi;
   zip_fileinfo    zfi;
   char *          pszGlobalComment = NULL;
   char *          pszFileComment   = NULL;
   void *          pExtraField      = NULL;
   void *          pLocalExtraField = NULL;
   int    iFilesLeft = 0;
   int    iFilesDel  = 0;
   int    iExtraFieldLen;
   int    method;
   int    level;
   int    iResult;
   char * pszFree;

   /* open source file */
   hUnzip = unzOpen( hb_fsNameConv( szZipFile, &pszFree ) );

   if( pszFree )
      hb_xfree( pszFree );

   if( hUnzip == NULL )
      return UNZ_ERRNO;

   pFileName = hb_fsFNameSplit( szZipFile );
   hFile     = hb_fsCreateTemp( pFileName->szPath, NULL, FC_NORMAL, szTempFile );
   hZip      = NULL;
   if( hFile != FS_ERROR )
   {
      hb_fsClose( hFile );
      hZip = zipOpen( szTempFile, APPEND_STATUS_CREATE );
   }
   hb_xfree( pFileName );

   if( hZip == NULL )
   {
      unzClose( hUnzip );
      return UNZ_ERRNO;
   }

   iResult = unzGetGlobalInfo( hUnzip, &ugi );
   if( iResult == UNZ_OK )
   {
      if( ugi.size_comment > 0 )
      {
         pszGlobalComment = ( char * ) hb_xgrab( ugi.size_comment + 1 );
         if( ( uLong ) unzGetGlobalComment( hUnzip, pszGlobalComment,
                                            ugi.size_comment ) != ugi.size_comment )
            iResult = UNZ_ERRNO;
         pszGlobalComment[ ugi.size_comment ] = '\0';
      }
      if( iResult == UNZ_OK )
         iResult = unzGoToFirstFile( hUnzip );
   }

   while( iResult == UNZ_OK )
   {
      iResult = unzGetCurrentFileInfo( hUnzip, &ufi, szCurrFile, HB_PATH_MAX - 1, NULL, 0, NULL, 0 );
      if( iResult != UNZ_OK )
         break;

      if( hb_strMatchFile( szCurrFile, szFileMask ) )
         iFilesDel++;
      else
      {
         if( ufi.size_file_extra )
            pExtraField = ( char * ) hb_xgrab( ufi.size_file_extra );
         if( ufi.size_file_comment )
            pszFileComment = ( char * ) hb_xgrab( ufi.size_file_comment + 1 );

         iResult = unzGetCurrentFileInfo( hUnzip, &ufi, NULL, 0,
                                          pExtraField, ufi.size_file_extra,
                                          pszFileComment, ufi.size_file_comment );
         if( pszFileComment )
            pszFileComment[ ufi.size_file_comment ] = '\0';
         if( iResult != UNZ_OK )
            break;

         iResult = unzOpenCurrentFile2( hUnzip, &method, &level, 1 );
         if( iResult != UNZ_OK )
            break;

         iExtraFieldLen = unzGetLocalExtrafield( hUnzip, NULL, 0 );
         if( iExtraFieldLen < 0 )
         {
            iResult = UNZ_ERRNO;
            break;
         }
         else if( iExtraFieldLen > 0 )
         {
            pLocalExtraField = hb_xgrab( iExtraFieldLen );
            if( unzGetLocalExtrafield( hUnzip, pLocalExtraField, iExtraFieldLen ) != iExtraFieldLen )
            {
               iResult = UNZ_ERRNO;
               break;
            }
         }

         memset( &zfi, 0, sizeof( zfi ) );
         memcpy( &zfi.tmz_date, &ufi.tmu_date, sizeof( tm_unz ) );
         zfi.dosDate     = ufi.dosDate;
         zfi.internal_fa = ufi.internal_fa;
         zfi.external_fa = ufi.external_fa;

         iResult = zipOpenNewFileInZip2( hZip, szCurrFile, &zfi, pLocalExtraField, iExtraFieldLen, pExtraField, ufi.size_file_extra, pszFileComment, method, level, 1 );
         if( iResult != UNZ_OK )
            break;

         if( ufi.compressed_size )
         {
            void * buffer = hb_xgrab( HB_Z_IOBUF_SIZE );
            uLong  ulLeft = ufi.compressed_size;

            while( ulLeft > 0 )
            {
               int iRead = HB_MIN( ulLeft, HB_Z_IOBUF_SIZE );
               iResult = unzReadCurrentFile( hUnzip, ( voidp ) buffer, iRead );
               if( iResult < 0 )
                  break;
               if( iResult != iRead )
               {
                  iResult = UNZ_ERRNO;
                  break;
               }
               iResult = zipWriteInFileInZip( hZip, ( voidp ) buffer, iRead );
               if( iResult != UNZ_OK )
                  break;
               ulLeft -= iRead;
            }
            hb_xfree( buffer );
            if( iResult != UNZ_OK )
               break;
         }

         iResult = zipCloseFileInZipRaw( hZip, ufi.uncompressed_size, ufi.crc );
         if( iResult != UNZ_OK )
            break;

         iResult = unzCloseCurrentFile( hUnzip );
         if( iResult != UNZ_OK )
            break;

         if( pExtraField )
         {
            hb_xfree( pExtraField );
            pExtraField = NULL;
         }
         if( pszFileComment )
         {
            hb_xfree( pszFileComment );
            pszFileComment = NULL;
         }
         if( pLocalExtraField )
         {
            hb_xfree( pLocalExtraField );
            pLocalExtraField = NULL;
         }
         iFilesLeft++;
      }
      iResult = unzGoToNextFile( hUnzip );
   }

   if( pExtraField )
      hb_xfree( pExtraField );
   if( pszFileComment )
      hb_xfree( pszFileComment );
   if( pLocalExtraField )
      hb_xfree( pLocalExtraField );

   if( iFilesDel == 0 )
      iResult = UNZ_ERRNO;
   else if( iResult == UNZ_END_OF_LIST_OF_FILE )
      iResult = UNZ_OK;

   if( iResult != UNZ_OK )
      zipClose( hZip, NULL );
   else
      iResult = zipClose( hZip, pszGlobalComment );
   unzClose( hUnzip );
   if( pszGlobalComment )
      hb_xfree( pszGlobalComment );

   if( iResult != UNZ_OK )
      hb_fsDelete( szTempFile );
   else
   {
      hb_fsDelete( szZipFile );

      if( iFilesLeft == 0 )
         hb_fsDelete( szTempFile );
      else if( ! hb_fsRename( szTempFile, szZipFile ) )
         iResult = UNZ_ERRNO;
   }

   return iResult;
}
예제 #14
0
파일: mzip.c 프로젝트: ggargano/hbtest2
static int hb_zipStoreFile( zipFile hZip, const char * szFileName, const char * szName, const char * szPassword, const char * szComment )
{
   char *       szZipName, * pString;
   HB_FHANDLE   hFile;
   HB_SIZE      nLen;
   HB_FATTR     ulExtAttr;
   zip_fileinfo zfi;
   int          iResult;
   HB_BOOL      fError;
   HB_BOOL      fText;
   HB_U32       ulCRC;

   if( szName )
   {
      /* change path separators to '/' */
      szZipName = hb_strdup( szName );

      nLen    = strlen( szZipName );
      pString = szZipName;
      while( nLen-- )
      {
         if( pString[ nLen ] == '\\' )
            pString[ nLen ] = '/';
      }
   }
   else
   {
      /* get file name */
      szZipName = hb_strdup( szFileName );

      nLen    = strlen( szZipName );
      pString = szZipName;

      while( nLen-- )
      {
         if( pString[ nLen ] == '/' || pString[ nLen ] == '\\' )
         {
            memmove( szZipName, &pString[ nLen + 1 ], strlen( szZipName ) - nLen );
            break;
         }
      }
   }

   memset( &zfi, 0, sizeof( zfi ) );
   fError    = HB_FALSE;
   ulExtAttr = 0;

#if defined( HB_OS_WIN )
   {
      LPTSTR  lpFileNameFree;
      LPCTSTR lpFileName = HB_FSNAMECONV( szFileName, &lpFileNameFree );
      DWORD   attr       = GetFileAttributes( lpFileName );

      if( attr != INVALID_FILE_ATTRIBUTES )
      {
         ulExtAttr = hb_translateExtAttr( szFileName, attr &
                                          ( FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                                            FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY |
                                            FILE_ATTRIBUTE_ARCHIVE ) );
      }
      else
         fError = HB_TRUE;

      if( lpFileNameFree )
         hb_xfree( lpFileNameFree );
   }
#elif defined( HB_OS_UNIX )
   {
      struct stat statbuf;
      struct tm   st;
      char *      pszFree;

      if( stat( hb_fsNameConv( szFileName, &pszFree ), &statbuf ) == 0 )
      {
         if( S_ISDIR( statbuf.st_mode ) )
         {
            ulExtAttr |= 0x40000000;
            ulExtAttr |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
         }
         else
         {
            ulExtAttr |= 0x80000000;
            ulExtAttr |= 0x20; /* FILE_ATTRIBUTE_ARCHIVE */
         }

         ulExtAttr |= ( ( statbuf.st_mode & S_IXOTH ) ? 0x00010000 : 0 ) |
                      ( ( statbuf.st_mode & S_IWOTH ) ? 0x00020000 : 0 ) |
                      ( ( statbuf.st_mode & S_IROTH ) ? 0x00040000 : 0 ) |
                      ( ( statbuf.st_mode & S_IXGRP ) ? 0x00080000 : 0 ) |
                      ( ( statbuf.st_mode & S_IWGRP ) ? 0x00100000 : 0 ) |
                      ( ( statbuf.st_mode & S_IRGRP ) ? 0x00200000 : 0 ) |
                      ( ( statbuf.st_mode & S_IXUSR ) ? 0x00400000 : 0 ) |
                      ( ( statbuf.st_mode & S_IWUSR ) ? 0x00800000 : 0 ) |
                      ( ( statbuf.st_mode & S_IRUSR ) ? 0x01000000 : 0 );

#  if defined( HB_HAS_LOCALTIME_R )
         localtime_r( &statbuf.st_mtime, &st );
#  else
         st = *localtime( &statbuf.st_mtime );
#  endif

         zfi.tmz_date.tm_sec  = st.tm_sec;
         zfi.tmz_date.tm_min  = st.tm_min;
         zfi.tmz_date.tm_hour = st.tm_hour;
         zfi.tmz_date.tm_mday = st.tm_mday;
         zfi.tmz_date.tm_mon  = st.tm_mon;
         zfi.tmz_date.tm_year = st.tm_year;
      }
      else
         fError = HB_TRUE;

      if( pszFree )
         hb_xfree( pszFree );
   }
#elif defined( HB_OS_DOS )
   {
#  if defined( __DJGPP__ ) || defined( __RSX32__ ) || defined( __GNUC__ )
      int    attr;
      char * pszFree;

      attr = _chmod( hb_fsNameConv( szFileName, &pszFree ), 0, 0 );

      if( pszFree )
         hb_xfree( pszFree );

      if( attr != -1 )
#  else
      HB_FATTR attr;

      if( hb_fsGetAttr( szFileName, &attr ) )
#  endif
      {
         ulExtAttr = attr & ( HB_FA_READONLY | HB_FA_HIDDEN | HB_FA_SYSTEM |
                              HB_FA_DIRECTORY | HB_FA_ARCHIVE );

         ulExtAttr = hb_translateExtAttr( szFileName, ulExtAttr );
      }
      else
         fError = HB_TRUE;
   }
#elif defined( HB_OS_OS2 )
   {
      FILESTATUS3 fs3;
      APIRET      ulrc;
      HB_FATTR    ulAttr;
      char *      pszFree;

      ulrc = DosQueryPathInfo( ( PCSZ ) hb_fsNameConv( szFileName, &pszFree ), FIL_STANDARD, &fs3, sizeof( fs3 ) );

      if( pszFree )
         hb_xfree( pszFree );

      if( ulrc == NO_ERROR )
      {
         ulAttr = 0;
         if( fs3.attrFile & FILE_READONLY )
            ulAttr |= HB_FA_READONLY;
         if( fs3.attrFile & FILE_HIDDEN )
            ulAttr |= HB_FA_HIDDEN;
         if( fs3.attrFile & FILE_SYSTEM )
            ulAttr |= HB_FA_SYSTEM;
         if( fs3.attrFile & FILE_DIRECTORY )
            ulAttr |= HB_FA_DIRECTORY;
         if( fs3.attrFile & FILE_ARCHIVED )
            ulAttr |= HB_FA_ARCHIVE;

         ulExtAttr = hb_translateExtAttr( szFileName, ulAttr );

         zfi.tmz_date.tm_sec  = fs3.ftimeLastWrite.twosecs * 2;
         zfi.tmz_date.tm_min  = fs3.ftimeLastWrite.minutes;
         zfi.tmz_date.tm_hour = fs3.ftimeLastWrite.hours;
         zfi.tmz_date.tm_mday = fs3.fdateLastWrite.day;
         zfi.tmz_date.tm_mon  = fs3.fdateLastWrite.month;
         zfi.tmz_date.tm_year = fs3.fdateLastWrite.year + 1980;
      }
      else
         fError = HB_TRUE;
   }
#else
   {
      HB_FATTR attr;

      if( ! hb_fsGetAttr( szFileName, &attr ) )
         ulExtAttr = 0x81B60020;  /* FILE_ATTRIBUTE_ARCHIVE | rw-rw-rw- */
      else
      {
         ulExtAttr = attr & ( HB_FA_READONLY | HB_FA_HIDDEN | HB_FA_SYSTEM |
                              HB_FA_DIRECTORY | HB_FA_ARCHIVE );

         ulExtAttr = hb_translateExtAttr( szFileName, ulExtAttr );
      }
   }
#endif

   if( fError )
   {
      hb_xfree( szZipName );
      return -200;
   }

   fText = HB_FALSE;
   ulCRC = 0;

   zfi.external_fa = ulExtAttr;
   /* TODO: zip.exe test: 0 for binary file, 1 for text. Does not depend on
      extension. We should analyse content of file to determine this??? */
   zfi.internal_fa = 0;

   if( ulExtAttr & 0x40000000 )
   {
      iResult = zipOpenNewFileInZip3( hZip, szZipName, &zfi, NULL, 0, NULL, 0, szComment,
                                      Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0,
                                      -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
                                      szPassword, ulCRC );
      if( iResult == 0 )
         zipCloseFileInZip( hZip );
   }
   else
   {
      hFile = hb_fsOpen( szFileName, FO_READ );

      if( hFile != FS_ERROR )
      {
#if defined( HB_OS_WIN )
         {
            FILETIME   ftutc, ft;
            SYSTEMTIME st;

            if( GetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), NULL, NULL, &ftutc ) &&
                FileTimeToLocalFileTime( &ftutc, &ft ) &&
                FileTimeToSystemTime( &ft, &st ) )
            {
               zfi.tmz_date.tm_sec  = st.wSecond;
               zfi.tmz_date.tm_min  = st.wMinute;
               zfi.tmz_date.tm_hour = st.wHour;
               zfi.tmz_date.tm_mday = st.wDay;
               zfi.tmz_date.tm_mon  = st.wMonth - 1;
               zfi.tmz_date.tm_year = st.wYear;
            }
         }
#endif
         if( szPassword )
         {
            if( hb_zipGetFileInfo( szFileName, &ulCRC, &fText ) )
               zfi.internal_fa = fText ? 1 : 0;
         }

         iResult = zipOpenNewFileInZip3( hZip, szZipName, &zfi, NULL, 0, NULL, 0, szComment,
                                         Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0,
                                         -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
                                         szPassword, ulCRC );
         if( iResult == 0 )
         {
            pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE );
            while( ( nLen = hb_fsReadLarge( hFile, pString, HB_Z_IOBUF_SIZE ) ) > 0 )
               zipWriteInFileInZip( hZip, pString, ( unsigned ) nLen );

            hb_xfree( pString );

            zipCloseFileInZip( hZip );
         }
         hb_fsClose( hFile );
      }
      else
         iResult = -200 - hb_fsError();
   }
   hb_xfree( szZipName );
   return iResult;
}
예제 #15
0
파일: fslink.c 프로젝트: JamesLinus/core
HB_BOOL hb_fsLink( const char * pszExisting, const char * pszNewFile )
{
   HB_BOOL fResult;

   if( pszExisting && pszNewFile )
   {
      hb_vmUnlock();

#if defined( HB_OS_WIN ) && ! defined( HB_OS_WIN_CE )
      {
         typedef BOOL ( WINAPI * _HB_CREATEHARDLINK )( LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES );

         static _HB_CREATEHARDLINK s_pCreateHardLink = NULL;

         if( ! s_pCreateHardLink )
         {
            HMODULE hModule = GetModuleHandle( TEXT( "kernel32.dll" ) );
            if( hModule )
               s_pCreateHardLink = ( _HB_CREATEHARDLINK )
                  HB_WINAPI_GETPROCADDRESST( hModule, "CreateHardLink" );
         }

         if( s_pCreateHardLink )
         {
            LPCTSTR lpFileName, lpExistingFileName;
            LPTSTR lpFileNameFree, lpExistingFileNameFree;

            lpFileName = HB_FSNAMECONV( pszNewFile, &lpFileNameFree );
            lpExistingFileName = HB_FSNAMECONV( pszExisting, &lpExistingFileNameFree );

            fResult = s_pCreateHardLink( lpFileName, lpExistingFileName, NULL ) != 0;
            hb_fsSetIOError( fResult, 0 );

            if( lpFileNameFree )
               hb_xfree( lpFileNameFree );
            if( lpExistingFileNameFree )
               hb_xfree( lpExistingFileNameFree );
         }
         else
         {
            hb_fsSetError( 1 );
            fResult = HB_FALSE;
         }
      }
#elif defined( HB_OS_UNIX )
      {
         char * pszExistingFree;
         char * pszNewFileFree;

         pszExisting = hb_fsNameConv( pszExisting, &pszExistingFree );
         pszNewFile = hb_fsNameConv( pszNewFile, &pszNewFileFree );

         fResult = ( link( pszExisting, pszNewFile ) == 0 );
         hb_fsSetIOError( fResult, 0 );

         if( pszExistingFree )
            hb_xfree( pszExistingFree );
         if( pszNewFileFree )
            hb_xfree( pszNewFileFree );
      }
#else
      {
         hb_fsSetError( 1 );
         fResult = HB_FALSE;
      }
#endif

      hb_vmLock();
   }
   else
   {
      hb_fsSetError( 2 );
      fResult = HB_FALSE;
   }

   return fResult;
}
예제 #16
0
파일: fslink.c 프로젝트: JamesLinus/core
/* NOTE: Caller must free the pointer, if not NULL */
char * hb_fsLinkRead( const char * pszFile )
{
   char * pszLink = NULL;

   if( pszFile )
   {
      hb_vmUnlock();

#if defined( HB_OS_WIN ) && ! defined( HB_OS_WIN_CE )
      {
         typedef DWORD ( WINAPI * _HB_GETFINALPATHNAMEBYHANDLE )( HANDLE, LPTSTR, DWORD, DWORD );

         static _HB_GETFINALPATHNAMEBYHANDLE s_pGetFinalPathNameByHandle = NULL;

         #ifndef VOLUME_NAME_DOS
         #define VOLUME_NAME_DOS       0x0
         #endif
         #ifndef VOLUME_NAME_GUID
         #define VOLUME_NAME_GUID      0x1
         #endif
         #ifndef VOLUME_NAME_NT
         #define VOLUME_NAME_NT        0x2
         #endif
         #ifndef VOLUME_NAME_NONE
         #define VOLUME_NAME_NONE      0x4
         #endif
         #ifndef FILE_NAME_NORMALIZED
         #define FILE_NAME_NORMALIZED  0x0
         #endif
         #ifndef FILE_NAME_OPENED
         #define FILE_NAME_OPENED      0x8
         #endif

         if( ! s_pGetFinalPathNameByHandle )
         {
            HMODULE hModule = GetModuleHandle( TEXT( "kernel32.dll" ) );
            if( hModule )
               s_pGetFinalPathNameByHandle = ( _HB_GETFINALPATHNAMEBYHANDLE )
                  HB_WINAPI_GETPROCADDRESST( hModule, "GetFinalPathNameByHandle" );
         }

         if( s_pGetFinalPathNameByHandle )
         {
            LPCTSTR lpFileName;
            LPTSTR lpFileNameFree;
            HANDLE hFile;
            DWORD dwAttr;
            HB_BOOL fDir;

            lpFileName = HB_FSNAMECONV( pszFile, &lpFileNameFree );

            dwAttr = GetFileAttributes( lpFileName );
            fDir = ( dwAttr != INVALID_FILE_ATTRIBUTES ) &&
                   ( dwAttr & FILE_ATTRIBUTE_DIRECTORY );

            hFile = CreateFile( lpFileName,
                                GENERIC_READ,
                                FILE_SHARE_READ,
                                NULL,
                                OPEN_EXISTING,
                                fDir ? ( FILE_ATTRIBUTE_DIRECTORY | FILE_FLAG_BACKUP_SEMANTICS ) : FILE_ATTRIBUTE_NORMAL,
                                NULL );

            if( hFile == INVALID_HANDLE_VALUE )
               hb_fsSetIOError( HB_FALSE, 0 );
            else
            {
               DWORD size;
               TCHAR lpLink[ HB_PATH_MAX ];
               size = s_pGetFinalPathNameByHandle( hFile, lpLink, HB_PATH_MAX, VOLUME_NAME_DOS );
               if( size < HB_PATH_MAX )
               {
                  if( size > 0 )
                  {
                     lpLink[ size ] = TEXT( '\0' );
                     pszLink = HB_OSSTRDUP( lpLink );
                  }

                  hb_fsSetIOError( HB_TRUE, 0 );
               }
               else
                  hb_fsSetError( 9 );
            }

            if( lpFileNameFree )
               hb_xfree( lpFileNameFree );
         }
         else
            hb_fsSetError( 1 );
      }
#elif defined( HB_OS_UNIX )
      {
         char * pszFileFree;
         size_t size;

         pszFile = hb_fsNameConv( pszFile, &pszFileFree );

         pszLink = ( char * ) hb_xgrab( HB_PATH_MAX + 1 );
         size = readlink( pszFile, pszLink, HB_PATH_MAX );
         hb_fsSetIOError( size != ( size_t ) -1, 0 );
         if( size == ( size_t ) -1 )
         {
            hb_xfree( pszLink );
            pszLink = NULL;
         }
         else
         {
            pszLink[ size ] = '\0';
            /* Convert from OS codepage */
            pszLink = ( char * ) HB_UNCONST( hb_osDecodeCP( pszLink, NULL, NULL ) );
         }

         if( pszFileFree )
            hb_xfree( pszFileFree );
      }
#else
      {
         hb_fsSetError( 1 );
      }
#endif

      hb_vmLock();
   }
   else
      hb_fsSetError( 2 );

   return pszLink;
}
예제 #17
0
파일: fslink.c 프로젝트: JamesLinus/core
HB_BOOL hb_fsLinkSym( const char * pszTarget, const char * pszNewFile )
{
   HB_BOOL fResult;

   if( pszTarget && pszNewFile )
   {
      hb_vmUnlock();

#if defined( HB_OS_WIN ) && ! defined( HB_OS_WIN_CE )
      {
         typedef BOOL ( WINAPI * _HB_CREATESYMBOLICLINK )( LPCTSTR, LPCTSTR, DWORD );

         static _HB_CREATESYMBOLICLINK s_pCreateSymbolicLink = NULL;

         #ifndef SYMBOLIC_LINK_FLAG_DIRECTORY
         #define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1
         #endif

         if( ! s_pCreateSymbolicLink )
         {
            HMODULE hModule = GetModuleHandle( TEXT( "kernel32.dll" ) );
            if( hModule )
               s_pCreateSymbolicLink = ( _HB_CREATESYMBOLICLINK )
                  HB_WINAPI_GETPROCADDRESST( hModule, "CreateSymbolicLink" );
         }

         if( s_pCreateSymbolicLink )
         {
            LPCTSTR lpSymlinkFileName, lpTargetFileName;
            LPTSTR lpSymlinkFileNameFree, lpTargetFileNameFree;
            DWORD dwAttr;
            HB_BOOL fDir;

            lpSymlinkFileName = HB_FSNAMECONV( pszNewFile, &lpSymlinkFileNameFree );
            lpTargetFileName = HB_FSNAMECONV( pszTarget, &lpTargetFileNameFree );

            dwAttr = GetFileAttributes( lpTargetFileName );
            fDir = ( dwAttr != INVALID_FILE_ATTRIBUTES ) &&
                   ( dwAttr & FILE_ATTRIBUTE_DIRECTORY );

            fResult = s_pCreateSymbolicLink( lpSymlinkFileName, lpTargetFileName, fDir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0 ) != 0;
            hb_fsSetIOError( fResult, 0 );

            if( lpSymlinkFileNameFree )
               hb_xfree( lpSymlinkFileNameFree );
            if( lpTargetFileNameFree )
               hb_xfree( lpTargetFileNameFree );
         }
         else
         {
            hb_fsSetError( 1 );
            fResult = HB_FALSE;
         }
      }
#elif defined( HB_OS_UNIX )
      {
         char * pszTargetFree;
         char * pszNewFileFree;

         pszTarget = hb_fsNameConv( pszTarget, &pszTargetFree );
         pszNewFile = hb_fsNameConv( pszNewFile, &pszNewFileFree );

         fResult = ( symlink( pszTarget, pszNewFile ) == 0 );
         hb_fsSetIOError( fResult, 0 );

         if( pszTargetFree )
            hb_xfree( pszTargetFree );
         if( pszNewFileFree )
            hb_xfree( pszNewFileFree );
      }
#else
      {
         hb_fsSetError( 1 );
         fResult = HB_FALSE;
      }
#endif

      hb_vmLock();
   }
   else
   {
      hb_fsSetError( 2 );
      fResult = HB_FALSE;
   }

   return fResult;
}
예제 #18
0
static HB_FHANDLE hb_fsCreateTempLow( const BYTE * pszDir, const BYTE * pszPrefix, ULONG ulAttr, BYTE * pszName, const UCHAR * pszExt )
{
   /* less attemps */
   int iAttemptLeft = 99, iLen;
   HB_FHANDLE fd;

   do
   {
      pszName[ 0 ] = '\0';

      if( pszDir && pszDir[ 0 ] != '\0' )
      {
         strncpy( ( char * ) pszName, ( char * ) pszDir, _POSIX_PATH_MAX );
      }
      else
      {
#if defined(HB_WIN32_IO)
         if( ! GetTempPathA( ( DWORD ) _POSIX_PATH_MAX, ( LPSTR ) pszName ) )
         {
            pszName[ 0 ] = '.';
            pszName[ 1 ] = '\0';
         }
#else
         char * pszTmpDir = hb_getenv( "TMPDIR" );

         if( !fsGetTempDirByCase( pszName, pszTmpDir ) )
         {
#ifdef P_tmpdir
            if( !fsGetTempDirByCase( pszName, P_tmpdir ) )
#endif
            {
               pszName[ 0 ] = '.';
               pszName[ 1 ] = '\0';
            }
         }
         if( pszTmpDir )
            hb_xfree( pszTmpDir );
#endif
      }

      if( pszName[0] != '\0' )
      {
         int len = strlen( ( char * ) pszName );
         if( pszName[ len - 1 ] != ( BYTE ) hb_set.HB_SET_DIRSEPARATOR )
         {
            pszName[ len ] = ( BYTE ) hb_set.HB_SET_DIRSEPARATOR;
            pszName[ len + 1 ] = '\0';
         }
      }

      if( pszPrefix )
         strncat( ( char * ) pszName, ( char * ) pszPrefix, _POSIX_PATH_MAX );

      iLen = ( int ) strlen( ( char * ) pszName );
      if( iLen > _POSIX_PATH_MAX - 6 )
         return FS_ERROR;

#if !defined(__WATCOMC__) && ( defined( HB_OS_LINUX ) || defined( HB_OS_BSD ) )
      if( hb_set.HB_SET_FILECASE != HB_SET_CASE_LOWER &&
          hb_set.HB_SET_FILECASE != HB_SET_CASE_UPPER &&
          hb_set.HB_SET_DIRCASE != HB_SET_CASE_LOWER &&
          hb_set.HB_SET_DIRCASE != HB_SET_CASE_UPPER &&
          pszExt == NULL )
      {
         strncat( ( char * ) pszName, "XXXXXX", _POSIX_PATH_MAX );
         fd = ( HB_FHANDLE ) mkstemp( ( char * ) pszName );
         hb_fsSetIOError( fd != ( HB_FHANDLE ) -1, 0 );
      }
      else
#endif
      {
         int i, n;
         double d = hb_random_num(), x;

         for( i = 0; i < 6; i++ )
         {
            d = d * 36;
            n = ( int ) d;
            d = modf( d, &x );
            pszName[ iLen++ ] = ( BYTE ) ( n + ( n > 9 ? 'a' - 10 : '0' ) );
         }
         pszName[ iLen ] = '\0';
         if( pszExt )
            strncat( ( char * ) pszName, ( char * ) pszExt, _POSIX_PATH_MAX );
         hb_fsNameConv( pszName, NULL );
         fd = hb_fsCreateEx( pszName, ulAttr, FO_EXCLUSIVE | FO_EXCL );
      }

      if( fd != ( HB_FHANDLE ) FS_ERROR )
         return fd;
   }
   while( --iAttemptLeft );

   return FS_ERROR;
}
예제 #19
0
파일: hbffind.c 프로젝트: jiangxilong/core
static HB_BOOL hb_fsFindNextLow( PHB_FFIND ffind )
{
   HB_BOOL bFound;

   int iYear  = 0;
   int iMonth = 0;
   int iDay   = 0;

   int iHour = 0;
   int iMin  = 0;
   int iSec  = 0;
   int iMSec = 0;

   HB_FATTR raw_attr = 0, nAttr = 0;

   /* Set the default values in case some platforms don't
      support some of these, or they may fail on them. */

   ffind->szName[ 0 ] = '\0';
   ffind->size = 0;

   /* Do platform dependant first/next search */

   hb_vmUnlock();

#if defined( HB_OS_DOS )

   {
      PHB_FFIND_INFO info = ( PHB_FFIND_INFO ) ffind->info;

      /* Handling HB_FA_LABEL doesn't need any special tricks
         under the MS-DOS platform. */

      if( ffind->bFirst )
      {
         ffind->bFirst = HB_FALSE;

         /* tzset(); */

#if defined( __WATCOMC__ )
         bFound = ( _dos_findfirst( ffind->pszFileMask, ( HB_USHORT ) hb_fsAttrToRaw( ffind->attrmask ), &info->entry ) == 0 );
#else
         bFound = ( findfirst( ffind->pszFileMask, &info->entry, ( HB_USHORT ) hb_fsAttrToRaw( ffind->attrmask ) ) == 0 );
#endif
      }
      else
      {
#if defined( __WATCOMC__ )
         bFound = ( _dos_findnext( &info->entry ) == 0 );
#else
         bFound = ( findnext( &info->entry ) == 0 );
#endif
      }

      /* Fill Harbour found file info */

      if( bFound )
      {
         hb_strncpy( ffind->szName, info->entry.ff_name, sizeof( ffind->szName ) - 1 );
         ffind->size = info->entry.ff_fsize;

         raw_attr = info->entry.ff_attrib;

         {
            time_t ftime;
            struct tm * ft;
            struct stat sStat;

            stat( info->entry.ff_name, &sStat );

            ftime = sStat.st_mtime;
            ft = localtime( &ftime );

            iYear  = ft->tm_year + 1900;
            iMonth = ft->tm_mon + 1;
            iDay   = ft->tm_mday;

            iHour  = ft->tm_hour;
            iMin   = ft->tm_min;
            iSec   = ft->tm_sec;
         }
      }
      hb_fsSetIOError( bFound, 0 );
   }

#elif defined( HB_OS_OS2 )

   {
      #define HB_OS2_DIRCNT   16

      PHB_FFIND_INFO info = ( PHB_FFIND_INFO ) ffind->info;
      APIRET ret = NO_ERROR;

      /* TODO: HB_FA_LABEL handling */

      if( ffind->bFirst )
      {
         ffind->bFirst = HB_FALSE;

         info->isWSeB = hb_isWSeB();

         info->findSize = sizeof( FILEFINDBUF3L );
         if( info->findSize & 0x07 )
            info->findSize += 0x08 - ( info->findSize & 0x07 );
         info->findSize *= HB_OS2_DIRCNT;
         if( info->findSize > 0xF000 )
            info->findSize = 0xF000;
         info->findInitCnt = ! info->isWSeB ? info->findSize / 32 : HB_OS2_DIRCNT;

         info->hFindFile = HDIR_CREATE;
         info->findCount = info->findInitCnt;
         ret = DosAllocMem( &info->entry, info->findSize, OBJ_TILE | PAG_COMMIT | PAG_WRITE );
         if( ret == NO_ERROR )
         {
            ret = DosFindFirst( ( PCSZ ) ffind->pszFileMask,
                                &info->hFindFile,
                                ( ULONG ) hb_fsAttrToRaw( ffind->attrmask ),
                                info->entry,
                                info->findSize,
                                &info->findCount,
                                FIL_STANDARDL );
            bFound = ret == NO_ERROR && info->findCount > 0;
            if( bFound )
               info->next = info->entry;
         }
         else
         {
            info->entry = NULL;
            bFound = HB_FALSE;
         }
      }
      else if( info->findCount == 0 )
      {
         info->findCount = info->findInitCnt;
         ret = DosFindNext( info->hFindFile,
                            info->entry,
                            info->findSize,
                            &info->findCount );
         bFound = ret == NO_ERROR && info->findCount > 0;
         if( bFound )
            info->next = info->entry;
      }
      else
         bFound = HB_TRUE;

      if( bFound )
      {
         ULONG oNextEntryOffset;

         if( info->isWSeB )
         {
            PFILEFINDBUF3L pFFB = ( PFILEFINDBUF3L ) info->next;

            hb_strncpy( ffind->szName, pFFB->achName, sizeof( ffind->szName ) - 1 );
            ffind->size = ( HB_FOFFSET ) pFFB->cbFile;
            raw_attr = pFFB->attrFile;

            iYear  = pFFB->fdateLastWrite.year + 1980;
            iMonth = pFFB->fdateLastWrite.month;
            iDay   = pFFB->fdateLastWrite.day;

            iHour = pFFB->ftimeLastWrite.hours;
            iMin  = pFFB->ftimeLastWrite.minutes;
            iSec  = pFFB->ftimeLastWrite.twosecs * 2;

            oNextEntryOffset = pFFB->oNextEntryOffset;
         }
         else
         {
            PFILEFINDBUF3 pFFB = ( PFILEFINDBUF3 ) info->next;

            hb_strncpy( ffind->szName, pFFB->achName, sizeof( ffind->szName ) - 1 );
            ffind->size = ( HB_FOFFSET ) pFFB->cbFile;
            raw_attr = pFFB->attrFile;

            iYear  = pFFB->fdateLastWrite.year + 1980;
            iMonth = pFFB->fdateLastWrite.month;
            iDay   = pFFB->fdateLastWrite.day;

            iHour = pFFB->ftimeLastWrite.hours;
            iMin  = pFFB->ftimeLastWrite.minutes;
            iSec  = pFFB->ftimeLastWrite.twosecs * 2;

            oNextEntryOffset = pFFB->oNextEntryOffset;
         }

         if( oNextEntryOffset > 0 )
         {
            info->next = ( char * ) info->next + oNextEntryOffset;
            info->findCount--;
         }
         else
            info->findCount = 0;
      }

      hb_fsSetError( ( HB_ERRCODE ) ret );
   }

#elif defined( HB_OS_WIN )

   {
      PHB_FFIND_INFO info = ( PHB_FFIND_INFO ) ffind->info;

      bFound = HB_FALSE;

#if ! defined( HB_OS_WIN_CE )
      if( ( ffind->attrmask & HB_FA_LABEL ) != 0 && ! info->fLabelDone )
      {
         TCHAR lpVolName[ HB_PATH_MAX ];
         LPTSTR lpFileMask = NULL;
         char * mask = NULL;

         info->fLabelDone = HB_TRUE;

         if( ffind->pszFileMask && *ffind->pszFileMask )
         {
            PHB_FNAME pFileName = hb_fsFNameSplit( ffind->pszFileMask );
            if( pFileName->szName && pFileName->szName[ 0 ] )
               mask = hb_strdup( pFileName->szName );
            if( pFileName->szPath && pFileName->szPath[ 0 ] &&
                ( pFileName->szPath[ 1 ] ||
                  pFileName->szPath[ 0 ] != HB_OS_PATH_DELIM_CHR ) )
               lpFileMask = HB_CHARDUP( pFileName->szPath );
            hb_xfree( pFileName );
         }
         bFound = GetVolumeInformation( lpFileMask, lpVolName,
                                        HB_SIZEOFARRAY( lpVolName ),
                                        NULL, NULL, NULL, NULL, 0 ) != 0;
         if( bFound )
         {
            HB_OSSTRDUP2( lpVolName, ffind->szName, sizeof( ffind->szName ) - 1 );
            if( mask && *mask && ! hb_strMatchFile( ffind->szName, mask ) )
            {
               ffind->szName[ 0 ] = '\0';
               bFound = HB_FALSE;
            }
         }
         if( lpFileMask )
            hb_xfree( lpFileMask );
         if( mask )
            hb_xfree( mask );
      }
#endif

      if( ! bFound &&
          ( ffind->attrmask & ( HB_FA_LABEL | HB_FA_HIDDEN | HB_FA_SYSTEM |
                                HB_FA_DIRECTORY ) ) != HB_FA_LABEL )
      {
         if( ffind->bFirst )
         {
            LPTSTR lpFileMask = HB_CHARDUP( ffind->pszFileMask );
            ffind->bFirst = HB_FALSE;
            info->dwAttr    = ( DWORD ) hb_fsAttrToRaw( ffind->attrmask );
            info->hFindFile = FindFirstFile( lpFileMask, &info->pFindFileData );
            hb_xfree( lpFileMask );

            if( ( info->hFindFile != INVALID_HANDLE_VALUE ) && _HB_WIN_MATCH() )
               bFound = HB_TRUE;
         }

         if( ! bFound && info->hFindFile != INVALID_HANDLE_VALUE )
         {
            while( FindNextFile( info->hFindFile, &info->pFindFileData ) )
            {
               if( _HB_WIN_MATCH() )
               {
                  bFound = HB_TRUE;
                  break;
               }
            }
         }

         /* Fill Harbour found file info */

         if( bFound )
         {
            HB_OSSTRDUP2( info->pFindFileData.cFileName, ffind->szName, sizeof( ffind->szName ) - 1 );

            if( info->pFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
               ffind->size = 0;
            else
            {
#if defined( __XCC__ ) || ( defined( __POCC__ ) && __POCC__ >= 500 )
               /* NOTE: PellesC 5.00.1 will go into an infinite loop if we don't
                        split this into two operations. [vszakats] */
               ffind->size  = ( HB_FOFFSET ) info->pFindFileData.nFileSizeLow;
               ffind->size += ( HB_FOFFSET ) info->pFindFileData.nFileSizeHigh << 32;
#else
               ffind->size = ( HB_FOFFSET ) info->pFindFileData.nFileSizeLow +
                           ( ( HB_FOFFSET ) info->pFindFileData.nFileSizeHigh << 32 );
#endif
            }

            raw_attr = ( HB_FATTR ) info->pFindFileData.dwFileAttributes;

            /* NOTE: One of these may fail when searching on an UNC path, I
                     don't know yet what's the reason. [vszakats] */

            {
               FILETIME ft;
               SYSTEMTIME time;

               if( FileTimeToLocalFileTime( &info->pFindFileData.ftLastWriteTime, &ft ) &&
                   FileTimeToSystemTime( &ft, &time ) )
               {
                  iYear  = time.wYear;
                  iMonth = time.wMonth;
                  iDay   = time.wDay;
                  iHour  = time.wHour;
                  iMin   = time.wMinute;
                  iSec   = time.wSecond;
                  iMSec  = time.wMilliseconds;
               }
            }
         }
      }
      hb_fsSetIOError( bFound, 0 );
   }

#elif defined( HB_OS_UNIX )

   {
      PHB_FFIND_INFO info = ( PHB_FFIND_INFO ) ffind->info;

      char dirname[ HB_PATH_MAX ];

      bFound = HB_FALSE;

      /* TODO: HB_FA_LABEL handling */

      if( ffind->bFirst )
      {
         char * pos;

         ffind->bFirst = HB_FALSE;

         hb_strncpy( dirname, ffind->pszFileMask, sizeof( dirname ) - 1 );
         pos = strrchr( dirname, HB_OS_PATH_DELIM_CHR );
         if( pos )
         {
            hb_strncpy( info->pattern, pos + 1, sizeof( info->pattern ) - 1 );
            *( pos + 1 ) = '\0';
         }
         else
         {
            hb_strncpy( info->pattern, dirname, sizeof( info->pattern ) - 1 );
            dirname[ 0 ] = '.';
            dirname[ 1 ] = HB_OS_PATH_DELIM_CHR;
            dirname[ 2 ] = '\0';
         }

         /* tzset(); */

         info->dir = opendir( dirname );
         hb_strncpy( info->path, dirname, sizeof( info->path ) - 1 );
      }

      if( info->dir && info->pattern[ 0 ] != '\0' )
      {
         while( ( info->entry = readdir( info->dir ) ) != NULL )
         {
            if( hb_strMatchFile( info->entry->d_name, info->pattern ) )
            {
               bFound = HB_TRUE;
               break;
            }
         }
      }

      /* Fill Harbour found file info */
      if( bFound )
      {
         hb_strncpy( dirname, info->path, sizeof( dirname ) - 1 );
         hb_strncat( dirname, info->entry->d_name, sizeof( dirname ) - 1 );
         {
            time_t ftime;
            struct tm lt;
#if defined( HB_USE_LARGEFILE64 )
            struct stat64 sStat, sStatL;
            if( lstat64( dirname, &sStat ) == 0 )
            {
               if( S_ISLNK( sStat.st_mode ) && ( ffind->attrmask & HB_FA_LINK ) == 0 )
               {
                  if( stat64( dirname, &sStatL ) == 0 )
                     memcpy( &sStat, &sStatL, sizeof( sStat ) );
                  nAttr |= HB_FA_LINK;
               }
#else
            struct stat sStat, sStatL;
            if( lstat( dirname, &sStat ) == 0 )
            {
               if( S_ISLNK( sStat.st_mode ) && ( ffind->attrmask & HB_FA_LINK ) == 0 )
               {
                  if( stat( dirname, &sStatL ) == 0 )
                     memcpy( &sStat, &sStatL, sizeof( sStat ) );
                  nAttr |= HB_FA_LINK;
               }
#endif
               hb_strncpy( ffind->szName, info->entry->d_name, sizeof( ffind->szName ) - 1 );
               ffind->size = sStat.st_size;

               raw_attr = sStat.st_mode;

               ftime = sStat.st_mtime;
#  if defined( HB_HAS_LOCALTIME_R )
               localtime_r( &ftime, &lt );
#  else
               lt = *localtime( &ftime );
#  endif

               iYear  = lt.tm_year + 1900;
               iMonth = lt.tm_mon + 1;
               iDay   = lt.tm_mday;

               iHour = lt.tm_hour;
               iMin  = lt.tm_min;
               iSec  = lt.tm_sec;

#  if defined( HB_OS_LINUX ) && \
      defined( __GLIBC__ ) && defined( __GLIBC_MINOR__ ) && \
      ( __GLIBC__ > 2 || ( __GLIBC__ == 2 && __GLIBC_MINOR__ >= 6 ) )
#     if defined( _BSD_SOURCE ) || defined( _SVID_SOURCE ) || \
         ( __GLIBC_MINOR__ >= 12 && \
           ( ( defined( _POSIX_C_SOURCE ) || _POSIX_C_SOURCE >= 200809L ) || \
             ( defined( _XOPEN_SOURCE ) || _XOPEN_SOURCE >= 700 ) ) )
               iMSec = sStat.st_mtim.tv_nsec / 1000000;
#     else
               iMSec = sStat.st_mtimensec / 1000000;
#     endif
#  endif
            }
            else
               bFound = HB_FALSE;
         }
      }
      hb_fsSetIOError( bFound, 0 );
   }

#else

   {
      int iTODO; /* TODO: for given platform */

      /* HB_SYMBOL_UNUSED( ffind ); */

      HB_SYMBOL_UNUSED( iYear );
      HB_SYMBOL_UNUSED( iMonth );
      HB_SYMBOL_UNUSED( iDay );
      HB_SYMBOL_UNUSED( iHour );
      HB_SYMBOL_UNUSED( iMin );
      HB_SYMBOL_UNUSED( iSec );
      HB_SYMBOL_UNUSED( iMSec );
      HB_SYMBOL_UNUSED( raw_attr );

      bFound = HB_FALSE;

      hb_fsSetIOError( bFound, 0 );
   }

#endif

   /* Fill common Harbour found file info */

   if( bFound )
   {
      /* Do the conversions common for all platforms */
      ffind->szName[ sizeof( ffind->szName ) - 1 ] = '\0';

#if ! defined( HB_OS_WIN )
      /* Convert from OS codepage */
      {
         char * pszFree = NULL;
         HB_SIZE nSize = sizeof( ffind->szName );
         const char * pszResult = hb_osDecodeCP( ffind->szName, &pszFree, &nSize );

         if( pszFree )
         {
            hb_strncpy( ffind->szName, pszResult, sizeof( ffind->szName ) - 1 );
            hb_xfree( pszFree );
         }
      }
#endif
      ffind->attr = hb_fsAttrFromRaw( raw_attr ) | nAttr;

      ffind->lDate = hb_dateEncode( iYear, iMonth, iDay );
      ffind->lTime = hb_timeEncode( iHour, iMin, iSec, iMSec );
      hb_dateStrPut( ffind->szDate, iYear, iMonth, iDay );
      ffind->szDate[ 8 ] = '\0';

      hb_snprintf( ffind->szTime, sizeof( ffind->szTime ), "%02d:%02d:%02d", iHour, iMin, iSec );
   }
   hb_vmLock();

   return bFound;
}

PHB_FFIND hb_fsFindFirst( const char * pszFileMask, HB_FATTR attrmask )
{
   PHB_FFIND ffind = ( PHB_FFIND ) hb_xgrabz( sizeof( HB_FFIND ) );

   /* Allocate platform dependent file find info storage */
   ffind->info = ( void * ) hb_xgrabz( sizeof( HB_FFIND_INFO ) );

   /* Store search parameters */
#if defined( HB_OS_WIN )
   ffind->pszFileMask = pszFileMask;
#else
   /* Convert to OS codepage */
   ffind->pszFileMask = hb_fsNameConv( pszFileMask, &ffind->pszFree );
#endif
   ffind->attrmask = attrmask;
   ffind->bFirst = HB_TRUE;

   /* Find first/next matching file */

   if( hb_fsFindNext( ffind ) )
      return ffind;

   /* If no file found at all, free stuff allocated so far and return NULL. */

   hb_fsFindClose( ffind );

   return NULL;
}