/* Release all allocated memory when called from the garbage collector */ static HB_GARBAGE_FUNC( hb_codeblockGarbageDelete ) { PHB_CODEBLOCK pCBlock = ( PHB_CODEBLOCK ) Cargo; HB_TRACE( HB_TR_DEBUG, ( "hb_codeblockGarbageDelete(%p)", Cargo ) ); /* free space allocated for pcodes - if it was a macro-compiled codeblock */ if( pCBlock->pCode && pCBlock->dynBuffer ) { pCBlock->dynBuffer = HB_FALSE; hb_xfree( HB_UNCONST( pCBlock->pCode ) ); } pCBlock->pCode = s_pCode; /* free space allocated for local variables */ if( pCBlock->pLocals ) { if( hb_xRefDec( pCBlock->pLocals ) ) { while( pCBlock->uiLocals ) hb_memvarValueDecRef( pCBlock->pLocals[ pCBlock->uiLocals-- ].item.asMemvar.value ); hb_xfree( pCBlock->pLocals ); } pCBlock->pLocals = NULL; pCBlock->uiLocals = 0; } }
static TOKEN_ENVIRONMENT sTokGet( int iParam, HB_BOOL fReadOnly ) { if( iParam < 0 || ( iParam > 0 && HB_ISCHAR( iParam ) ) ) { if( iParam < 0 || fReadOnly || HB_ISBYREF( iParam ) ) { HB_SIZE nLen; if( iParam < 0 ) iParam = -iParam; nLen = hb_parclen( iParam ); if( nLen >= sizeof( TOKEN_POSITION ) * 2 ) { TOKEN_ENVIRONMENT env = ( TOKEN_ENVIRONMENT ) HB_UNCONST( hb_parc( iParam ) ); if( sTokEnvGetSize( env ) == nLen ) return fReadOnly ? env : ( TOKEN_ENVIRONMENT ) hb_xmemdup( env, nLen + 1 ); } } return NULL; } else return * ( TOKEN_ENVIRONMENT * ) hb_stackGetTSD( &s_token ); }
static HB_ERRCODE odbcExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { SDDCONN * pSDDConn = ( SDDCONN * ) pConnection->pSDDConn; const O_HB_CHAR * pchStatement; void * hStatement; HB_SIZE nStatementLen; SQLHSTMT hStmt; SQLLEN iCount; char * szError; HB_ERRCODE errCode; SQLRETURN result; #if ODBCVER >= 0x0300 if( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) ) #else if( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) ) #endif { szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, hb_itemGetCPtr( pItem ), errCode ); hb_xfree( szError ); return HB_FAILURE; } pchStatement = O_HB_ITEMGETSTR( pItem, &hStatement, &nStatementLen ); result = SQLExecDirect( hStmt, ( SQLTCHAR * ) HB_UNCONST( pchStatement ), ( SQLINTEGER ) nStatementLen ); hb_strfree( hStatement ); if( SQL_SUCCEEDED( result ) ) { if( SQL_SUCCEEDED( SQLRowCount( hStmt, &iCount ) ) ) { /* TODO: new id */ hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ( unsigned long ) iCount ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif return HB_SUCCESS; } } szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); hb_rddsqlSetError( errCode, szError, hb_itemGetCPtr( pItem ), NULL, errCode ); hb_xfree( szError ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif return HB_FAILURE; }
static void hb_PEM_read_bio( PEM_READ_BIO * func, HB_BOOL fX509 ) { BIO * bio = NULL; HB_BYTE * pBuffer = NULL; HB_SIZE nSize = 0; if( hb_BIO_is( 1 ) ) bio = hb_BIO_par( 1 ); else if( HB_ISCHAR( 1 ) ) { pBuffer = hb_fileLoad( hb_parc( 1 ), 0, &nSize ); if( pBuffer ) bio = BIO_new_mem_buf( ( char * ) pBuffer, ( int ) nSize ); } else if( HB_ISNUM( 1 ) ) bio = BIO_new_fd( hb_parni( 1 ), BIO_NOCLOSE ); if( bio ) { PHB_ITEM pPassCallback = hb_param( 2, HB_IT_EVALITEM ); pem_password_cb * cb; void * cargo, * result; if( pPassCallback ) { cb = hb_ssl_pem_password_cb; cargo = pPassCallback; } else { cb = NULL; cargo = HB_UNCONST( hb_parc( 2 ) ); /* NOTE: Discarding 'const' qualifier, OpenSSL will memcpy() it */ } result = ( *func )( bio, NULL, cb, cargo ); if( fX509 && result ) hb_X509_ret( ( X509 * ) result, HB_TRUE ); else hb_retptr( result ); if( ! hb_BIO_is( 1 ) ) BIO_free( bio ); if( pBuffer ) { OPENSSL_cleanse( pBuffer, ( size_t ) nSize ); hb_xfree( pBuffer ); } } else hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); }
PHB_EXPR hb_compExprNewString( const char * szValue, HB_SIZE nLen, HB_BOOL fDealloc, HB_COMP_DECL ) { PHB_EXPR pExpr; HB_TRACE( HB_TR_DEBUG, ( "hb_compExprNewString(%s)", szValue ) ); pExpr = HB_COMP_EXPR_NEW( HB_ET_STRING ); pExpr->value.asString.string = ( char * ) HB_UNCONST( szValue ); pExpr->value.asString.dealloc = fDealloc; pExpr->nLength = nLen; pExpr->ValType = HB_EV_STRING; return pExpr; }
static PHB_LZSSX_COMPR hb_LZSSxInit( PHB_FILE pInput, const HB_BYTE * pSrcBuf, HB_SIZE nSrcBuf, PHB_FILE pOutput, HB_BYTE * pDstBuf, HB_SIZE nDstBuf ) { PHB_LZSSX_COMPR pCompr = ( PHB_LZSSX_COMPR ) hb_xgrab( sizeof( HB_LZSSX_COMPR ) ); if( pInput != NULL && nSrcBuf == 0 ) nSrcBuf = LZSS_IOBUFLEN; if( pOutput != NULL && nDstBuf == 0 ) nDstBuf = LZSS_IOBUFLEN; pCompr->pInput = pInput; pCompr->inBuffer = ( HB_BYTE * ) HB_UNCONST( pSrcBuf ); pCompr->inBuffSize = nSrcBuf; pCompr->inBuffPos = 0; pCompr->inBuffRead = ( pInput == NULL ) ? nSrcBuf : 0; pCompr->fInFree = ( pInput != NULL && pSrcBuf == NULL ); pCompr->pOutput = pOutput; pCompr->outBuffer = pDstBuf; pCompr->outBuffSize = nDstBuf; pCompr->outBuffPos = 0; pCompr->fOutFree = ( pOutput != NULL && pDstBuf == NULL ); pCompr->nOutSize = 0; pCompr->fResult = HB_TRUE; pCompr->fContinue = HB_FALSE; if( pCompr->fInFree ) pCompr->inBuffer = ( HB_BYTE * ) hb_xgrab( nDstBuf ); if( pCompr->fOutFree ) pCompr->outBuffer = ( HB_BYTE * ) hb_xgrab( nDstBuf ); /* initialize the ring buffer with spaces, because SIX uses dynamic ring buffer then we do not have to fill last MAXLENGTH characters */ memset( pCompr->ring_buffer, ' ', RBUFLENGTH - 1 ); return pCompr; }
/* write data using stream structure */ long hb_znetWrite( PHB_ZNETSTREAM pStream, HB_SOCKET sd, const void * buffer, long len, HB_MAXINT timeout, long * plast ) { long snd = 0; pStream->wr.next_in = ( Bytef * ) HB_UNCONST( buffer ); pStream->wr.avail_in = ( uInt ) len; pStream->err = Z_OK; while( pStream->wr.avail_in ) { if( pStream->wr.avail_out == 0 ) { snd = hb_znetStreamWrite( pStream, sd, timeout ); if( plast ) *plast = snd; if( snd <= 0 ) break; snd = 0; } pStream->err = deflate( &pStream->wr, Z_NO_FLUSH ); if( pStream->err != Z_OK ) { if( pStream->err == Z_BUF_ERROR ) pStream->err = Z_OK; else { hb_socketSetError( HB_ZNET_SOCK_ERROR_BASE - pStream->err ); snd = -1; } break; } } len -= pStream->wr.avail_in; return len == 0 ? snd : len; }
static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) { SDDCONN * pSDDConn = ( SDDCONN * ) pArea->pConnection->pSDDConn; SDDDATA * pSDDData; O_HB_CHAR * pchQuery; SQLHSTMT hStmt; SQLSMALLINT iNameLen; PHB_ITEM pItemEof, pItem; HB_BOOL bError; HB_USHORT uiFields, uiIndex; HB_ERRCODE errCode; char * szError; SQLRETURN result; pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); pSDDData = ( SDDDATA * ) pArea->pSDDData; #if ODBCVER >= 0x0300 if( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) ) #else if( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) ) #endif { szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } pchQuery = O_HB_CHARDUP( pArea->szQuery ); result = SQLExecDirect( hStmt, ( SQLTCHAR * ) HB_UNCONST( pchQuery ), ( SQLINTEGER ) O_HB_STRLEN( pchQuery ) ); hb_xfree( pchQuery ); if( ! SQL_SUCCEEDED( result ) ) { szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } if( ! SQL_SUCCEEDED( SQLNumResultCols( hStmt, &iNameLen ) ) ) { szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1000, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } uiFields = ( HB_USHORT ) iNameLen; SELF_SETFIELDEXTENT( &pArea->area, uiFields ); pItemEof = hb_itemArrayNew( uiFields ); pItem = hb_itemNew( NULL ); #if 0 HB_TRACE( HB_TR_ALWAYS, ( "fieldcount=%d", iNameLen ) ); #endif errCode = 0; bError = HB_FALSE; for( uiIndex = 0; uiIndex < uiFields; uiIndex++ ) { DBFIELDINFO dbFieldInfo; char * pszName; SQLTCHAR cName[ 256 ]; SQLULEN uiSize; SQLSMALLINT iDataType, iDec, iNull; if( ! SQL_SUCCEEDED( SQLDescribeCol( hStmt, ( SQLSMALLINT ) uiIndex + 1, cName, HB_SIZEOFARRAY( cName ), &iNameLen, &iDataType, &uiSize, &iDec, &iNull ) ) ) { hb_itemRelease( pItemEof ); hb_itemRelease( pItem ); szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, NULL ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1001, szError, pArea->szQuery, 0 ); hb_xfree( szError ); return HB_FAILURE; } memset( &dbFieldInfo, 0, sizeof( dbFieldInfo ) ); dbFieldInfo.atomName = pszName = O_HB_OSSTRDUP( ( O_HB_CHAR * ) cName ); /* We do mapping of many SQL types to one Harbour field type here, so, we need store real SQL type in uiTypeExtended. SQL types are signed, so, HB_USHORT type casting is a little hackish. We need to remember use this casting also in expressions like this: if( pField->uiTypeExtended == ( HB_USHORT ) SQL_BIGINT ) or introduce our own unsigned SQL types. [Mindaugas] */ dbFieldInfo.uiTypeExtended = ( HB_USHORT ) iDataType; dbFieldInfo.uiLen = ( HB_USHORT ) uiSize; dbFieldInfo.uiDec = iDec; if( iNull == SQL_NULLABLE ) dbFieldInfo.uiFlags |= HB_FF_NULLABLE; #if 0 HB_TRACE( HB_TR_ALWAYS, ( "field: name=%s type=%d len=%d dec=%d null=%d", dbFieldInfo.atomName, iDataType, uiSize, iDec, iNull ) ); #endif switch( iDataType ) { case SQL_CHAR: case SQL_VARCHAR: case SQL_LONGVARCHAR: dbFieldInfo.uiType = HB_FT_STRING; break; case SQL_WCHAR: case SQL_WVARCHAR: case SQL_WLONGVARCHAR: dbFieldInfo.uiType = HB_FT_STRING; dbFieldInfo.uiFlags |= HB_FF_UNICODE; break; case SQL_BINARY: case SQL_VARBINARY: case SQL_LONGVARBINARY: dbFieldInfo.uiType = HB_FT_STRING; dbFieldInfo.uiFlags |= HB_FF_BINARY; break; case SQL_TINYINT: case SQL_SMALLINT: case SQL_INTEGER: case SQL_BIGINT: dbFieldInfo.uiType = HB_FT_INTEGER; break; case SQL_DECIMAL: case SQL_NUMERIC: dbFieldInfo.uiType = HB_FT_LONG; break; case SQL_REAL: case SQL_FLOAT: case SQL_DOUBLE: dbFieldInfo.uiType = HB_FT_DOUBLE; break; case SQL_BIT: dbFieldInfo.uiType = HB_FT_LOGICAL; break; case SQL_DATE: #if ODBCVER >= 0x0300 case SQL_TYPE_DATE: #endif dbFieldInfo.uiType = HB_FT_DATE; break; case SQL_TIME: #if ODBCVER >= 0x0300 case SQL_TYPE_TIME: #endif dbFieldInfo.uiType = HB_FT_TIME; break; /* SQL_DATETIME = SQL_DATE = 9 */ case SQL_TIMESTAMP: #if ODBCVER >= 0x0300 case SQL_TYPE_TIMESTAMP: #endif dbFieldInfo.uiType = HB_FT_TIMESTAMP; break; default: #if 0 HB_TRACE( HB_TR_ALWAYS, ( "new sql type=%d", iDataType ) ); #endif bError = HB_TRUE; errCode = ( HB_ERRCODE ) iDataType; break; } if( ! bError ) { switch( dbFieldInfo.uiType ) { case HB_FT_STRING: { char * pStr; pStr = ( char * ) hb_xgrab( ( HB_SIZE ) dbFieldInfo.uiLen + 1 ); memset( pStr, ' ', dbFieldInfo.uiLen ); pStr[ dbFieldInfo.uiLen ] = '\0'; hb_itemPutCL( pItem, pStr, dbFieldInfo.uiLen ); hb_xfree( pStr ); break; } case HB_FT_MEMO: hb_itemPutC( pItem, NULL ); break; case HB_FT_INTEGER: hb_itemPutNI( pItem, 0 ); break; case HB_FT_LONG: if( dbFieldInfo.uiDec == 0 ) hb_itemPutNLLen( pItem, 0, dbFieldInfo.uiLen ); else hb_itemPutNDLen( pItem, 0.0, dbFieldInfo.uiLen, dbFieldInfo.uiDec ); break; case HB_FT_DOUBLE: hb_itemPutNDLen( pItem, 0.0, dbFieldInfo.uiLen, dbFieldInfo.uiDec ); break; case HB_FT_LOGICAL: hb_itemPutL( pItem, HB_FALSE ); break; case HB_FT_DATE: hb_itemPutDL( pItem, 0 ); break; case HB_FT_TIME: hb_itemPutTDT( pItem, 0, 0 ); break; case HB_FT_TIMESTAMP: hb_itemPutTDT( pItem, 0, 0 ); break; default: hb_itemClear( pItem ); bError = HB_TRUE; break; } hb_arraySetForward( pItemEof, uiIndex + 1, pItem ); if( ! bError ) bError = ( SELF_ADDFIELD( &pArea->area, &dbFieldInfo ) == HB_FAILURE ); hb_xfree( pszName ); } if( bError ) break; } hb_itemRelease( pItem ); if( bError ) { hb_itemRelease( pItemEof ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode ); return HB_FAILURE; } pArea->ulRecCount = 0; pArea->ulRecMax = SQLDD_ROWSET_INIT; pArea->pRow = ( void ** ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( void * ) ); pArea->pRowFlags = ( HB_BYTE * ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) ); pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; pSDDData->hStmt = hStmt; return HB_SUCCESS; }
/* --- SDD METHODS --- */ static HB_ERRCODE odbcConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { SQLHENV hEnv = NULL; SQLHDBC hConnect = NULL; char * szError; HB_ERRCODE errCode; #if ODBCVER >= 0x0300 if( SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv ) ) ) #else if( SQL_SUCCEEDED( SQLAllocEnv( &hEnv ) ) ) #endif { #if ODBCVER >= 0x0300 SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION, ( SQLPOINTER ) SQL_OV_ODBC3, SQL_IS_UINTEGER ); #endif #if ODBCVER >= 0x0300 if( SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hConnect ) ) ) #else if( SQL_SUCCEEDED( SQLAllocConnect( hEnv, &hConnect ) ) ) #endif { const O_HB_CHAR * pchConStr; void * hConnStr; HB_SIZE nConnLen; SQLTCHAR cBuffer[ 1024 ]; SQLSMALLINT iLen = HB_SIZEOFARRAY( cBuffer ); cBuffer[ 0 ] = '\0'; pchConStr = O_HB_ARRAYGETSTR( pItem, 2, &hConnStr, &nConnLen ); if( SQL_SUCCEEDED( SQLDriverConnect( hConnect, NULL, ( SQLTCHAR * ) HB_UNCONST( pchConStr ), ( SQLSMALLINT ) nConnLen, cBuffer, HB_SIZEOFARRAY( cBuffer ), &iLen, SQL_DRIVER_NOPROMPT ) ) ) { hb_strfree( hConnStr ); pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); ( ( SDDCONN * ) pConnection->pSDDConn )->hConn = hConnect; ( ( SDDCONN * ) pConnection->pSDDConn )->hEnv = hEnv; return HB_SUCCESS; } else { hb_strfree( hConnStr ); szError = odbcGetError( hEnv, hConnect, SQL_NULL_HSTMT, &errCode ); hb_rddsqlSetError( errCode, szError, NULL, NULL, 0 ); hb_xfree( szError ); } #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_DBC, hConnect ); #else SQLFreeConnect( hConnect ); #endif } else { szError = odbcGetError( hEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_CONNALLOC, szError, hb_arrayGetCPtr( pItem, 2 ), errCode ); hb_xfree( szError ); } #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_ENV, hEnv ); #else SQLFreeEnv( hEnv ); #endif } else { szError = odbcGetError( SQL_NULL_HENV, SQL_NULL_HDBC, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_ENVALLOC, szError, hb_arrayGetCPtr( pItem, 2 ), errCode ); hb_xfree( szError ); } return HB_FAILURE; }
/* 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; }
HB_WCHAR * hb_fsNameConvU16( const char * szFileName ) { char * pszBuffer = NULL; HB_WCHAR * lpwFileName; if( s_fFnTrim || s_cDirSep != HB_OS_PATH_DELIM_CHR || s_iFileCase != HB_SET_CASE_MIXED || s_iDirCase != HB_SET_CASE_MIXED ) { PHB_FNAME pFileName; szFileName = pszBuffer = hb_strncpy( ( char * ) hb_xgrab( HB_PATH_MAX ), szFileName, HB_PATH_MAX - 1 ); if( s_cDirSep != HB_OS_PATH_DELIM_CHR ) { char * p = ( char * ) HB_UNCONST( szFileName ); while( *p ) { if( *p == s_cDirSep ) *p = HB_OS_PATH_DELIM_CHR; p++; } } pFileName = hb_fsFNameSplit( szFileName ); /* strip trailing and leading spaces */ if( s_fFnTrim ) { HB_SIZE nLen; if( pFileName->szName ) { nLen = strlen( pFileName->szName ); while( nLen && pFileName->szName[ nLen - 1 ] == ' ' ) --nLen; while( nLen && pFileName->szName[ 0 ] == ' ' ) { ++pFileName->szName; --nLen; } ( ( char * ) pFileName->szName )[ nLen ] = '\0'; } if( pFileName->szExtension ) { nLen = strlen( pFileName->szExtension ); while( nLen && pFileName->szExtension[ nLen - 1 ] == ' ' ) --nLen; while( nLen && pFileName->szExtension[ 0 ] == ' ' ) { ++pFileName->szExtension; --nLen; } ( ( char * ) pFileName->szExtension )[ nLen ] = '\0'; } } /* FILECASE */ if( s_iFileCase == HB_SET_CASE_LOWER ) { if( pFileName->szName ) hb_strlow( ( char * ) pFileName->szName ); if( pFileName->szExtension ) hb_strlow( ( char * ) pFileName->szExtension ); } else if( s_iFileCase == HB_SET_CASE_UPPER ) { if( pFileName->szName ) hb_strupr( ( char * ) pFileName->szName ); if( pFileName->szExtension ) hb_strupr( ( char * ) pFileName->szExtension ); } /* DIRCASE */ if( pFileName->szPath ) { if( s_iDirCase == HB_SET_CASE_LOWER ) hb_strlow( ( char * ) pFileName->szPath ); else if( s_iDirCase == HB_SET_CASE_UPPER ) hb_strupr( ( char * ) pFileName->szPath ); } hb_fsFNameMerge( ( char * ) szFileName, pFileName ); hb_xfree( pFileName ); } lpwFileName = hb_mbtowc( szFileName ); if( pszBuffer ) hb_xfree( pszBuffer ); return lpwFileName; }
void hb_cmdargUpdate( void ) { HB_TRACE( HB_TR_DEBUG, ( "hb_cmdargUpdate()" ) ); #if ! defined( HB_OS_WIN ) if( s_argc > 0 ) { # if defined( HB_OS_OS2 ) { PPIB ppib = NULL; APIRET ulrc; ulrc = DosGetInfoBlocks( NULL, &ppib ); if( ulrc == NO_ERROR ) { ulrc = DosQueryModuleName( ppib->pib_hmte, HB_SIZEOFARRAY( s_szAppName ), s_szAppName ); if( ulrc == NO_ERROR ) s_argv[ 0 ] = s_szAppName; } } # else /* NOTE: try to create absolute path from s_argv[ 0 ] if necessary */ { PHB_FNAME pFName = hb_fsFNameSplit( s_argv[ 0 ] ); HB_BOOL fInPath = HB_FALSE; if( ! pFName->szPath ) { char * pszPATH = hb_getenv( "PATH" ); if( pszPATH && *pszPATH ) { HB_PATHNAMES * pSearchPath = NULL, * pNextPath; hb_fsAddSearchPath( pszPATH, &pSearchPath ); pNextPath = pSearchPath; while( pNextPath ) { pFName->szPath = pNextPath->szPath; hb_fsFNameMerge( s_szAppName, pFName ); if( hb_fsFileExists( s_szAppName ) ) { /* even if the file is located using PATH then it does * not mean we will have absolute path here. It's not * good idea but PATH envvar can also contain relative * directories, f.e. "." or "bin" so we should add * current directory if necessary in code below. */ hb_xfree( pFName ); pFName = hb_fsFNameSplit( s_szAppName ); fInPath = HB_TRUE; break; } pNextPath = pNextPath->pNext; } hb_fsFreeSearchPath( pSearchPath ); if( ! fInPath ) pFName->szPath = NULL; } if( pszPATH ) hb_xfree( pszPATH ); } if( pFName->szPath ) { # if defined( HB_OS_HAS_DRIVE_LETTER ) if( pFName->szPath[ 0 ] != HB_OS_PATH_DELIM_CHR && ! pFName->szDrive ) # else if( pFName->szPath[ 0 ] != HB_OS_PATH_DELIM_CHR ) # endif { if( pFName->szPath[ 0 ] == '.' && pFName->szPath[ 1 ] == HB_OS_PATH_DELIM_CHR ) pFName->szPath += 2; s_szAppName[ 0 ] = HB_OS_PATH_DELIM_CHR; hb_fsCurDirBuff( 0, s_szAppName + 1, HB_PATH_MAX - 1 ); if( s_szAppName[ 1 ] != 0 ) { hb_strncat( s_szAppName, HB_OS_PATH_DELIM_CHR_STRING, HB_PATH_MAX - 1 ); hb_strncat( s_szAppName, pFName->szPath, HB_PATH_MAX - 1 ); pFName->szPath = hb_strdup( s_szAppName ); hb_fsFNameMerge( s_szAppName, pFName ); hb_xfree( HB_UNCONST( pFName->szPath ) ); s_argv[ 0 ] = s_szAppName; } } else if( fInPath ) s_argv[ 0 ] = s_szAppName; } hb_xfree( pFName ); } # endif } #endif }
static PHB_FILE s_fileExtOpen( PHB_FILE_FUNCS pFuncs, const char * pszFileName, const char * pDefExt, HB_FATTR nExFlags, const char * pPaths, PHB_ITEM pError ) { PHB_FILE pFile = NULL; #if defined( HB_OS_UNIX ) HB_BOOL fSeek = HB_FALSE; # if defined( HB_USE_LARGEFILE64 ) struct stat64 statbuf; # else struct stat statbuf; # endif #endif HB_BOOL fResult, fShared; int iMode; HB_FHANDLE hFile; char * pszFile; HB_SYMBOL_UNUSED( pFuncs ); fShared = ( nExFlags & ( FO_DENYREAD | FO_DENYWRITE | FO_EXCLUSIVE ) ) == 0; iMode = ( int ) ( nExFlags & ( FO_READ | FO_WRITE | FO_READWRITE ) ); pszFile = hb_fsExtName( pszFileName, pDefExt, nExFlags, pPaths ); hb_vmUnlock(); #if ! defined( HB_OS_UNIX ) fResult = HB_TRUE; #else # if defined( HB_USE_SHARELOCKS ) && ! defined( HB_USE_BSDLOCKS ) if( nExFlags & FXO_SHARELOCK ) { if( iMode == FO_WRITE && fShared ) { if( access( ( char * ) pszFile, R_OK ) == 0 || access( ( char * ) pszFile, F_OK ) != 0 ) { nExFlags = ( nExFlags ^ FO_WRITE ) | FO_READWRITE; iMode = FO_READWRITE; } else nExFlags ^= FXO_SHARELOCK; } else if( iMode == FO_READ && ! fShared ) { nExFlags &= ~ ( HB_FATTR ) ( FO_DENYREAD | FO_DENYWRITE | FO_EXCLUSIVE ); fShared = HB_TRUE; } } # endif hb_threadEnterCriticalSection( &s_fileMtx ); # if defined( HB_USE_LARGEFILE64 ) fResult = stat64( ( char * ) pszFile, &statbuf ) == 0; # else fResult = stat( ( char * ) pszFile, &statbuf ) == 0; # endif hb_fsSetIOError( fResult, 0 ); if( fResult ) { pFile = hb_fileFind( ( HB_ULONG ) statbuf.st_dev, ( HB_ULONG ) statbuf.st_ino ); if( pFile ) { if( ! fShared || ! pFile->shared || ( nExFlags & FXO_TRUNCATE ) != 0 ) { fResult = HB_FALSE; pFile = NULL; } else if( pFile->mode != FO_READWRITE && pFile->mode != iMode ) { iMode = FO_READWRITE; pFile = NULL; } else { pFile->used++; if( ( nExFlags & FXO_NOSEEKPOS ) == 0 ) { # if defined( HB_OS_VXWORKS ) fSeek = ! S_ISFIFO( statbuf.st_mode ); # else fSeek = ! S_ISFIFO( statbuf.st_mode ) && ! S_ISSOCK( statbuf.st_mode ); # endif } } } } else fResult = HB_TRUE; if( fResult && pFile == NULL ) #endif /* HB_OS_UNIX */ { hFile = hb_fsExtOpen( pszFile, NULL, nExFlags & ~ ( HB_FATTR ) ( FXO_DEFAULTS | FXO_COPYNAME ), NULL, NULL ); if( hFile != FS_ERROR ) { HB_ULONG device = 0, inode = 0; #if ! defined( HB_OS_UNIX ) hb_threadEnterCriticalSection( &s_fileMtx ); #else # if defined( HB_USE_LARGEFILE64 ) if( fstat64( hFile, &statbuf ) == 0 ) # else if( fstat( hFile, &statbuf ) == 0 ) # endif { device = ( HB_ULONG ) statbuf.st_dev; inode = ( HB_ULONG ) statbuf.st_ino; if( ( nExFlags & FXO_NOSEEKPOS ) == 0 ) { # if defined( HB_OS_VXWORKS ) fSeek = ! S_ISFIFO( statbuf.st_mode ); # else fSeek = ! S_ISFIFO( statbuf.st_mode ) && ! S_ISSOCK( statbuf.st_mode ); # endif } } #endif /* HB_OS_UNIX */ pFile = hb_fileNew( hFile, fShared, iMode, device, inode, HB_TRUE ); if( pFile->hFile != hFile ) { if( pFile->mode != FO_READWRITE && iMode == FO_READWRITE ) { HB_FHANDLE hTemp = pFile->hFileRO; pFile->hFileRO = pFile->hFile; pFile->hFile = hFile; pFile->mode = iMode; hFile = hTemp; } if( ! fShared || ! pFile->shared || pFile->mode != FO_READWRITE ) { fResult = HB_FALSE; if( pFile->hFileRO == FS_ERROR && pFile->uiLocks != 0 ) { pFile->hFileRO = hFile; hFile = FS_ERROR; } } if( pFile->uiLocks == 0 ) { #if ! defined( HB_USE_SHARELOCKS ) || defined( HB_USE_BSDLOCKS ) if( pFile->hFileRO != FS_ERROR ) { hb_fsClose( pFile->hFileRO ); pFile->hFileRO = FS_ERROR; } #endif if( hFile != FS_ERROR ) { hb_fsClose( hFile ); hFile = FS_ERROR; #if defined( HB_USE_SHARELOCKS ) && ! defined( HB_USE_BSDLOCKS ) /* TOFIX: possible race condition */ hb_fsLockLarge( pFile->hFile, HB_SHARELOCK_POS, HB_SHARELOCK_SIZE, FL_LOCK | FLX_SHARED ); #endif } } if( !fResult ) { if( pFile ) { --pFile->used; pFile = NULL; } if( hFile != FS_ERROR ) { /* TOFIX: possible race condition in MT mode, * close() is not safe due to existing locks * which are removed. */ hb_fsClose( hFile ); } } } #if ! defined( HB_OS_UNIX ) hb_threadLeaveCriticalSection( &s_fileMtx ); #endif } } #if defined( HB_OS_UNIX ) hb_threadLeaveCriticalSection( &s_fileMtx ); if( pFile && fSeek ) pFile = hb_fileposNew( pFile ); #endif if( ! fResult ) hb_fsSetError( ( nExFlags & FXO_TRUNCATE ) ? 5 : 32 ); if( ( nExFlags & FXO_COPYNAME ) != 0 && pFile ) hb_strncpy( ( char * ) HB_UNCONST( pszFileName ), pszFile, HB_PATH_MAX - 1 ); if( pError ) { hb_errPutFileName( pError, pszFile ); if( ! fResult ) { hb_errPutOsCode( pError, hb_fsError() ); hb_errPutGenCode( pError, ( HB_ERRCODE ) ( ( nExFlags & FXO_TRUNCATE ) ? EG_CREATE : EG_OPEN ) ); } } hb_xfree( pszFile ); hb_vmLock(); return pFile; }