Example #1
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;
}
Example #2
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;
}