Пример #1
0
Файл: files.c Проект: AMHF/core
static PHB_FFIND _hb_fileStart( HB_BOOL fNext, HB_BOOL fAny )
{
   PHB_FFDATA pFFData = HB_GET_FFDATA();

   if( hb_pcount() > 0 )
   {
      const char * szFile = hb_parc( 1 );

      if( pFFData->ffind )
      {
         hb_fsFindClose( pFFData->ffind );
         pFFData->ffind = NULL;
      }

      if( szFile )
      {
         HB_FATTR ulAttr;

         ulAttr = ( HB_FATTR ) hb_parnldef( 2, fAny ? HB_FA_ANY : HB_FA_ALL );
         pFFData->ulAttr = hb_parl( 3 ) ? ulAttr : 0;
         pFFData->ffind  = hb_fsFindFirst( szFile, ulAttr );
         while( pFFData->ffind && pFFData->ulAttr &&
                HB_FF_ATTR( pFFData->ffind ) != pFFData->ulAttr )
         {
            if( ! hb_fsFindNext( pFFData->ffind ) )
            {
               hb_fsFindClose( pFFData->ffind );
               pFFData->ffind = NULL;
            }
         }
      }
   }
   else if( fNext && pFFData->ffind )
   {
      do
      {
         if( ! hb_fsFindNext( pFFData->ffind ) )
         {
            hb_fsFindClose( pFFData->ffind );
            pFFData->ffind = NULL;
            break;
         }
      }
      while( pFFData->ulAttr && HB_FF_ATTR( pFFData->ffind ) != pFFData->ulAttr );
   }

   return pFFData->ffind;
}
Пример #2
0
Файл: files.c Проект: AMHF/core
static void hb_fileFindRelease( void * cargo )
{
   PHB_FFDATA pFFData = ( PHB_FFDATA ) cargo;

   if( pFFData->ffind )
      hb_fsFindClose( pFFData->ffind );
}
Пример #3
0
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;
}
Пример #4
0
HB_BOOL hb_fsFile( const char * pszFilename )
{
   PHB_FFIND ffind;

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

   if( ( ffind = hb_fsFindFirst( pszFilename, HB_FA_ALL ) ) != NULL )
   {
      hb_fsFindClose( ffind );
      return HB_TRUE;
   }

   return HB_FALSE;
}
Пример #5
0
static HB_GARBAGE_FUNC( PHB_FFIND_release )
{
   void ** ph = ( void ** ) Cargo;

   /* Check if pointer is not NULL to avoid multiple freeing */
   if( ph && *ph )
   {
      /* Destroy the object */
      hb_fsFindClose( ( PHB_FFIND ) *ph );

      /* set pointer to NULL just in case */
      *ph = NULL;
   }
}
Пример #6
0
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
static char * hb_fsFileFind( const char * pszFileMask )
{
   PHB_FFIND ffind;

   if( ( ffind = hb_fsFindFirst( pszFileMask, HB_FA_ALL ) ) != NULL )
   {
      char pszFileName[ HB_PATH_MAX ];
      PHB_FNAME pFileName = hb_fsFNameSplit( pszFileMask );
      pFileName->szName = ffind->szName;
      pFileName->szExtension = NULL;
      hb_fsFNameMerge( pszFileName, pFileName );
      hb_fsFindClose( ffind );
      hb_xfree( pFileName );
      return hb_strdup( pszFileName );
   }
   return NULL;
}
Пример #8
0
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
HB_BOOL hb_fsIsDirectory( const char * pszFilename )
{
   HB_BOOL bResult = HB_FALSE;
   PHB_FFIND ffind;
   char * pszFree = NULL;
   int iLen;

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

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

   if( pszFilename[ iLen ] )
      pszFilename = pszFree = hb_strndup( pszFilename, iLen );

   if( iLen && iLen <= ( HB_PATH_MAX - 1 ) )
   {
      if( ( ffind = hb_fsFindFirst( pszFilename, HB_FA_DIRECTORY ) ) != NULL )
      {
         do
         {
            if( ( ffind->attr & HB_FA_DIRECTORY ) == HB_FA_DIRECTORY )
            {
               bResult = HB_TRUE;
               break;
            }
         }
         while( hb_fsFindNext( ffind ) );
         hb_fsFindClose( ffind );
      }
   }

   if( pszFree )
      hb_xfree( pszFree );

   return bResult;
}
Пример #10
0
PHB_ITEM hb_fsDirectory( const char * pszDirSpec, const char * pszAttributes, HB_BOOL fDateTime )
{
   PHB_ITEM  pDir = hb_itemArrayNew( 0 );
   char *    pszFree = NULL;
   PHB_FFIND ffind;
   HB_FATTR  ulMask;

   /* Get the passed attributes and convert them to Harbour Flags */

   ulMask = HB_FA_ARCHIVE | HB_FA_READONLY;

   if( pszAttributes && *pszAttributes )
      ulMask |= hb_fsAttrEncode( pszAttributes );

   if( pszDirSpec && *pszDirSpec )
   {
      if( ulMask != HB_FA_LABEL )
      {
         /* CA-Cl*pper compatible behavior - add all file mask when
          * last character is directory or drive separator
          */
         HB_SIZE nLen = strlen( pszDirSpec ) - 1;
#ifdef HB_OS_HAS_DRIVE_LETTER
         if( pszDirSpec[ nLen ] == HB_OS_PATH_DELIM_CHR ||
             pszDirSpec[ nLen ] == HB_OS_DRIVE_DELIM_CHR )
#else
         if( pszDirSpec[ nLen ] == HB_OS_PATH_DELIM_CHR )
#endif
            pszDirSpec = pszFree =
                           hb_xstrcpy( NULL, pszDirSpec, HB_OS_ALLFILE_MASK, NULL );
      }
   }
   else
      pszDirSpec = HB_OS_ALLFILE_MASK;

   /* Get the file list */

   if( ( ffind = hb_fsFindFirst( pszDirSpec, ulMask ) ) != NULL )
   {
      PHB_ITEM pSubarray = hb_itemNew( NULL );

      do
      {
         char buffer[ 32 ];

         hb_arrayNew    ( pSubarray, F_LEN );
         hb_arraySetC   ( pSubarray, F_NAME, ffind->szName );
         hb_arraySetNInt( pSubarray, F_SIZE, ffind->size );
         hb_arraySetC   ( pSubarray, F_TIME, ffind->szTime );
         hb_arraySetC   ( pSubarray, F_ATTR, hb_fsAttrDecode( ffind->attr, buffer ) );

         if( fDateTime )
            hb_arraySetTDT( pSubarray, F_DATE, ffind->lDate, ffind->lTime );
         else
            hb_arraySetDL ( pSubarray, F_DATE, ffind->lDate );

         /* Don't exit when array limit is reached */
         hb_arrayAddForward( pDir, pSubarray );
      }
      while( hb_fsFindNext( ffind ) );

      hb_itemRelease( pSubarray );

      hb_fsFindClose( ffind );
   }

   if( pszFree )
      hb_xfree( pszFree );

   return pDir;
}
Пример #11
0
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;
}
Пример #12
0
static BOOL hb_fsFileStats(
   BYTE * pszFileName,
   BYTE * pszAttr,
   HB_FOFFSET * llSize,
   LONG * lcDate,
   LONG * lcTime,
   LONG * lmDate,
   LONG * lmTime )
{
   BOOL fResult = FALSE;

#if defined( HB_OS_UNIX )

   struct stat statbuf;

   if( stat( ( char * ) pszFileName, &statbuf ) == 0 )
   {
      // determine if we can read/write/execute the file
      USHORT      usAttr, ushbAttr = 0;
      time_t      ftime;
#if _POSIX_C_SOURCE >= 199506L
      struct tm   tms;
#endif
      struct tm * ptms;

      /* See which attribs are applicable */
      if( statbuf.st_uid == geteuid() )
      {
         usAttr =
            ( ( statbuf.st_mode & S_IRUSR ) ? 1 << 2 : 0 ) |
            ( ( statbuf.st_mode & S_IWUSR ) ? 1 << 1 : 0 ) |
            ( ( statbuf.st_mode & S_IXUSR ) ? 1 : 0 );
      }
      else if( statbuf.st_gid == getegid() )
      {
         usAttr =
            ( ( statbuf.st_mode & S_IRGRP ) ? 1 << 2 : 0 ) |
            ( ( statbuf.st_mode & S_IWGRP ) ? 1 << 1 : 0 ) |
            ( ( statbuf.st_mode & S_IXGRP ) ? 1 : 0 );
      }
      else
      {
         usAttr =
            ( ( statbuf.st_mode & S_IROTH ) ? 1 << 2 : 0 ) |
            ( ( statbuf.st_mode & S_IWOTH ) ? 1 << 1 : 0 ) |
            ( ( statbuf.st_mode & S_IXOTH ) ? 1 : 0 );
      }

      /* Standard characters */
      if( ( usAttr & 4 ) == 0 ) /* Hidden (can't read)*/
         ushbAttr |= HB_FA_HIDDEN;

      if( ( usAttr & 2 ) == 0 ) /* read only (can't write)*/
         ushbAttr |= HB_FA_READONLY;

      if( ( usAttr & 1 ) == 1 ) /* executable?  (xbit)*/
         ushbAttr |= HB_FA_SYSTEM;

      /* Extension characters */

      if( ( statbuf.st_mode & S_IFLNK ) == S_IFLNK )
         *pszAttr++ = 'Z';  /* Xharbour extension */

      if( ( statbuf.st_mode & S_IFSOCK ) == S_IFSOCK )
         *pszAttr++ = 'K';  /* Xharbour extension */

      /* device */
      if( ( statbuf.st_mode & S_IFBLK ) == S_IFBLK ||
          ( statbuf.st_mode & S_IFCHR ) == S_IFCHR )
         ushbAttr |= HB_FA_DEVICE;  /* Xharbour extension */

      if( ( statbuf.st_mode & S_IFIFO ) == S_IFIFO )
         *pszAttr++ = 'Y';  /* Xharbour extension */

      if( S_ISDIR( statbuf.st_mode ) )
         ushbAttr |= HB_FA_DIRECTORY;  /* Xharbour extension */
      /* Give the ARCHIVE if readwrite, not executable and not special */
      else if( S_ISREG( statbuf.st_mode ) && ushbAttr == 0 )
         ushbAttr |= HB_FA_ARCHIVE;

      *llSize  = ( HB_FOFFSET ) statbuf.st_size;

      ftime    = statbuf.st_mtime;
#if _POSIX_C_SOURCE >= 199506L && ! defined( HB_OS_DARWIN_5 )
      ptms     = localtime_r( &ftime, &tms );
#else
      ptms     = localtime( &ftime );
#endif

      *lcDate  = hb_dateEncode( ptms->tm_year + 1900,
                                ptms->tm_mon + 1, ptms->tm_mday );
      *lcTime  = ptms->tm_hour * 3600 + ptms->tm_min * 60 + ptms->tm_sec;

      ftime    = statbuf.st_atime;
#if _POSIX_C_SOURCE >= 199506L && ! defined( HB_OS_DARWIN_5 )
      ptms     = localtime_r( &ftime, &tms );
#else
      ptms     = localtime( &ftime );
#endif
      *lmDate  = hb_dateEncode( ptms->tm_year + 1900,
                                ptms->tm_mon + 1, ptms->tm_mday );
      *lmTime  = ptms->tm_hour * 3600 + ptms->tm_min * 60 + ptms->tm_sec;

      hb_fsAttrDecode( ushbAttr, ( char * ) pszAttr );

      fResult = TRUE;
   }

#elif defined( HB_OS_WIN )

   {
      DWORD             dwAttribs;
      WIN32_FIND_DATAA  ffind;
      HANDLE            hFind;
      FILETIME          filetime;
      SYSTEMTIME        time;

      /* Get attributes... */
      dwAttribs = GetFileAttributesA( ( char * ) pszFileName );
      if( dwAttribs == INVALID_FILE_ATTRIBUTES )
      {
         /* return */
         return FALSE;
      }

      hb_fsAttrDecode( hb_fsAttrFromRaw( dwAttribs ), ( char * ) pszAttr );

      /* If file existed, do a findfirst */
      hFind = FindFirstFileA( ( char * ) pszFileName, &ffind );
      if( hFind != INVALID_HANDLE_VALUE )
      {
         FindClose( hFind );

         /* get file times and work them out */
         *llSize = ( HB_FOFFSET ) ffind.nFileSizeLow + ( ( HB_FOFFSET ) ffind.nFileSizeHigh << 32 );

         if( FileTimeToLocalFileTime( &ffind.ftCreationTime, &filetime ) &&
             FileTimeToSystemTime( &filetime, &time ) )
         {
            *lcDate  = hb_dateEncode( time.wYear, time.wMonth, time.wDay );
            *lcTime  = time.wHour * 3600 + time.wMinute * 60 + time.wSecond;
         }
         else
         {
            *lcDate  = hb_dateEncode( 0, 0, 0 );
            *lcTime  = 0;
         }

         if( FileTimeToLocalFileTime( &ffind.ftLastAccessTime, &filetime ) &&
             FileTimeToSystemTime( &filetime, &time ) )
         {
            *lmDate  = hb_dateEncode( time.wYear, time.wMonth, time.wDay );
            *lmTime  = time.wHour * 3600 + time.wMinute * 60 + time.wSecond;
         }
         else
         {
            *lcDate  = hb_dateEncode( 0, 0, 0 );
            *lcTime  = 0;
         }
         fResult = TRUE;
      }
   }

#else

   /* Generic algorithm based on findfirst */
   {
      PHB_FFIND findinfo = hb_fsFindFirst( ( char * ) pszFileName, HB_FA_ALL );

      if( findinfo )
      {
         hb_fsAttrDecode( findinfo->attr, ( char * ) pszAttr );
         *llSize  = ( HB_FOFFSET ) findinfo->size;
         *lcDate  = findinfo->lDate;
         *lcTime  = ( findinfo->szTime[ 0 ] - '0' ) * 36000 +
                    ( findinfo->szTime[ 1 ] - '0' ) * 3600 +
                    ( findinfo->szTime[ 3 ] - '0' ) * 600 +
                    ( findinfo->szTime[ 4 ] - '0' ) * 60 +
                    ( findinfo->szTime[ 6 ] - '0' ) * 10 +
                    ( findinfo->szTime[ 7 ] - '0' );
         *lmDate  = hb_dateEncode( 0, 0, 0 );
         *lmTime  = 0;
         hb_fsFindClose( findinfo );
         fResult  = TRUE;
      }
   }

#endif

   hb_fsSetIOError( fResult, 0 );
   return fResult;
}
Пример #13
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;
}