/* * PromptThisFileForSave */ bool PromptThisFileForSave( const char *filename ) { #ifdef __WIN__ info *cinfo; HWND hwnd_old = NO_WINDOW; while( isspace( *filename ) ) { filename++; } for( cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next ) { if( SameFile( cinfo->CurrentFile->name, filename ) ) { if( cinfo->CurrentFile != NULL && cinfo->CurrentFile->dup_count == 0 && cinfo->CurrentFile->modified ) { BringUpFile( cinfo, true ); /* we have a modified file, so bring to the front */ BringWindowToTop( root_window_id ); hwnd_old = SetFocus( root_window_id ); // file modified -- so prompt for save FilePromptForSaveChanges( CurrentFile ); } } } if( !BAD_ID( hwnd_old ) ) { SetWindowPos( root_window_id, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE ); SetFocus( hwnd_old ); } #else filename = filename; #endif return( true ); } /* PromptThisFileForSave */
/* * QueryFile */ bool QueryFile( const char *filename ) { info *cinfo; while( isspace( *filename ) ) { filename++; } for( cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next ) { if( SameFile( cinfo->CurrentFile->name, filename ) ) { return( true ); } } return( false ); } /* QueryFile */
void WriteNewLib( void ) { char tmp[ _MAX_PATH + 1 ]; char *bak,*lib,*out; lib = Options.input_name; if( Options.output_name != NULL && !SameFile( lib, Options.output_name ) ) { out = Options.output_name; } else { out = MakeTmpName( tmp ); } if( Options.export_list_file ) { ExportListFile = LibOpen( Options.export_list_file, LIBOPEN_BINARY_WRITE | O_CREAT); } else { ExportListFile = NULL; } NewLibrary = LibOpen( out, LIBOPEN_BINARY_WRITE ); if( NewLibrary == NULL ) { if( out == tmp ) { FatalError( ERR_CANT_OPEN, out, "Cannot create temporary file" ); } else { FatalError( ERR_CANT_OPEN, out, strerror( errno ) ); } } WriteFileTable(); LibClose( NewLibrary ); if( ExportListFile != NULL ) { LibClose( ExportListFile ); } ResetInputLibs();//closes all input libs if( out == tmp ) { bak = MakeBakName(); if( access( bak, F_OK ) == 0 && remove( bak ) != 0 ) { FatalError( ERR_CANT_REMOVE, bak ); } if( access( lib, F_OK ) == 0 && rename( lib, bak ) != 0 ) { FatalError( ERR_CANT_REPLACE, lib, strerror( errno ) ); } if( rename( tmp, lib ) != 0 ) { rename( bak, lib ); FatalError( ERR_CANT_REPLACE, lib, strerror( errno ) ); } if( Options.no_backup ) { remove( bak ); } } }
bool MoveFile(const AString& src, const AString& dst, bool binary) { const ADVBConfig& config = ADVBConfig::Get(); bool success = false; if (SameFile(src, dst)) { config.printf("File move: '%s' and '%s' are the same file", src.str(), dst.str()); success = true; } else if (rename(src, dst) == 0) success = true; else if (CopyFile(src, dst, binary)) { if (remove(src) == 0) success = true; else config.logit("Failed to remove '%s' after copy", src.str()); } return success; }
bool FindFName( void ) { //=================== ftnfile *fcb; char *fname; ConnectFile(); fname = IOCB->fileinfo->filename; fcb = Files; for(;;) { if( fcb == NULL ) return( FALSE ); if( ( IOCB->fileinfo != fcb ) && SameFile( fname, fcb->filename ) ) break; fcb = fcb->link; } // file already connected DiscoFile( IOCB->fileinfo ); IOCB->fileinfo = fcb; return( TRUE ); }
bool CopyFile(const AString& src, const AString& dst, bool binary) { const ADVBConfig& config = ADVBConfig::Get(); AStdFile fp1, fp2; bool success = false; if (SameFile(src, dst)) { config.printf("File copy: '%s' and '%s' are the same file", src.str(), dst.str()); success = true; } else if (fp1.open(src, binary ? "rb" : "r")) { if (fp2.open(dst, binary ? "wb" : "w")) { success = CopyFile(fp1, fp2); if (!success) config.logit("Failed to copy '%s' to '%s': transfer failed", src.str(), dst.str()); } else config.logit("Failed to copy '%s' to '%s': failed to open '%s' for writing", src.str(), dst.str(), dst.str()); } else config.logit("Failed to copy '%s' to '%s': failed to open '%s' for reading", src.str(), dst.str(), src.str()); return success; }
/* * EditFile - read a file into text */ vi_rc EditFile( char *name, bool dammit ) { char *fn, **list, *currfn; int i, cnt, ocnt; int j, len; window_id wn = NO_WINDOW; char cdir[FILENAME_MAX]; info *ci, *il; bool usedir = false; char mask[FILENAME_MAX]; bool reset_dir; int index; char *altname = NULL; vi_rc rc; fn = MemAlloc( FILENAME_MAX ); /* * get file name */ strcpy( cdir, CurrentDirectory ); reset_dir = false; RemoveLeadingSpaces( name ); if( name[0] == '$' ) { EliminateFirstN( name, 1 ); usedir = true; } fn[0] = 0; // if( NextWord1( name, fn ) <= 0 ) if( GetStringWithPossibleQuote2( name, fn, false ) != ERR_NO_ERR ) { usedir = true; mask[0] = '*'; mask[1] = 0; } if( usedir ) { if( EditFlags.ExMode ) { MemFree( fn ); return( ERR_INVALID_IN_EX_MODE ); } len = strlen( fn ); if( len > 0 ) { i = len - 1; strcpy( mask, fn ); cnt = 0; while( i >= 0 ) { if( fn[i] == FILE_SEP ) { for( j = i + 1; j <= len; j++ ) { mask[j - (i + 1)] = fn[j]; } cnt = i; break; } i--; } fn[cnt] = 0; } if( fn[0] != 0 ) { rc = SelectFileOpen( fn, &fn, mask, true ); } else { #ifdef __WIN__ if( name[0] == '\0' ) { altname = MemAlloc( 1000 ); rc = SelectFileOpen( CurrentDirectory, &altname, mask, true ); NextWord1( altname, fn ); // if multiple, kill path if( isMultipleFiles( altname ) ) { NextWord1( altname, fn ); // get 1st name } } else { rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); } #else rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); #endif } if( altname ) { name = altname; } if( rc != ERR_NO_ERR || fn[0] == 0 ) { MemFree( fn ); SetCWD( cdir ); return( rc ); } } /* * loop through all files */ rc = ERR_NO_ERR; EditFlags.WatchForBreak = true; #ifdef __WIN__ ToggleHourglass( true ); #endif do { if( IsDirectory( fn ) ) { if( EditFlags.ExMode ) { rc = ERR_INVALID_IN_EX_MODE; reset_dir = true; break; } rc = SelectFileOpen( fn, &fn, "*", false ); if( rc != ERR_NO_ERR ) { reset_dir = true; break; } if( fn[0] == 0 ) { reset_dir = true; rc = ERR_NO_ERR; break; } } currfn = fn; ocnt = cnt = ExpandFileNames( currfn, &list ); if( !cnt ) { cnt = 1; } else { currfn = list[0]; } /* * loop through all expanded files */ index = 1; while( cnt > 0 ) { cnt--; /* * quit current file if ! specified, else just save current state */ if( dammit ) { ci = InfoHead; if( CurrentInfo == ci ) { ci = ci->next; } RemoveFromAutoSaveList(); #ifdef __WIN__ CloseAChildWindow( CurrentWindow ); #else CloseAWindow( CurrentWindow ); #endif FreeUndoStacks(); FreeMarkList(); FreeEntireFile( CurrentFile ); MemFree( DeleteLLItem( (ss **)&InfoHead, (ss **)&InfoTail, (ss *)CurrentInfo ) ); CurrentInfo = NULL; CurrentWindow = NO_WINDOW; } else { ci = CurrentInfo; SaveCurrentInfo(); wn = CurrentWindow; } /* * see if new file is already being edited */ SaveCurrentInfo(); for( il = InfoHead; il != NULL; il = il->next ) { if( SameFile( il->CurrentFile->name, currfn ) ) { break; } if( strcmp( CurrentDirectory, il->CurrentFile->home ) ) { /* directory has changed -- check with full path * note that this will fail if an absolute path * was specified thus we do the regular check first */ char path[FILENAME_MAX]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath( il->CurrentFile->name, drive, dir, fname, ext ); if( !strlen( drive ) ) { _splitpath( il->CurrentFile->home, drive, NULL, NULL, NULL ); } if( !strlen( dir ) ) { _splitpath( il->CurrentFile->home, NULL, dir, NULL, NULL ); } else if( dir[0] != '\\' ) { char dir2[_MAX_DIR]; _splitpath( il->CurrentFile->home, NULL, dir2, NULL, NULL ); strcat( dir2, dir ); strcpy( dir, dir2 ); } _makepath( path, drive, dir, fname, ext ); if( SameFile( path, currfn ) ) { break; } } } if( il != NULL ) { BringUpFile( il, true ); } else { /* * file not edited, go get it */ rc = NewFile( currfn, false ); if( rc != ERR_NO_ERR && rc != NEW_FILE ) { RestoreInfo( ci ); DCDisplayAllLines(); break; } if( !dammit ) { InactiveWindow( wn ); } if( EditFlags.BreakPressed ) { break; } } if( cnt > 0 ) { currfn = list[index]; index++; } } if( ocnt > 0 ) { MemFreeList( ocnt, list ); } if( EditFlags.BreakPressed ) { ClearBreak(); break; } } while( NextWord1( name, fn ) > 0 ); if( altname ) { MemFree( altname ); } MemFree( fn ); #ifdef __WIN__ ToggleHourglass( false ); #endif EditFlags.WatchForBreak = false; if( reset_dir ) { SetCWD( cdir ); } return( rc ); } /* EditFile */
/* * SaveFile - save data from current file */ vi_rc SaveFile( char *name, linenum start, linenum end, int dammit ) { int i; bool existflag = FALSE, restpath = FALSE, makerw = FALSE; char *fn; fcb *cfcb, *sfcb, *efcb; linenum s, e, lc; long bc = 0; status_type lastst; vi_rc rc; int write_crlf; if( CurrentFile == NULL ) { return( ERR_NO_FILE ); } /* * get file name */ if( name == NULL ) { if( CurrentFile->viewonly ) { return( ERR_FILE_VIEW_ONLY ); } if( CFileReadOnly() ) { rc = readOnlyCheck(); if( rc != ERR_NO_ERR ) { return( ERR_READ_ONLY_FILE ); } makerw = TRUE; } fn = CurrentFile->name; restpath = TRUE; } else { existflag = TRUE; fn = name; } if( fn[0] == 0 ) { return( ERR_NO_FILE_NAME ); } if( SameFile( fn, CurrentFile->name ) ) { if( CurrentFile->viewonly ) { return( ERR_FILE_VIEW_ONLY ); } } if( dammit ) { existflag = FALSE; } /* * get range and fcbs */ if( start == -1L && end == -1L ) { s = 1L; rc = CFindLastLine( &e ); if( rc != ERR_NO_ERR ) { return( rc ); } } else { s = start; e = end; } lc = e - s + 1; rc = FindFcbWithLine( s, CurrentFile, &sfcb ); if( rc != ERR_NO_ERR ) { return( rc ); } rc = FindFcbWithLine( e, CurrentFile, &efcb ); if( rc != ERR_NO_ERR ) { return( rc ); } if( restpath ) { rc = ChangeDirectory( CurrentFile->home ); if( rc != ERR_NO_ERR ) { return( rc ); } } if( !CurrentFile->is_stdio ) { if( makerw ) { chmod( fn, PMODE_RW ); } rc = FileOpen( fn, existflag, O_TRUNC | O_WRONLY | O_BINARY | O_CREAT, WRITEATTRS, &fileHandle); if( rc != ERR_NO_ERR ) { Message1( strerror( errno ) ); return( rc ); } } else { fileHandle = 0; #ifdef __WATCOMC__ setmode( fileno( stdout ), O_BINARY ); #endif } /* * start writing fcbs */ #ifdef __WIN__ ToggleHourglass( TRUE ); #endif if( EditFlags.CRLFAutoDetect ) { write_crlf = CurrentFile->write_crlf; } else { write_crlf = EditFlags.WriteCRLF; } lastst = UpdateCurrentStatus( CSTATUS_WRITING ); for( cfcb = sfcb; cfcb != efcb; cfcb = cfcb->next ) { rc = writeRange( s, cfcb->end_line, cfcb, &bc, write_crlf, TRUE ); if( rc != ERR_NO_ERR ) { #ifdef __WIN__ ToggleHourglass( FALSE ); #endif UpdateCurrentStatus( lastst ); return( rc ); } s = cfcb->end_line + 1; } /* * last bit */ rc = writeRange( s, e, efcb, &bc, write_crlf, EditFlags.LastEOL ); #ifdef __WIN__ ToggleHourglass( FALSE ); #endif UpdateCurrentStatus( lastst ); if( rc != ERR_NO_ERR ) { return( rc ); } if( !CurrentFile->is_stdio ) { i = close( fileHandle ); if( makerw ) { chmod( fn, PMODE_R ); } if( i == -1 ) { Message1( strerror( errno ) ); return( ERR_FILE_CLOSE ); } } if( restpath ) { rc = ChangeDirectory( CurrentDirectory ); if( rc != ERR_NO_ERR ) { return( rc ); } } FileIOMessage( fn, lc, bc ); return( ERR_NO_ERR ); } /* SaveFile */
/* * EditFile - read a file into text */ vi_rc EditFile( const char *name, bool dammit ) { char *fn, **list, *currfn; int i, cnt, ocnt; int j, len; window_id wid = NO_WINDOW; char cdir[FILENAME_MAX]; info *ci, *il; bool usedir = false; char mask[FILENAME_MAX]; bool reset_dir; int index; #ifdef __WIN__ char *altname = NULL; #endif vi_rc rc; fn = MemAlloc( FILENAME_MAX ); /* * get file name */ strcpy( cdir, CurrentDirectory ); reset_dir = false; name = SkipLeadingSpaces( name ); if( name[0] == '$' ) { ++name; usedir = true; } fn[0] = '\0'; // if( NextWord1FN( name, fn ) <= 0 ) if( GetStringWithPossibleQuote2( &name, fn, false ) != ERR_NO_ERR ) { usedir = true; mask[0] = '*'; mask[1] = '\0'; } if( usedir ) { if( EditFlags.ExMode ) { MemFree( fn ); return( ERR_INVALID_IN_EX_MODE ); } len = strlen( fn ); if( len > 0 ) { strcpy( mask, fn ); cnt = 0; for( i = len; i-- > 0; ) { if( fn[i] == FILE_SEP ) { for( j = i + 1; j <= len; j++ ) { mask[j - (i + 1)] = fn[j]; } cnt = i; break; } } fn[cnt] = '\0'; } if( fn[0] != '\0' ) { rc = SelectFileOpen( fn, &fn, mask, true ); } else { #ifdef __WIN__ if( name[0] == '\0' ) { altname = MemAlloc( 1000 ); rc = SelectFileOpen( CurrentDirectory, &altname, mask, true ); name = GetNextFileName( altname, fn ); // if multiple, kill path if( isMultipleFiles( name ) ) { name = GetNextFileName( name, fn ); // get 1st name } } else { rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); } #else rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); #endif } if( rc != ERR_NO_ERR || fn[0] == '\0' ) { MemFree( fn ); SetCWD( cdir ); return( rc ); } } /* * loop through all files */ rc = ERR_NO_ERR; EditFlags.WatchForBreak = true; #ifdef __WIN__ ToggleHourglass( true ); #endif do { if( IsDirectory( fn ) ) { if( EditFlags.ExMode ) { rc = ERR_INVALID_IN_EX_MODE; reset_dir = true; break; } rc = SelectFileOpen( fn, &fn, "*", false ); if( rc != ERR_NO_ERR ) { reset_dir = true; break; } if( fn[0] == '\0' ) { reset_dir = true; rc = ERR_NO_ERR; break; } } currfn = fn; ocnt = cnt = ExpandFileNames( currfn, &list ); if( !cnt ) { cnt = 1; } else { currfn = list[0]; } /* * loop through all expanded files */ index = 1; while( cnt > 0 ) { cnt--; /* * quit current file if ! specified, else just save current state */ if( dammit ) { ci = InfoHead; if( CurrentInfo == ci ) { ci = ci->next; } RemoveFromAutoSaveList(); #ifdef __WIN__ CloseAChildWindow( current_window_id ); #else CloseAWindow( current_window_id ); #endif FreeUndoStacks(); FreeMarkList(); FreeEntireFile( CurrentFile ); MemFree( DeleteLLItem( (ss **)&InfoHead, (ss **)&InfoTail, (ss *)CurrentInfo ) ); CurrentInfo = NULL; current_window_id = NO_WINDOW; } else { ci = CurrentInfo; SaveCurrentInfo(); wid = current_window_id; } /* * see if new file is already being edited */ SaveCurrentInfo(); for( il = InfoHead; il != NULL; il = il->next ) { if( SameFile( il->CurrentFile->name, currfn ) ) { break; } if( strcmp( CurrentDirectory, il->CurrentFile->home ) ) { /* directory has changed -- check with full path * note that this will fail if an absolute path * was specified thus we do the regular check first */ char path[FILENAME_MAX]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; size_t path_len; _splitpath( il->CurrentFile->name, drive, dir, fname, ext ); if( drive[0] == '\0' ) { _splitpath( il->CurrentFile->home, drive, NULL, NULL, NULL ); } strcpy( path, il->CurrentFile->home ); path_len = strlen( path ); if( path_len-- > 0 ) { #ifdef __UNIX__ if( path[path_len] != FILE_SEP ) { #else if( path[path_len] != DRV_SEP && path[path_len] != FILE_SEP ) { #endif strcat( path, FILE_SEP_STR ); } } if( dir[0] == '\0' ) { _splitpath( path, NULL, dir, NULL, NULL ); } else if( dir[0] != FILE_SEP ) { char dir2[_MAX_DIR]; _splitpath( path, NULL, dir2, NULL, NULL ); strcat( dir2, dir ); strcpy( dir, dir2 ); } _makepath( path, drive, dir, fname, ext ); if( SameFile( path, currfn ) ) { break; } } } if( il != NULL ) { BringUpFile( il, true ); } else { /* * file not edited, go get it */ rc = NewFile( currfn, false ); if( rc != ERR_NO_ERR && rc != NEW_FILE ) { RestoreInfo( ci ); DCDisplayAllLines(); break; } if( !dammit ) { InactiveWindow( wid ); } if( EditFlags.BreakPressed ) { break; } } if( cnt > 0 ) { currfn = list[index]; index++; } } if( ocnt > 0 ) { MemFreeList( ocnt, list ); } if( EditFlags.BreakPressed ) { ClearBreak(); break; } name = GetNextFileName( name, fn ); } while( *fn != '\0' ); #ifdef __WIN__ if( altname != NULL ) { MemFree( altname ); } #endif MemFree( fn ); #ifdef __WIN__ ToggleHourglass( false ); #endif EditFlags.WatchForBreak = false; if( reset_dir ) { SetCWD( cdir ); } return( rc ); } /* EditFile */ #ifndef __WIN__ static const char *fileOpts[] = { (const char *)"<F1> Go To", (const char *)"<F2> Quit", (const char *)"<F3> Save & Quit" }; static const vi_key fileopts_evlist[] = { VI_KEY( F1 ), VI_KEY( F2 ), VI_KEY( F3 ), VI_KEY( DUMMY ) }; /* * EditFileFromList - edit from file in current active list */ vi_rc EditFileFromList( void ) { int i, tmp, j, n = 0, fcnt; window_id wid; bool repeat = true; info *cinfo; char **list, modchar; bool show_lineno; window_info wi; selectitem si; vi_rc rc; /* * set up options for file list */ memcpy( &wi, &extraw_info, sizeof( window_info ) ); wi.area.x1 = 2; wi.area.x2 = 19; rc = DisplayExtraInfo( &wi, &wid, fileOpts, sizeof( fileOpts ) / sizeof( fileOpts[0] ) ); if( rc != ERR_NO_ERR ) { return( rc ); } while( repeat > 0 ) { /* * set up for this pass */ repeat = false; MoveWindowToFrontDammit( wid, false ); SaveCurrentInfo(); /* * allocate a buffer for strings, add strings */ list = (char **) MemAlloc( GimmeFileCount() * sizeof( char * ) ); for( j = 0, cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next, ++j ) { list[j] = MemAlloc( strlen( cinfo->CurrentFile->name ) + 3 ); if( cinfo->CurrentFile->modified ) { modchar = '*'; } else { modchar = ' '; } MySprintf( list[j], "%c %s", modchar, cinfo->CurrentFile->name ); } fcnt = j; tmp = filelistw_info.area.y2; i = filelistw_info.area.y2 - filelistw_info.area.y1 + 1; if( filelistw_info.has_border ) { i -= 2; } if( j < i ) { filelistw_info.area.y2 -= ( i - j ); } show_lineno = true; /* * get file */ if( n + 1 > j ) { n = j - 1; } memset( &si, 0, sizeof( si ) ); si.wi = &filelistw_info; si.title = "Current Files"; si.list = list; si.maxlist = j; si.num = n; si.retevents = fileopts_evlist; si.event = VI_KEY( DUMMY ); si.show_lineno = show_lineno; si.cln = n + 1; si.eiw = wid; rc = SelectItem( &si ); n = si.num; if( rc == ERR_NO_ERR ) { if( n >= 0 ) { cinfo = InfoHead; for( j = 0; j < n; ++j ) { cinfo = cinfo->next; } BringUpFile( cinfo, true ); switch( si.event ) { case VI_KEY( DUMMY ): case VI_KEY( F1 ): break; case VI_KEY( F2 ): rc = NextFile(); if( rc <= ERR_NO_ERR ) { repeat = true; } break; case VI_KEY( F3 ): rc = SaveAndExit( NULL ); if( rc <= ERR_NO_ERR ) { repeat = true; } break; } } } filelistw_info.area.y2 = tmp; MemFreeList( fcnt, list ); } /* * get rid of option stuff */ CloseAWindow( wid ); return( rc ); } /* EditFileFromList */