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

📄 ovfile.c

📁 汇编语言编的关于ov143b.asm的小程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  068  9-Jun-87  ovfile.c

        Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
*/

#include <stdio.h>
#include <dos.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ov.h"
#include "overr.h"
#include "direct.h"
#include "dosfile.h"
#include "strmem.h"

static int stowidx;
static FILE_ENT *stowfp;
static DRIVE_ENT *drvlst = NULL;
static char *notitself = "You can't copy a file to itself!";
static char *mustbedir = "Enter a drive and/or directory name!";

extern WINDOW cw;
extern int diridx;
extern char **dirlst;
extern FILE_ENT files[];
extern char *cantopen, *none_tagged;

DRIVE_ENT *findrive(int);
struct search_block *nxtfile();
char far *largest_f(unsigned int, unsigned int *);
unsigned int readbuf(int,char far *,unsigned int);
unsigned int writebuf(int,char far *,unsigned int);
char *strupr(), *strchr(), *dirplus(FILE_ENT *, char *);
char *parsepath(char *, char *, char *, int, char **, char **);
int free_f(char far *), stowfile(struct search_block *, char *);
int add2windows(char *, char *, FILE_ENT *), delfromwins(char *, char *);


/******************************************************************************
 **                        G E T F I L E S                                   **
 *****************************************************************************/

getfiles() {           /* get data for the files in the current dir */

   int firsttime;
   char pathbuf[MAX_PATHLEN+6];
   register struct search_block *sbp;

   cw.files_size = 0;                  /* no files yet */
   firsttime = TRUE;                   /* tell nxtfile() its the first call */

   stowidx = 0;                        /* start storing at the begining */
   stowfp = files;

   /* if showall mode is active, call scantree to get all files, otherwise
      scan the current dir ourselves */

   if (cw.showall) {   /* scan the current disk? */

      *pathbuf = '\0';
      strncat(pathbuf,cw.dirbuf,2);
      scantree("\\",pathbuf,0x16,stowfile);    /* scan entire disk */

   } else        /* scan the current directory */

      while (stowidx < MAX_FILES && (sbp = nxtfile("*.*",0x16,&firsttime)))
            stowfile(sbp,NULL);

   /* sort the file names if there are any, also set nfiles */

   if (cw.nfiles = stowidx)
      sort_files(NULL);

   /* set current file pointer to the first file */

   cw.curidx = 0;

   /* set file counters */

   cw.num_files = cw.nfiles;           /* files in the dir */
   cw.num_tagged = 0;                  /* no files tagged yet */
   cw.tag_size = 0;

}


/*****************************************************************************
                             S T O W F I L E
 *****************************************************************************/

static int
stowfile(sbp,dirp)     /* store a file in files[] */
register struct search_block *sbp;
char *dirp;
{
   register int len;
   static char *lastdir = NULL;
   register FILE_ENT *fp = stowfp;     /* fast stow pointer */

   /* if dirp is not NULL, this call is defining which dir is being scanned,
      add this dir name to the showall dir list - return NZ (keep going) if
      dir name added okay, 0 (stop scanning) if can't add dir name - don't
      incr diridx unless everything is okay.  The dir name passed will always
      have a trailing \ which we don't really want - don't keep it unless
      this happens to be the root dir (len == 3) */

   if (dirp) {
      if (diridx < MAX_DIR) {
         len = strlen(dirp);
         dirlst[diridx++] = lastdir = Strndup(dirp,len > 3 ? len-1 : len);
         return(1);
      }
      return(0);
   }

   /* ignore . and .. entries.  If the file mask is defined, make
      sure the file name matches.  Make sure the file attributes
      match the selection attributes */

   if (stowidx >= MAX_FILES || *sbp->fn == '.' ||
       (cw.selatrs & sbp->attrib) != sbp->attrib ||
       (*cw.mask && (cw.maskcmp != match_name(sbp->fn,cw.mask))))

      return(1);       /* don't want this one, but keep looking */

   /* we want the file, fill in files[] entry */

   fp->size = sbp->size;
   fp->flags = sbp->attrib & 0x3f;
   fp->index = stowidx++;
   fp->date = sbp->date;
   fp->time = sbp->time;
   strcpy(fp->name,sbp->fn);
   fp->dirp = cw.showall ? lastdir : cw.dirbuf;   /* point to files dir str */
   cw.files_size += fp->size;                     /* accumulate total size */

   stowfp++;                             /* update for next time */
   return(1);                            /* keep looking */
}


