297 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			297 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
| /* f4temp.c (c)Copyright Sequiter Software Inc., 1988-1996.  All rights reserved. */
 | |
| 
 | |
| #include "d4all.h"
 | |
| #ifdef __TURBOC__
 | |
|    #pragma hdrstop
 | |
| #endif
 | |
| 
 | |
| #ifdef S4TEMP
 | |
|    #include "t4test.h"
 | |
| #endif
 | |
| 
 | |
| #ifdef S4WINTEL
 | |
|    #ifndef S4IBMOS2
 | |
|       #ifndef __TURBOC__
 | |
|          #include <sys\locking.h>
 | |
|          #define S4LOCKING
 | |
|       #endif
 | |
|       #ifdef _MSC_VER
 | |
|          #include <sys\types.h>
 | |
|          #include <sys\locking.h>
 | |
|       #endif
 | |
|    #endif
 | |
| 
 | |
| /*   #include <sys\stat.h>*/
 | |
| /*   #include <share.h>*/
 | |
| #endif
 | |
| 
 | |
| /*#include <fcntl.h>*/
 | |
| 
 | |
| #ifdef S4CB51
 | |
| int S4FUNCTION file4temp( FILE4 *file, CODE4 *c4, char *buf, const int auto_remove )
 | |
| {
 | |
|    int i, save_flag, rc, old_excl ;
 | |
|    #ifndef S4OPTIMIZE_OFF
 | |
|       int old_opt_wr, tf_do_buffer ;
 | |
|       unsigned long tf_hash_init ;
 | |
|       char tf_type, tf_buffer_writes, tf_write_buffer ;
 | |
|       long tf_len ;
 | |
|       LINK4 tf_link ;
 | |
|    #endif
 | |
|    time_t t ;
 | |
|    char name[20], *name_ptr ;
 | |
| 
 | |
|    #ifdef E4PARM_HIGH
 | |
|       if ( file == 0 || c4 == 0  )
 | |
|          return error4( c4, e4parm_null, E90623 ) ;
 | |
|    #endif
 | |
| 
 | |
|    #ifndef S4OPTIMIZE_OFF
 | |
|       if ( auto_remove && c4->opt.numBuffers != 0 )
 | |
|       {
 | |
|          if ( c4->opt.forceCurrent == 0 )
 | |
|          {
 | |
|             memset( (void *)file, 0, sizeof( FILE4 ) ) ;
 | |
|             file->isTemp = 1 ;
 | |
|             file->codeBase = c4 ;
 | |
|             file->fileCreated = 0 ;
 | |
|             #ifndef S4OFF_MULTI
 | |
|                file->lowAccessMode = 1 ;
 | |
|             #endif
 | |
|             old_opt_wr = c4->optimizeWrite ;
 | |
|             c4->optimizeWrite = 1 ;
 | |
|             file4optimize( file, 1, OPT4OTHER ) ;
 | |
|             c4->optimizeWrite = old_opt_wr ;
 | |
|             if ( buf != 0 )
 | |
|                file->name = buf ;
 | |
|             return 0 ;
 | |
|          }
 | |
|          else
 | |
|          {
 | |
|             tf_hash_init = file->hashInit ;
 | |
|             tf_len = file->len ;
 | |
|             tf_type = file->type ;
 | |
|             tf_buffer_writes = file->bufferWrites ;
 | |
|             tf_do_buffer = file->doBuffer ;
 | |
|             tf_write_buffer = file->writeBuffer ;
 | |
|             memcpy( (void *)&tf_link, (void *)&file->link, sizeof( LINK4 ) ) ;
 | |
|          }
 | |
|       }
 | |
|    #endif
 | |
| 
 | |
|    name_ptr = buf ;
 | |
|    if ( name_ptr == 0 )
 | |
|       name_ptr = name ;
 | |
| 
 | |
|    strcpy( name_ptr, "TEMP" ) ;
 | |
|    for ( i = 0 ; i < 100 ; i++ )
 | |
|    {
 | |
|       u4delaySec() ;
 | |
|       time( &t ) ;
 | |
|       t %= 10000L ;
 | |
| 
 | |
|       c4ltoa45( t, name_ptr + 4, -4 ) ;
 | |
|       strcpy( name_ptr + 8, ".TMP" ) ;
 | |
| 
 | |
|       save_flag = c4->errCreate ;
 | |
|       c4->errCreate = 0 ;
 | |
| 
 | |
|       #ifndef S4OFF_MULTI
 | |
|          old_excl = c4->accessMode ;
 | |
|          c4->accessMode = 1 ; /* all temporary files are for exclusive access only */
 | |
|       #endif
 | |
|       rc = file4create( file, c4, name_ptr, (int)(buf == 0) ) ;
 | |
|       #ifndef S4OFF_MULTI
 | |
|          c4->accessMode = old_excl ;
 | |
|       #endif
 | |
| 
 | |
|       c4->errCreate = save_flag ;
 | |
|       if ( rc < 0 )
 | |
|          return -1 ;
 | |
|       if ( rc == 0 )
 | |
|       {
 | |
|          if ( auto_remove )
 | |
|             file->isTemp = 1 ;
 | |
|          #ifndef S4OPTIMIZE_OFF
 | |
|             if ( auto_remove && c4->opt.numBuffers != 0 )
 | |
|             {
 | |
|                if ( c4->opt.forceCurrent == 1 )
 | |
|                {
 | |
|                   file->hashInit = tf_hash_init ;
 | |
|                   file->len = tf_len ;
 | |
|                   file->type = tf_type ;
 | |
|                   file->bufferWrites = tf_buffer_writes ;
 | |
|                   file->doBuffer = tf_do_buffer ;
 | |
|                   file->writeBuffer = tf_write_buffer ;
 | |
|                   memcpy( (void *)&file->link, (void *)&tf_link, sizeof( LINK4 ) ) ;
 | |
|                }
 | |
|                else
 | |
|                {
 | |
|                   old_opt_wr = c4->optimizeWrite ;
 | |
|                   c4->optimizeWrite = 1 ;
 | |
|                   file4optimize( file, 1, OPT4OTHER ) ;
 | |
|                   c4->optimizeWrite = old_opt_wr ;
 | |
|                }
 | |
|             }
 | |
|          #endif
 | |
|          return  0 ;
 | |
|       }
 | |
|    }
 | |
| 
 | |
|    return error4( c4, e4create, E80605 ) ;
 | |
| }
 | |
