⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.c

📁 功能强大的文本编辑器
💻 C
📖 第 1 页 / 共 5 页
字号:
   return( rc );
}


/*
 * Name:    save_backup
 * Purpose: To save a backup copy of the current file to disk.
 * Date:    June 5, 1991
 * Passed:  window:  pointer to current window
 */
int  save_backup( WINDOW *window )
{
   /*
    * set up file name
    */
   return( write_to_disk( window, window->file_info->backup_fname ) );
}


/*
 * Name:    write_to_disk
 * Purpose: To write file from memory to disk
 * Date:    June 5, 1991
 * Passed:  window:  pointer to current window
 *          fname:   file name to save on disk
 */
int  write_to_disk( WINDOW *window, char *fname )
{
char name[MAX_COLS]; /* name of file to be saved */
char status_line[MAX_COLS+2]; /* status line at top of window */
char line_buff[(MAX_COLS+1)*2]; /* buffer for char and attribute  */
register file_infos *file;
int  rc;
int  prompt_line;
int  fattr;

   file = window->file_info;
   prompt_line = window->bottom_line;

   /*
    * set up file name
    */
   assert( strlen( fname ) < MAX_COLS );
   strcpy( name, fname );
   save_screen_line( 0, prompt_line, line_buff );
   eol_clear( 0, prompt_line, g_display.message_color );

   /*
    * saving
    */
   combine_strings( status_line, utils6, name, "'" );
   s_output( status_line, prompt_line, 0, g_display.message_color );
   if ((rc = hw_save( name, file, 1L, file->length, NOTMARKED )) == ERROR) {
      if (ceh.flag != ERROR) {
         if (get_fattr( name, &fattr ) == OK && fattr & READ_ONLY)
            /*
             * file is read only
             */
            combine_strings( status_line, utils7a, name, utils7b );
         else
            /*
             * cannot write to
             */
            combine_strings( status_line, utils8, name, "'" );
         error( WARNING, prompt_line, status_line );
      }
   }
   restore_screen_line( 0, prompt_line, line_buff );
   return( rc );
}


/*
 * Name:    save_as_file
 * Purpose: To save the current file to disk, but under a new name.
 * Date:    June 5, 1991
 * Passed:  window:  pointer to current window
 */
int  save_as_file( WINDOW *window )
{
char name[MAX_COLS];            /* new name for file */
char line_buff[(MAX_COLS+1)*2]; /* buffer for char and attribute  */
int  prompt_line;
int  rc;
int  fattr;
register WINDOW *win;           /* put window pointer in a register */
line_list_ptr temp_ll;

   win = window;
   entab_linebuff( );
   if (un_copy_line( win->ll, win, TRUE ) == ERROR)
      return( ERROR );
   /*
    * read in name
    */
   prompt_line = win->bottom_line;
   save_screen_line( 0, prompt_line, line_buff );
   name[0] = '\0';
   /*
    * new file name:
    */
   if ((rc = get_name( utils9, prompt_line, name,
                       g_display.message_color )) == OK  &&  *name != '\0') {

       /*
        * make sure it is OK to overwrite any existing file
        */
      rc = get_fattr( name, &fattr );
      if (rc == OK) {   /* file exists */
         /*
          * overwrite existing file?
          */
         set_prompt( utils10, prompt_line );
         if (get_yn( ) != A_YES  ||  change_mode( name, prompt_line ) == ERROR)
            rc = ERROR;
      }
      if (rc != ERROR)
         rc = write_to_disk( win, name );

      /*
       * depending on personal taste, you may want to uncomment the next
       *  lines to clear the dirty flags.
       */
/*
 *     if (rc == OK) {
 *        temp_ll = window->file_info->line_list;
 *        for (; temp_ll->len != EOF; temp_ll=temp_ll->next)
 *           temp_ll->dirty = FALSE;
 *        window->file_info->dirty = GLOBAL;
 *     }
 */
   }
   restore_screen_line( 0, prompt_line, line_buff );
   return( rc );
}


