/* * DIPCliOpen */ dig_fhandle DIGCLIENT DIGCliOpen( const char *path, dig_open mode ) { dig_fhandle ret; int flags; flags = O_BINARY; if( mode & DIG_READ ) flags |= O_RDONLY; if( mode & DIG_WRITE ) flags |= O_WRONLY; if( mode & DIG_TRUNC ) flags |= O_TRUNC; if( mode & DIG_CREATE ) { flags |= O_CREAT; ret = (dig_fhandle)sopen4( path, flags, SH_DENYWR, S_IRWXU | S_IRWXG | S_IRWXO ); } else { ret = (dig_fhandle)sopen3( path, flags, SH_DENYWR ); } return( ret ); }
b_file *Openf( char *f, f_attrs attrs ) { // Open a file. int retc; long int fpos; #if defined( __WATCOMC__ ) || !defined( __UNIX__ ) int share; share = SH_COMPAT; if( attrs & S_DENYRW ) { share = SH_DENYRW; } else if( attrs & S_DENYWR ) { share = SH_DENYWR; } else if( attrs & S_DENYRD ) { share = SH_DENYRD; } else if( attrs & S_DENYNO ) { share = SH_DENYNO; } #endif if( attrs & WRONLY ) { attrs |= WRITE_ONLY; if( attrs & APPEND ) { retc = sopen4( f, O_WRONLY | O_BINARY | O_CREAT, share, PMODE_RW ); } else { retc = sopen4( f, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, share, PMODE_RW ); } } else if( attrs & RDONLY ) { retc = sopen3( f, O_RDONLY | O_BINARY, share ); } else { // if( attrs & RDWR ) { retc = sopen4( f, O_RDWR | O_BINARY | O_CREAT, share, PMODE_RW ); } if( retc < 0 ) { FSetSysErr( NULL ); return( NULL ); } fpos = 0; if( attrs & APPEND ) { fpos = lseek( retc, 0, SEEK_END ); if( fpos < 0 ) { FSetSysErr( NULL ); close( retc ); return( NULL ); } } return( _AllocFile( retc, attrs, fpos ) ); }
IntlData *LoadInternationalData( const char *file_prefix ) { IntlData *data; int fh; res_language_enumeration language; unsigned len; char *drive; char *dir; char cmd_name[_MAX_PATH]; char split_buff[_MAX_PATH2]; char base[16]; language = _WResLanguage(); if( language == RLE_ENGLISH ) { return( NULL ); } if( imageName( cmd_name ) == NULL ) { return( NULL ); } _splitpath2( cmd_name, split_buff, &drive, &dir, NULL, NULL ); len = strlen( file_prefix ); if( len > 6 ) { len = 6; } memcpy( base, file_prefix, len ); base[len++] = '0'; base[len++] = '0' + language; base[len] = '\0'; _makepath( cmd_name, drive, dir, base, "." LOCALE_DATA_EXT ); fh = sopen3( cmd_name, O_RDONLY | O_BINARY, SH_DENYWR ); if( fh == -1 ) { return( NULL ); } data = getData( fh ); if( data != NULL ) { if( data->usage_text == NULL || data->errors_text == NULL ) { FreeInternationalData( data ); data = NULL; } } close( fh ); return( data ); }
RET_T InsFile( const char *name, BOOLEAN envsearch ) /********************************************************** * Open file named name, and push it into stream. If can't find name, it * tries an implicit suffix search (possibly using the env variable PATH) */ { SENT *tmp; int fh; char path[_MAX_PATH]; assert( name != NULL ); if( TrySufPath( path, name, NULL, envsearch ) == RET_SUCCESS ) { PrtMsg( DBG | INF | LOC | ENTERING_FILE, path ); fh = sopen3( path, O_RDONLY | O_BINARY, SH_DENYWR ); if( fh == -1 ) { return( RET_ERROR ); } tmp = getSENT( SENT_FILE ); tmp->free = TRUE; tmp->data.file.name = StrDupSafe( path ); pushFH( tmp, fh ); if( !Glob.overide ) { UnGetCH( EOL ); InsString( path, FALSE ); InsString( "$+$(__MAKEFILES__)$- ", FALSE ); DefMacro( "__MAKEFILES__" ); } return( RET_SUCCESS ); } return( RET_ERROR ); }
/* * AutoSaveInit - initialize for auto-save */ void AutoSaveInit( void ) { char path[FILENAME_MAX]; char path2[FILENAME_MAX]; char as_path[FILENAME_MAX]; char asl_path[FILENAME_MAX]; int len; int cnt; FILE *f; int pid; int ch; int handle; int off; // int old_len; int i; /* * initialize tmpname */ #ifdef __UNIX__ strcpy( currTmpName,"aaaaaaaaaaaa.tmp" ); #else strcpy( currTmpName,"aaaaaaaa.tmp" ); #endif pid = getpid(); itoa( pid, path, 36 ); len = strlen( path ); memcpy( &currTmpName[TMP_FNAME_LEN - len], path, len ); #ifdef __QNX__ { int len2, len3; int nid, uid; nid = getnid(); itoa( nid, path, 36 ); len2 = strlen( path ); memcpy( &currTmpName[TMP_FNAME_LEN - len - len2], path, len2 ); uid = getuid(); itoa( uid, path, 36 ); len3 = strlen( path ); memcpy( &currTmpName[TMP_FNAME_LEN - len - len2 - len3], path, len3 ); memcpy( &checkFileName[EXTRA_EXT_OFF], path, len3 ); memcpy( &checkFileTmpName[EXTRA_EXT_OFF], path, len3 ); memcpy( &lockFileName[EXTRA_EXT_OFF], path, len3 ); } #endif /* * check if we need to recover lost files */ if( EditFlags.RecoverLostFiles ) { cnt = 0; MakeTmpPath( as_path, checkFileName ); MakeTmpPath( asl_path, lockFileName ); off = strlen( as_path ) - 5; for( ch = START_CHAR; ch <= END_CHAR; ch++ ) { as_path[off] = ch; asl_path[off] = ch; handle = sopen3( as_path, O_RDONLY | O_TEXT, SH_DENYRW ); if( handle < 0 ) { continue; } f = fdopen( handle, "r" ); if( f != NULL ) { while( fgets( path2, FILENAME_MAX, f ) != NULL ) { for( i = strlen( path2 ); i && isWSorCtrlZ( path2[i - 1] ); --i ) { path2[i - 1] = '\0'; } NextWord1( path2, path ); RemoveLeadingSpaces( path2 ); NewFile( path, FALSE ); AddString2( &(CurrentFile->name), path2 ); SetFileWindowTitle( CurrentWindow, CurrentInfo, TRUE ); FileSPVAR(); CurrentFile->modified = TRUE; CurrentFile->check_readonly = TRUE; FTSRunCmds( path2 ); cnt++; } fclose( f ); remove( as_path ); } else { close( handle ); } remove( asl_path ); } if( cnt == 0 ) { MyPrintf( "No files recovered!\n" ); ExitEditor( -1 ); } Message1( "%d files recovered", cnt ); } if( EditVars.AutoSaveInterval == 0 ) { return; } // old_len = strlen( lockFileName ); MakeTmpPath( path, lockFileName ); len = strlen( path ) - strlen( lockFileName ); off = len + CHAR_OFF; for( ch = START_CHAR; ch <= END_CHAR; ch++ ) { path[off] = ch; lockFileHandle = sopen4( path, O_CREAT | O_TRUNC | O_RDWR | O_TEXT, SH_DENYRW, PMODE_RW ); if( lockFileHandle > 0 ) { break; } } if( lockFileHandle < 0 ) { // MyPrintf( "Too many editors running!\n" ); MyPrintf( "Error opening temp file - '%s'\n", strerror( errno ) ); ExitEditor( -1 ); } lockFileName[CHAR_OFF] = ch; checkFileName[CHAR_OFF] = ch; checkFileTmpName[CHAR_OFF] = ch; } /* AutoSaveInit */
/* * LostFileCheck - check if there are any lost files out there */ bool LostFileCheck( void ) { char path[FILENAME_MAX]; vi_key key; char ch; int off; int handle; MakeTmpPath( path, lockFileName ); off = strlen( path ) - 5; for( ch = START_CHAR; ch <= END_CHAR; ch++ ) { path[off] = ch; handle = sopen3( path, O_RDONLY | O_TEXT, SH_DENYRW ); if( handle > 0 ) { MakeTmpPath( path, checkFileName ); path[off] = ch; if( access( path, F_OK ) == -1 ) { MakeTmpPath( path, lockFileName ); path[off] = ch; close( handle ); handle = -1; remove( path ); } else { break; } } } if( handle > 0 ) { close( handle ); if( !EditFlags.RecoverLostFiles ) { if( !EditFlags.IgnoreLostFiles ) { #ifdef __WIN__ CloseStartupDialog(); key = GetAutosaveResponse(); handleKey( key ); ShowStartupDialog(); return( TRUE ); #else SetPosToMessageLine(); MyPrintf( "Files have been lost since your last session, do you wish to:\n" ); MyPrintf( "\ti)gnore\n\tr)ecover\n\tq)uit\n" ); for( ;; ) { key = GetKeyboard(); if( handleKey( key ) ) { return( TRUE ); } } #endif } else { remove( path ); return( FALSE ); } } } else { if( EditFlags.RecoverLostFiles ) { EditFlags.RecoverLostFiles = FALSE; EditFlags.NoInitialFileLoad = FALSE; } } return( FALSE ); } /* LostFileCheck */
/* * BoundDataInit - check if data is bound to our exe */ void BoundDataInit( void ) { int h; size_t size; char buff[sizeof( MAGIC_COOKIE ) + sizeof( bind_size )]; char *tmp; unsigned dataFcnt; /* * get trailer */ h = sopen3( EXEName, O_RDONLY | O_BINARY, SH_COMPAT ); if( h == -1 ) { return; } lseek( h, - (long)sizeof( buff ), SEEK_END ); read( h, buff, sizeof( buff ) ); /* * seek to start of data */ if( memcmp( buff, MAGIC_COOKIE, sizeof( MAGIC_COOKIE ) ) ) { close( h ); return; } size = *(bind_size *)( buff + sizeof( MAGIC_COOKIE ) ); dataStart = -(long)( size + sizeof( buff ) ); lseek( h, dataStart, SEEK_END ); /* * get everything */ BndMemory = MemAlloc( size ); read( h, BndMemory, size ); close( h ); /* * get number of files, and get space to store data */ dataFcnt = *(bind_size *)BndMemory; dataOffsets = MemAlloc( dataFcnt * sizeof( bind_size ) ); entryCounts = MemAlloc( dataFcnt * sizeof( bind_size ) ); /* * get file names */ tmp = BndMemory + sizeof( bind_size ); size = *(bind_size *)tmp; tmp += sizeof( bind_size ); dataFnames = MemAlloc( size ); memcpy( dataFnames, tmp, size ); tmp += size; /* * copy over file offset and linenumber data */ size = dataFcnt * sizeof( bind_size ); memcpy( dataOffsets, tmp, size ); tmp += size; memcpy( entryCounts, tmp, size ); BoundData = true; } /* BoundDataInit */
/* * SpecialOpen - open a file or exe */ bool SpecialOpen( const char *fn, GENERIC_FILE *gf, bool bounddata ) { long shift = 0; int h, i; unsigned char len; vi_rc rc; /* * process bound file */ if( BoundData ) { if( !strcmp( fn, CONFIG_FILE ) ) { i = 0; } else { i = Tokenize( dataFnames, fn, true ); } if( i != TOK_INVALID && bounddata ) { shift = dataStart + dataOffsets[i]; gf->type = GF_BOUND; EditFlags.BndMemoryLocked = true; if( BndMemory == NULL ) { h = sopen3( EXEName, O_RDONLY | O_BINARY, SH_COMPAT ); if( h == -1 ) { return( false ); } lseek( h, shift, SEEK_END ); read( h, &len, 1 ); gf->data.handle = h; } else { shift -= dataStart; gf->data.pos = BndMemory + shift; len = gf->data.pos[0]; gf->data.pos++; } gf->gf.a.currline = 0; gf->gf.a.maxlines = entryCounts[i]; gf->gf.a.length = len; return( true ); } } /* * special case - open current buffer */ if( fn[0] == '.' && fn[1] == '\0' ) { gf->type = GF_BUFFER; gf->data.cfile = CurrentFile; rc = GimmeLinePtr( 1, CurrentFile, &(gf->gf.b.cfcb), &(gf->gf.b.cline)); return( rc == ERR_NO_ERR ); } /* * process regular file */ gf->type = GF_FILE; gf->data.f = GetFromEnvAndOpen( fn ); return( gf->data.f != NULL ); } /* SpecialOpen */
pch_absorb PCHeaderAbsorb( char *include_file ) /*********************************************/ { pch_absorb ret; int status; auto jmp_buf restore_state; #ifndef NDEBUG clock_t start; clock_t stop; start = clock(); #endif if( ErrCount != 0 ) { return( PCHA_ERRORS_PRESENT ); } if( CompFlags.fhw_switch_used ) { return( PCHA_IGNORE ); } pchFile = sopen3( PCHFileName(), O_RDONLY | O_BINARY, SH_DENYWR ); if( pchFile == -1 ) { return( PCHA_NOT_PRESENT ); } ioBuffer = CMemAlloc( IO_BUFFER_SIZE ); pch_buff_eob = ioBuffer + IO_BUFFER_SIZE; pch_buff_cur = pch_buff_eob; ret = PCHA_OK; abortData = &restore_state; status = setjmp( restore_state ); if( status == 0 ) { if( initialRead() == 0 ) { ret = PCHA_NOT_PRESENT; } else { auto precompiled_header_header header; #ifdef OPT_BR unsigned long br_posn; #endif PCHReadVar( header ); #ifdef OPT_BR br_posn = header.browse_info; #endif if( headerIsOK( &header ) ) { if( ! stalePCH( include_file ) ) { execInitFunctions( false ); execControlFunctions( false, readFunctions ); execFiniFunctions( false ); #ifdef OPT_BR if( 0 != br_posn ) { BrinfPchRead(); } #endif } else { ret = PCHA_STALE; } } else { pchWarn( WARN_PCH_CONTENTS_HEADER_ERROR ); ret = PCHA_HEADER; } } } else { CErr1( ERR_PCH_READ_ERROR ); } abortData = NULL; CMemFreePtr( &ioBuffer ); close( pchFile ); if( CompFlags.pch_debug_info_opt && ret == PCHA_OK ) { CompFlags.pch_debug_info_read = true; } #ifndef NDEBUG stop = clock(); printf( "%u ticks to load pre-compiled header\n", (unsigned)( stop - start ) ); #endif return( ret ); }
void PCHPerformReloc( pch_reloc_index ri ) /****************************************/ { long start_position; long stop_position; unsigned reloc_size; char *volatile pch_fname; // must be preserved by setjmp() int status; jmp_buf restore_state; #ifndef NDEBUG clock_t start; clock_t stop; start = clock(); #endif if( ErrCount != 0 ) { return; } start_position = relocInfo[ ri ].start; if( start_position == 0 ) { return; } stop_position = relocInfo[ ri ].stop; pch_fname = PCHFileName(); pchFile = sopen3( pch_fname, O_RDWR | O_BINARY | O_EXCL, SH_DENYRW ); if( pchFile == -1 ) { CErr2p( ERR_PCH_OPEN_ERROR, pch_fname ); return; } DbgAssert( ( stop_position - start_position ) < UINT_MAX ); reloc_size = stop_position - start_position; ioBuffer = CMemAlloc( reloc_size ); abortData = &restore_state; status = setjmp( restore_state ); if( status == 0 ) { if( lseek( pchFile, start_position, SEEK_SET ) != start_position ) { fail(); } if( read( pchFile, ioBuffer, reloc_size ) != reloc_size ) { fail(); } if( relocFunctions[ri]( ioBuffer, reloc_size ) == PCHCB_ERROR ) { fail(); } if( lseek( pchFile, -(long)reloc_size, SEEK_CUR ) != start_position ) { fail(); } if( write( pchFile, ioBuffer, reloc_size ) != reloc_size ) { fail(); } // keep this PCH file pch_fname = NULL; } else { CErr1( ERR_PCH_WRITE_ERROR ); } abortData = NULL; CMemFreePtr( &ioBuffer ); close( pchFile ); if( pch_fname != NULL ) { // write error occurred; delete PCH file remove( pch_fname ); } #ifndef NDEBUG stop = clock(); printf( "%u ticks to relocate pre-compiled header (%u section)\n", (unsigned)( stop - start ), ri ); #endif }