/******************************************************************************
 **                             I N F O                                      **
 *****************************************************************************/

info() {               /* toggle the extended info display */

   cw.info_display ^= 1;               /* toggle info on/off */

   infocnt(cw.info_display ? 1 : -1);  /* one more or less info window */

   adjust_window();                    /* resize the window data */

   update_window(1);                   /* display files in new format */
}


/******************************************************************************
 **                      C O P Y _ C U R R E N T                             **
 *****************************************************************************/

copy_current() {       /* copy the current file */

   FILE_ENT tmpfent;
   register FILE_ENT *fp;
   register DRIVE_ENT *dp;
   char *dest, *target, *todir, *tofn;

   dest = prompt("Copy current file","Copy to where? ",NULL,0,MAX_REPLY);
   if (strlen(dest) == 0)
      return;

   fp = &files[cw.curidx];
   target = parsepath(fp->dirp,fp->name,dest,isadir(fp->dirp,dest),&todir,&tofn);

   if (strcmp(fp->dirp,todir) != 0 || strcmp(fp->name,tofn) != 0) {
      if (copyfile(fp,target)) {
         tmpfent = *fp;                     /* new file will have */
         tmpfent.flags |= ARCHIVE;          /* ARCHIVE attrib on  */
         add2windows(todir,tofn,&tmpfent);
         dp = findrive(*todir);
         getvolsiz(dp->drive,&dp->vol_size,&dp->vol_free,&dp->clustersiz);
      }
   } else           /* attempt to copy file to itself! */

      show_error(0,0,1,notitself);

   free(target);
   free(todir);
   free(tofn);
}


/******************************************************************************
 **                        C O P Y _ T A G G E D                             **
 *****************************************************************************/

copy_tagged() {        /* mass copy of tagged files to somewhere */

   int i, ch;
   FILE_ENT tmpfent;
   register FILE_ENT *fp;
   register DRIVE_ENT *dp;
   char *dir, *target, *todir, *tofn;

   if (cw.num_tagged == 0)             /* are there any tagged files? */
      show_error(0,NONE_TAGGED,1,none_tagged);

   dir = prompt("Copy tagged files","Copy to which dir? ",NULL,0,MAX_REPLY);
   if (strlen(dir) == 0)
      return;

   if (!isadir(cw.dirbuf,dir))         /* user must give a directory name */
      show_error(0,MUST_BE_DIR,1,mustbedir);

   /* Okay, copy the tagged files */

   for (fp = files, i = 0; i < cw.nfiles && !brkout(); i++, fp++)

      if (fp->flags & TAGGED) {
         disp_msg(2,"Copying ",fp->name);

         target = parsepath(fp->dirp,fp->name,dir,1,&todir,&tofn);

         if (strcmp(fp->dirp,todir) != 0 || strcmp(fp->name,tofn) != 0) {

            if (copyfile(fp,target)) {
               tmpfent = *fp;                     /* new file will have */
               tmpfent.flags |= ARCHIVE;          /* ARCHIVE attrib on  */
               add2windows(todir,tofn,&tmpfent);
            }

         } else        /* trying to copy a file to itself */

            show_error(0,0,1,notitself);

         free(target);                 /* release temp strings */
         free(todir);
         free(tofn);
      }

   clr_msg();                          /* clear last copying msg */

   /* update volume stats after copy */

   dp = strlen(dir) > 1 && dir[1] == ':' ? findrive(*dir) : cw.drivep;
   getvolsiz(dp->drive,&dp->vol_size,&dp->vol_free,&dp->clustersiz);

}


/******************************************************************************
 **                         C O P Y F I L E                                  **
 *****************************************************************************/