/*
 * Name:    change_fattr
 * Purpose: To change the file attributes
 * Date:    December 31, 1991
 * Passed:  window:  pointer to current window
 */
int  change_fattr( WINDOW *window )
{
char name[MAX_COLS];            /* new name for file */
char line_buff[(MAX_COLS+1)*2]; /* buffer for char and attribute  */
file_infos *file;
WINDOW *wp;
int  prompt_line;
register int ok;
unsigned char fattr;
char *s;
int  rc;

   prompt_line = window->bottom_line;
   save_screen_line( 0, prompt_line, line_buff );
   name[0] = '\0';
   /*
    * enter new file attributes
    */
   if ((ok = get_name( utils14, prompt_line, name,
                       g_display.message_color )) == OK) {
      if (*name != '\0') {
         fattr = 0;
         s = name;

         /*
          * yes, I know lint complains about "ok = *s++".
          */
         while (ok = *s++) {
            switch (ok) {
               case 'a' :
               case 'A' :
                  fattr |= ARCHIVE;
                  break;
               case 's' :
               case 'S' :
                  fattr |= SYSTEM;
                  break;
               case 'h' :
               case 'H' :
                  fattr |= HIDDEN;
                  break;
               case 'r' :
               case 'R' :
                  fattr |= READ_ONLY;
                  break;
               default :
                  break;
            }
         }
         file = window->file_info;
         if (set_fattr( file->file_name, fattr ))
            /*
             * new file attributes not set
             */
            error( WARNING, prompt_line, utils15 );
         else {
            file->file_attrib = fattr;
            for (wp=g_status.window_list; wp!=NULL; wp=wp->next) {
               if (wp->file_info == file && wp->visible)
                  show_window_fname( wp );
            }
         }
      }
      rc = OK;
   } else
      rc = ERROR;
   restore_screen_line( 0, prompt_line, line_buff );
   return( rc );
}


/*
 * Name:    get_fattr
 * Purpose: To get dos file attributes
 * Date:    December 26, 1991
 * Passed:  fname: ASCIIZ file name.  Null terminated file name
 *          fattr: pointer to file attributes
 * Returns: 0 if successfull, non zero if not
 * Notes:   Uses the DOS function to get file attributes.  I really didn't
 *           like the file attribute functions in the C library:  fstat() and
 *           stat() or access() and chmod().
 *           FYI, File Attributes:
 *              0x00 = Normal.  Can be read or written w/o restriction
 *              0x01 = Read-only.  Cannot be opened for write; a file with
 *                     the same name cannot be created.
 *              0x02 = Hidden.  Not found by directory search.
 *              0x04 = System.  Not found by directory search.
 *              0x08 = Volumn Label.
 *              0x10 = Directory.
 *              0x20 = Archive.  Set whenever the file is changed, or
 *                     cleared by the Backup command.
 *           Return codes:
 *              0 = No error
 *              1 = AL not 0 or 1
 *              2 = file is invalid or does not exist
 *              3 = path is invalid or does not exist
 *              5 = Access denied
 */
int  get_fattr( char far *fname, int *fattr )
{
int  rc;                /* return code */
int  attr;

   ASSEMBLE {
        push    ds
        mov     dx, WORD PTR fname      /* get OFFSET of filename string */
        mov     ax, WORD PTR fname+2    /* get SEGMENT of filename string */
        mov     ds, ax                  /* put SEGMENT in ds */
        mov     ax, 0x4300              /* function:  get file attributes */
        int     0x21                    /* DOS interrupt */
        pop     ds

        jc      an_error                /* save the error code from get attr */
        xor     ax, ax                  /* if no carry, no error */
        jmp     SHORT get_out           /* lets get out */
   }
an_error:


   ASSEMBLE {
        xor     cx, cx                  /* if error, then zero out cx - attrs */
   }
get_out:

   ASSEMBLE {
        mov     WORD PTR rc, ax         /* ax contains error number on error */
        mov     WORD PTR attr, cx       /* cx contains file attributes */
   }
   *fattr = attr;
   if (ceh.flag == ERROR)
      rc = ERROR;
   return( rc );
}