| #endif
 | |
| 
 | |
| int file4tempLow( FILE4 *file, CODE4 *c4, const int autoRemove )
 | |
| {
 | |
|    int i, saveFlag, rc ;
 | |
|    #ifndef S4OFF_MULTI
 | |
|       int oldExcl ;
 | |
|    #endif
 | |
|    #ifndef S4OPTIMIZE_OFF
 | |
|       int oldOptWr ;
 | |
|       int tfDoBuffer = 0 ;
 | |
|       long tfHashInit = -1L ;
 | |
|       char tfType = (char)-1 ;
 | |
|       char tfBufferWrites = (char)-1 ;
 | |
|       char tfWriteBuffer = (char)-1 ;
 | |
|       long tfLen = -1L ;
 | |
|       LINK4 tfLink ;
 | |
|    #endif
 | |
|    time_t t ;
 | |
|    char name[255], *env ;
 | |
|    char fileName[13] ;
 | |
|    char drive[3] ;
 | |
| 
 | |
|    #ifdef E4PARM_HIGH
 | |
|       if ( file == 0 || c4 == 0  )
 | |
|          return error4( c4, e4parm_null, E90618 ) ;
 | |
|    #endif
 | |
| 
 | |
|    #ifndef S4OPTIMIZE_OFF
 | |
|       code4memStartMaxSet( c4, c4->memMaxPercent ) ;  /* start optimization if not enabled and not suspended */
 | |
| 
 | |
|       memset( &tfLink, 0, sizeof( tfLink ) ) ;
 | |
| 
 | |
|       if ( autoRemove && c4->opt.numBuffers != 0 )
 | |
|       {
 | |
|          if ( c4->opt.forceCurrent == 0 )
 | |
|          {
 | |
|             memset( (void *)file, 0, sizeof( FILE4 ) ) ;
 | |
|             file->isTemp = 1 ;
 | |
|             file->codeBase = c4 ;
 | |
|             file->fileCreated = 0 ;
 | |
|             #ifndef S4OFF_MULTI
 | |
|                file->lowAccessMode = OPEN4DENY_RW ;
 | |
|             #endif
 | |
|             oldOptWr = c4->optimizeWrite ;
 | |
|             c4->optimizeWrite = 1 ;
 | |
|             file4optimize( file, 1, OPT4OTHER ) ;
 | |
|             c4->optimizeWrite = oldOptWr ;
 | |
|             return 0 ;
 | |
|          }
 | |
|          else
 | |
|          {
 | |
|             tfHashInit = file->hashInit ;
 | |
|             tfLen = file->len ;
 | |
|             tfType = file->type ;
 | |
|             tfBufferWrites = file->bufferWrites ;
 | |
|             tfDoBuffer = file->doBuffer ;
 | |
|             tfWriteBuffer = file->writeBuffer ;
 | |
|             memcpy( (void *)&tfLink, (void *)&file->link, sizeof( LINK4 ) ) ;
 | |
|             #ifdef E4ANALYZE
 | |
|                if ( file->name != 0 )
 | |
|                   return error4( c4, e4struct, E90618 ) ;
 | |
|             #endif
 | |
|          }
 | |
|       }
 | |
|    #endif
 | |
| 
 | |
|    saveFlag = c4->errCreate ;
 | |
|    c4->errCreate = 0 ;
 | |
|    #ifndef S4OFF_MULTI
 | |
|       oldExcl = c4->accessMode ;
 | |
|       c4->accessMode = OPEN4DENY_RW ; /* all temporary files are for exclusive access only */
 | |
|    #endif
 | |
| 
 | |
|    env = u4environ( "TEMP", 0 ) ;
 | |
|    if ( env == 0 )
 | |
|       env = u4environ( "TMP", 0 ) ;
 | |
|    if ( env == 0 )
 | |
|    {
 | |
|       drive[0] = 0 ;
 | |
|       env = "" ;
 | |
|    }
 | |
|    else
 | |
|    {
 | |
|       if ( env[1] == ':' )
 | |
|       {
 | |
|          drive[0] = env[0] ;
 | |
|          drive[1] = env[1] ;
 | |
|          drive[2] = 0 ;
 | |
|       }
 | |
|       else
 | |
|          drive[0] = 0 ;
 | |
|    }
 | |
| 
 | |
|    strcpy( fileName, "TEMP" ) ;
 | |
|    strcpy( fileName + 8, ".TMP" ) ;
 | |
|    fileName[12] = 0 ;
 | |
| 
 | |
|    for ( i = 0 ;; )
 | |
|    {
 | |
|       if ( i >= 100 )
 | |
|       {
 | |
|          rc = error4( c4, e4create, E80605 ) ;
 | |
|          break ;
 | |
|       }
 | |
|       i++ ;
 | |
| 
 | |
|       time( &t ) ;
 | |
|       t %= 10000L ;
 | |
|       c4ltoa45( t, fileName + 4, -4 ) ;
 | |
|       u4nameMake( name, sizeof( name ), drive, env + strlen( drive ), fileName ) ;
 | |
| 
 | |
|       rc = file4create( file, c4, name, 1 ) ;
 | |
|       if ( rc <= 0 )
 | |
|          break ;
 | |
| 
 | |
|       u4delayHundredth( 50 ) ;
 | |
|    }
 | |
| 
 | |
|    #ifndef S4OFF_MULTI
 | |
|       c4->accessMode = oldExcl ;
 | |
|    #endif
 | |
|    c4->errCreate = saveFlag ;
 | |
| 
 | |
|    if ( rc < 0 )
 | |
|       return error4stack( c4, (short)rc, E90618 ) ;
 | |
|    if ( autoRemove )
 | |
|       file->isTemp = 1 ;
 | |
|    #ifndef S4OPTIMIZE_OFF
 | |
|       if ( autoRemove && c4->opt.numBuffers != 0 )
 | |
|       {
 | |
|          if ( c4->opt.forceCurrent == 1 )
 | |
|          {
 | |
|             file->hashInit = tfHashInit ;
 | |
|             file->len = tfLen ;
 | |
|             file->type = tfType ;
 | |
|             file->bufferWrites = tfBufferWrites ;
 | |
|             file->doBuffer = tfDoBuffer ;
 | |
|             file->writeBuffer = tfWriteBuffer ;
 | |
|             memcpy( (void *)&file->link, (void *)&tfLink, sizeof( LINK4 ) ) ;
 | |
|          }
 | |
|          else
 | |
|          {
 | |
|             oldOptWr = c4->optimizeWrite ;
 | |
|             c4->optimizeWrite = 1 ;
 | |
|             file4optimize( file, 1, OPT4OTHER ) ;
 | |
|             c4->optimizeWrite = oldOptWr ;
 | |
|          }
 | |
|       }
 | |
|    #endif
 | |
|    return 0 ;
 | |
| }
 | |
| 
 | |
| 
 |