diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 35d73b85e3..050dd4bd66 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,30 @@ 2002-12-01 13:30 UTC+0100 Foo Bar */ +2007-10-27 16:46 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) + * include/hbapifs.h + * source/common/hbfsapi.c + ! Added missing HB_EXPORTs. + + Added hb_fsFileExists(). This is the preferred way to check + for the existence of a file, because it is fast and will + find a file even of directory traversal is not possible + for the file's directory. Works the same way as file + existence check in most other tools. + ; NOTE: DOS branch was not compiled or tested (it is a + rewrite of a tested ASM function though). + + * common.mak + * include/hbextern.ch + * source/rtl/Makefile + + source/rtl/hbfile.c + + Added HB_FILEEXISTS() Harbour level function to check + for the existence of a file. This works better than + FILE() in most situations, but it doesn't accept any + wildcards. + + * source/compiler/gencobj.c + ! Fixed to not used access(). Using hb_fsFileExists() instead. + 2007-10-27 16:04 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * source/compiler/gencobj.c ! HOST_OS_UNIX_COMPATIBLE -> OS_UNIX_COMPATIBLE diff --git a/harbour/common.mak b/harbour/common.mak index 25c8942377..4b59ec2060 100644 --- a/harbour/common.mak +++ b/harbour/common.mak @@ -446,6 +446,7 @@ RTL_LIB_OBJS = \ $(OBJ_DIR)\hbhex$(OBJEXT) \ $(OBJ_DIR)\hbmd5$(OBJEXT) \ $(OBJ_DIR)\hbffind$(OBJEXT) \ + $(OBJ_DIR)\hbfile$(OBJEXT) \ $(OBJ_DIR)\hbgtcore$(OBJEXT) \ $(OBJ_DIR)\hbinet$(OBJEXT) \ $(OBJ_DIR)\hbrandom$(OBJEXT) \ diff --git a/harbour/include/hbapifs.h b/harbour/include/hbapifs.h index 67198e51b3..273d3a1757 100644 --- a/harbour/include/hbapifs.h +++ b/harbour/include/hbapifs.h @@ -147,6 +147,7 @@ extern HB_EXPORT FHANDLE hb_fsPOpen( BYTE * pFilename, BYTE * pMode ); extern HB_EXPORT FHANDLE hb_fsGetOsHandle( FHANDLE hFileHandle ); extern HB_EXPORT USHORT hb_getFError( void ); /* get FERROR() flag */ extern HB_EXPORT void hb_setFError( USHORT uiError ); /* set FERROR() flag */ +extern HB_EXPORT BOOL hb_fsFileExists( const char * pszFileName ); /* check if a file exists (wildcard chars not accepted). */ #define hb_fsFLock( h, s, l ) hb_fsLock( h, s, l, FL_LOCK ) #define hb_fsFUnlock( h, s, l ) hb_fsLock( h, s, l, FL_UNLOCK ) diff --git a/harbour/include/hbextern.ch b/harbour/include/hbextern.ch index f10ea510ae..328a55822d 100644 --- a/harbour/include/hbextern.ch +++ b/harbour/include/hbextern.ch @@ -780,6 +780,7 @@ EXTERNAL HB_COLORINDEX EXTERNAL HB_COMPILER EXTERNAL HB_PCODEVER EXTERNAL HB_BUILDDATE +EXTERNAL HB_FILEEXISTS EXTERNAL HB_FNAMEMERGE EXTERNAL HB_FNAMESPLIT EXTERNAL HB_LANGNAME diff --git a/harbour/source/common/hbfsapi.c b/harbour/source/common/hbfsapi.c index b58c08ac0a..aba9621eba 100644 --- a/harbour/source/common/hbfsapi.c +++ b/harbour/source/common/hbfsapi.c @@ -50,9 +50,21 @@ * */ +#define HB_OS_WIN_32_USED + #include "hbapi.h" #include "hbapifs.h" +#if defined( HB_OS_WIN_32 ) + #if !defined( INVALID_FILE_ATTRIBUTES ) + #define INVALID_FILE_ATTRIBUTES ( ( DWORD ) -1 ) + #endif +#elif defined( HB_OS_UNIX ) + #include + #include + #include +#endif + /* NOTE: Not really belongs here, but until we can't find a better place it will do it. [vszakats] */ extern void hb_fhnd_ForceLink( void ); @@ -60,7 +72,7 @@ extern void hb_fhnd_ForceLink( void ); /* * Function that adds zero or more paths to a list of pathnames to search */ -void hb_fsAddSearchPath( const char * szPath, HB_PATHNAMES ** pSearchList ) +HB_EXPORT void hb_fsAddSearchPath( const char * szPath, HB_PATHNAMES ** pSearchList ) { char * pPath; char * pDelim; @@ -91,7 +103,7 @@ void hb_fsAddSearchPath( const char * szPath, HB_PATHNAMES ** pSearchList ) /* * free list of pathnames to search */ -void hb_fsFreeSearchPath( HB_PATHNAMES * pSearchList ) +HB_EXPORT void hb_fsFreeSearchPath( HB_PATHNAMES * pSearchList ) { HB_PATHNAMES * pNext; @@ -110,7 +122,7 @@ void hb_fsFreeSearchPath( HB_PATHNAMES * pSearchList ) } /* Split given filename into path, name and extension, plus determine drive */ -PHB_FNAME hb_fsFNameSplit( const char * pszFileName ) +HB_EXPORT PHB_FNAME hb_fsFNameSplit( const char * pszFileName ) { PHB_FNAME pFileName; char * pszPos; @@ -197,7 +209,7 @@ PHB_FNAME hb_fsFNameSplit( const char * pszFileName ) /* NOTE: szFileName buffer must be at least _POSIX_PATH_MAX long */ /* This function joins path, name and extension into a string with a filename */ -char * hb_fsFNameMerge( char * pszFileName, PHB_FNAME pFileName ) +HB_EXPORT char * hb_fsFNameMerge( char * pszFileName, PHB_FNAME pFileName ) { static char szPathSep[] = {OS_PATH_DELIMITER,0}; /* see NOTE below */ char * pszName; @@ -264,3 +276,40 @@ char * hb_fsFNameMerge( char * pszFileName, PHB_FNAME pFileName ) return pszFileName; } + +HB_EXPORT BOOL hb_fsFileExists( const char * pszFileName ) +{ + if( pszFileName == NULL ) + return FALSE; + + #if defined( HB_OS_DOS ) + { + union REGS regs; + struct SREGS sregs; + + regs.HB_XREGS.ax = 0x4300; + regs.HB_XREGS.dx = FP_OFF( pszFileName ); + sregs.ds = FP_SEG( pszFileName ); + + HB_DOS_INT86X( 0x21, ®s, ®s, &sregs ); + + return regs.x.cflag == 0; + } + #elif defined( HB_OS_WIN_32 ) + { + return GetFileAttributes( pszFileName ) != INVALID_FILE_ATTRIBUTES; + } + #elif defined( HB_OS_UNIX ) + { + struct stat statbuf; + + return stat( pszFileName, &statbuf ) == 0; + } + #else + { + HB_SYMBOL_UNUSED( pszFileName ); + + return FALSE; + } + #endif +} diff --git a/harbour/source/compiler/gencobj.c b/harbour/source/compiler/gencobj.c index b3e6e95704..73c764f4d4 100644 --- a/harbour/source/compiler/gencobj.c +++ b/harbour/source/compiler/gencobj.c @@ -36,12 +36,8 @@ #if defined( OS_UNIX_COMPATIBLE ) #define HB_NULL_STR " > /dev/null" - #define HB_ACCESS_FLAG F_OK #elif defined( OS_DOS_COMPATIBLE ) #define HB_NULL_STR " >nul" - #define HB_ACCESS_FLAG 0 -#else - #define HB_ACCESS_FLAG 0 #endif /*--------------------------------------------------------------------------*/ @@ -52,7 +48,7 @@ static char * hb_searchpath( const char * pszFile, char * pszEnv, char * pszCfg BOOL bFound = FALSE; /* Check current dir first */ - if( access( ( const char * ) pszFile, HB_ACCESS_FLAG ) == 0 ) + if( hb_fsFileExists( ( const char * ) pszFile ) ) { snprintf( pszCfg, _POSIX_PATH_MAX + 1, "%s", pszFile ); return ( char * ) pszFile; @@ -75,7 +71,7 @@ static char * hb_searchpath( const char * pszFile, char * pszEnv, char * pszCfg if( *pszPath ) { snprintf( pszCfg, _POSIX_PATH_MAX + 1, "%s%c%s", pszPath, OS_PATH_DELIMITER, pszFile ); - if( access( ( const char * ) pszCfg, HB_ACCESS_FLAG ) == 0 ) + if( hb_fsFileExists( ( const char * ) pszCfg ) ) { bFound = TRUE; break; diff --git a/harbour/source/rtl/Makefile b/harbour/source/rtl/Makefile index c63d5ff293..8db5ee06cf 100644 --- a/harbour/source/rtl/Makefile +++ b/harbour/source/rtl/Makefile @@ -56,6 +56,7 @@ C_SOURCES=\ hbhex.c \ hbmd5.c \ hbffind.c \ + hbfile.c \ hbgtcore.c \ hbinet.c \ hbrandom.c \ diff --git a/harbour/source/rtl/hbfile.c b/harbour/source/rtl/hbfile.c new file mode 100644 index 0000000000..77f6f60a44 --- /dev/null +++ b/harbour/source/rtl/hbfile.c @@ -0,0 +1,59 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * HB_FILEEXISTS() function + * + * Copyright 2007 Viktor Szakats + * www - http://www.harbour-project.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/). + * + * As a special exception, the Harbour Project gives permission for + * additional uses of the text contained in its release of Harbour. + * + * The exception is that, if you link the Harbour libraries with other + * files to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the Harbour library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the Harbour + * Project under the name Harbour. If you copy code from other + * Harbour Project or Free Software Foundation releases into a copy of + * Harbour, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for Harbour, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. + * + */ + +#include "hbapi.h" +#include "hbapifs.h" + +HB_FUNC( HB_FILEEXISTS ) +{ + hb_retl( hb_fsFileExists( hb_parc( 1 ) ) ); +}