static int
copyfile(fp,to)        /* copy from to */
register FILE_ENT *fp;
char *to;
{
   char *fn;
   char far *buffer;
   int fd, td, rc = 0;
   unsigned int bufsiz, len;
   static char *copyabort = " -- copy aborted";

   /* allocate space for the in memory copy buffer */

   buffer = largest_f(63*1024,&bufsiz);  /* get largest possible blk up 2 63k */
   if (bufsiz < 1024) {                  /* give it up if < 1k available */
      if (bufsiz)                        /* release the tiny buffer */
         free_f(buffer);
      show_error(0,0,2,"No free memory to allocate copy buffer",copyabort);
      goto error1;
   }

   /* open the from file */

   fd = open(fn = fname(fp),O_RDONLY|O_BINARY);
   free(fn);

   if (fd == -1) {
      show_error(SHOW_DOS,0,3,cantopen,fp->name,": ");
      goto error2;
   }

   /* open the output file */

   if ((td = open(to,O_CREAT|O_BINARY|O_TRUNC|O_RDWR,S_IWRITE)) == -1) {
      show_error(SHOW_DOS,0,3,cantopen,to,": ");
      goto error3;
   }

   /* copy the file, a buffer at a time */

   while (len = readbuf(fd,buffer,bufsiz))

     if (writebuf(td,buffer,len) < len) {
         show_error(0,0,3,"Error writting to ",to,": Disk may be full");
         goto error4;
      }

   setftime(td,fp->date,fp->time);     /* to file time = from file */

   rc = 1;                             /* the copy seems to have worked */

   error4: close(td);                  /* close the to file */

   if (!rc)                            /* erase to file if there was an */
      unlink(to);                      /*   error while trying to copy  */

   error3: close(fd);                  /* close the from file */
   error2: free_f(buffer);             /* release copy buffer */

   /* set new file attrib's = old file + ACRHIVE */

   if (rc)
      setattrib(to,(fp->flags & (ARCHIVE|SYSTEM|HIDDEN|RDONLY)) | ARCHIVE);

   error1: return(rc);                 /* tell caller if we worked */
}


/******************************************************************************
 **                    E R A S E _ C U R R E N T                             **
 *****************************************************************************/

erase_current() {      /* erase the current file */

   int ch;
   char askmsg[30];
   register FILE_ENT *fp;

   fp = &files[cw.curidx];

   strcpy(askmsg,"Erase ");
   strcat(askmsg,fp->name);
   strcat(askmsg," ? (y/N): ");
   ch = ask(askmsg);

   if (yes(ch))
      if (delfile(fp)) {                       /* delete the file */
         delfromwins(fp->dirp,fp->name);       /* remove from windows */
         getvolsiz(*cw.dirbuf,&cw.drivep->vol_size, /* vol stats have changed */
            &cw.drivep->vol_free,&cw.drivep->clustersiz);
      }
}


/*****************************************************************************
                          E R A S E _ T A G G E D
 *****************************************************************************/

erase_tagged() {       /* erase the tagged files */

   int ch;
   register int i;
   register FILE_ENT *fp;

   if (cw.num_tagged == 0)             /* are there any tagged files? */
      show_error(0,NONE_TAGGED,1,none_tagged);

   ch = ask("Erase all tagged files? (y/N): ");
   if (!yes(ch))
      return;

   /* scan files[] looking for tagged files, when found, delete it */

   for (i = 0, fp = files; i < cw.nfiles && !brkout(); i++, fp++)
      if (fp->flags & TAGGED) {
         disp_msg(2,"Erasing ",fp->name);
         if (delfile(fp))
            delfromwins(fp->dirp,fp->name);
      }

   clr_msg();                                  /* clear last erasing msg */

   getvolsiz(*cw.dirbuf,&cw.drivep->vol_size,  /* volume stats have changed */
      &cw.drivep->vol_free,&cw.drivep->clustersiz);
}


/*****************************************************************************
                               D E L F I L E
 *****************************************************************************/

static int
delfile(fp)            /* delete a file */
register FILE_ENT *fp;

⌨️ 快捷键说明

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