/*
 * Name:    set_fattr
 * Purpose: To set dos file attributes
 * Date:    December 26, 1991
 * Passed:  fname: ASCIIZ file name.  Null terminated file name
 *          fattr: file attributes
 * Returns: 0 if successfull, non zero if not
 * Notes:   Uses the DOS function to get file attributes.
 *           Return codes:
 *              0 = No error
 *              1 = AL not 0 or 1
 *              2 = file is invalid or does not exist
 *              3 = path is invalid or does not exist
 *              5 = Access denied
 */
int  set_fattr( char far *fname, int fattr )
{
int  rc;                /* return code */

   ASSEMBLE {
        push    ds
        mov     dx, WORD PTR fname      /* get OFFSET of filename string */
        mov     ax, WORD PTR fname+2    /* get SEGMENT of filename string */
        mov     ds, ax                  /* put SEGMENT in ds */
        mov     cx, WORD PTR fattr      /* cx contains file attributes */
        mov     ax, 0x4301              /* function:  get file attributes */
        int     0x21                    /* DOS interrupt */
        pop     ds

        jc      get_out                 /* save the error code from get attr */
        xor     ax, ax                  /* if no carry, no error */
   }
get_out:

   ASSEMBLE {
        mov     WORD PTR rc, ax         /* ax contains error number on error */
   }
   if (ceh.flag == ERROR)
      rc = ERROR;
   return( rc );
}


/*
 * Name:    get_current_directory
 * Purpose: get current directory
 * Date:    February 13, 1992
 * Passed:  path:  pointer to buffer to store path
 *          drive: drive to get current directory
 * Notes:   use simple DOS interrupt
 */
int  get_current_directory( char far *path, int drive )
{
int  rc;

   ASSEMBLE {
        push    si                      /* save register vars if any */
        push    ds                      /* save ds */

        mov     dx, WORD PTR drive      /* dl = drive, 0 = default, 1 = a, etc.. */
        mov     si, WORD PTR path       /* get OFFSET of path */
        mov     ax, WORD PTR path+2     /* get SEGMENT of path */
        mov     ds, ax                  /* put it in ds */
        mov     ah, 0x47                /* function 0x47 == get current dir */
        int     0x21                    /* standard DOS interrupt */
        xor     ax, ax                  /* zero out ax, return OK if no error */
        jnc     no_error                /* if carry set, then an error */
        mov     ax, ERROR               /* return -1 if error */
   }
no_error:

   ASSEMBLE {
        pop     ds                      /* get back ds */
        pop     si                      /* get back si */
        mov     WORD PTR rc, ax         /* save return code */
   }
   if (ceh.flag == ERROR)
      rc = ERROR;
   return( rc );
}


/*
 * Name:    set_current_directory
 * Purpose: set current directory
 * Date:    February 13, 1992
 * Passed:  new_path: directory path, which may include drive letter
 * Notes:   use simple DOS interrupt
 */
int  set_current_directory( char far *new_path )
{
int  rc;

   ASSEMBLE {
        push    ds                      /* save ds */

        mov     dx, WORD PTR new_path   /* get OFFSET of new_path */
        mov     ax, WORD PTR new_path+2 /* get SEGMENT of new_path */
        mov     ds, ax                  /* put it in ds */
        mov     ah, 0x3b                /* function 0x3b == set current dir */
        int     0x21                    /* standard DOS interrupt */
        xor     ax, ax                  /* zero out ax, return OK if no error */
        jnc     no_error                /* if carry set, then an error */
        mov     ax, ERROR               /* return -1 if error */
   }
no_error:

   ASSEMBLE {
        pop     ds                      /* get back ds */
        mov     WORD PTR rc, ax         /* save return code */
   }
   if (ceh.flag == ERROR)
      rc = ERROR;
   return( rc );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -