home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-4 / APPEND.C < prev    next >
Text File  |  2000-06-30  |  2KB  |  78 lines

  1. /*
  2.     APPEND.C
  3.     Written by Leor Zolman, BD Software, 9/82
  4.  
  5.     Usage:
  6.         A>append  <new_text>  <dest_file> 
  7.  
  8.     Appends the given new text onto the existing dest file,
  9.     using CP/M 2.2x random-record I/O to keep from having to
  10.     read through the entire destination file.
  11.  
  12.     The new-text file is loaded into memory before appending,
  13.     so it has to be short enough to fit into memory.
  14.  
  15.     If the dest-file doesn't exist, it is created anew.
  16. */
  17.  
  18. #include "bdscio.h"
  19.  
  20. char *tbuffer;
  21.  
  22. main(argc, argv)
  23. char **argv;
  24. {
  25.     int lastsec, esize, i, j;
  26.     int fdcurr, fdentry;
  27.     char *eptr;
  28.     unsigned tbufsize;
  29.  
  30.     tbuffer = endext();
  31.     tbufsize = topofmem() - endext() - 500;
  32.  
  33.     if (argc != 3)
  34.         exit(puts("Usage: append <new_text> <dest_file>\n"));
  35.  
  36.  
  37.     if ((fdcurr = open(argv[2],2)) == ERROR) {
  38.         printf("Creating a new current workfile: %s...\n",argv[2]);
  39.         fdcurr = creat(argv[2]);
  40.         tbuffer[0] = CPMEOF;
  41.         lastsec = 0;
  42.      } else {
  43.         lastsec = rcfsiz(fdcurr) - 1;
  44.         seek (fdcurr, lastsec, 0);
  45.         if (read(fdcurr, tbuffer, 1) < 0) {
  46.             printf("Can't read %s\n",argv[2]);
  47.             exit();
  48.          }
  49.      }
  50.  
  51.     for (i = 0; tbuffer[i] != CPMEOF; i++);    /* find EOF */
  52.  
  53.     if ((fdentry = open(argv[1],0)) == ERROR) {
  54.         printf("Can't open %s to append\n",argv[1]);
  55.         fabort(fdcurr);
  56.         exit();
  57.      }
  58.  
  59.     esize = read(fdentry, tbuffer + i, (tbufsize - SECSIZ)) * SECSIZ;
  60.     close(fdentry);
  61.  
  62.     for (eptr = tbuffer + i + esize - SECSIZ; *eptr != CPMEOF; eptr++)
  63.         if (eptr == tbuffer + i + esize) {
  64.             *eptr++ = CPMEOF;
  65.             break;
  66.          }
  67.  
  68.     seek(fdcurr, lastsec, 0);
  69.     if (write(fdcurr, tbuffer, (j = (eptr - tbuffer)/128 + 1)) != j) {
  70.         printf("Write error; disk probably full\n");
  71.         fabort(fdcurr);
  72.         exit();
  73.      }
  74.  
  75.     close(fdcurr);
  76.     printf("%s successfully updated.\n",argv[2]);
  77. }
  78.