home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 208_01 / e5.c < prev    next >
Text File  |  1987-10-13  |  4KB  |  190 lines

  1. /*
  2. HEADER:        CUG208;
  3. TITLE:        'e' for CP/M68K
  4. VERSION:    1.48b
  5.  
  6. DESCRIPTION:    "a screen editor";
  7.  
  8. KEYWORDS:    editor;
  9. SYSTEM:        CP/M68K, V1.2;
  10. FILENAME:    e/e5.c
  11. WARNINGS:    "the default value is for systems with 128K bytes
  12.          of memory or more";
  13. SEE-ALSO:    cpm68k.c, e68k.doc, CUG VOL 133;
  14. AUTHORS:    G.N.Gilbert('e'), J.W.Haefner(for DeSmet C on MSDOS and UNIX)
  15. CODER:        Yoshimasa Tsuji
  16. COMPILERS:    DRI C(Alcyon C) for CP/M68K;
  17. */
  18. /*
  19.     FUNCTIONS: readfile, writefile, exists, scans, retag
  20.     PURPOSE: read and write files
  21. */
  22.  
  23. #include "e.h"
  24.  
  25. FILE *fp;
  26.  
  27. readfile(nname)    /* read file 'nname' and insert it after current line*/
  28. char *nname;
  29. {
  30.     register int c, i;
  31.     register int line;
  32.  
  33.     if ((fp= fopen(nname,"r")) == 0) {
  34.         error("Can't find file");
  35.         nname[0]='\0';
  36.         return(FAIL);
  37.     }
  38.     puttext();
  39.     line=cline;
  40.     do {
  41.         for (i=0; i < LLIM && (c= fgetc(fp)) != EOF && c != '\n'; )
  42.             text[i++]=c;
  43.         text[i]='\0';
  44.         if (line%20 == 0) ptlineno(line);
  45.     } while (inject(line++,text) != FAIL && c != EOF);
  46.     fclose(fp);
  47.     strcpy(text,getline(cline));
  48.     return(0);
  49. }
  50.  
  51. writefile(ffrom,tto,nname,nametoprint,exiting)
  52. int ffrom, tto;
  53. int exiting;
  54. char *nname, *nametoprint;
  55. {
  56.     register int l;
  57.     long copybytestart;
  58.     bool copying;
  59.     register char *t;
  60.     register int c, d;
  61.     extern long ftell();
  62.  
  63.  
  64.     puttext();
  65.     if (nname[0] <= ' ') {
  66.         error("Bad name");
  67.         return(FAIL);
  68.     }
  69.     if ((fp= fopen(nname,"w")) == 0) {
  70.         error("cannot write file");
  71.         return(FAIL);
  72.     }
  73.     putmess("Saving ");
  74.     putstr(nametoprint);
  75.     copying=NO;
  76.     if (exiting) tto=lastread;
  77.     else if (tto == lastl) tto=loc(lastl,0);
  78.     for (l=ffrom; l <= tto; ) {
  79.         t=getline(l++);
  80.         /* if(fputs(getline(l++),fp)== EOF) goto diskfull;
  81.          * is not feasible because it could return EOF
  82.          * when the line is empty. -- a bug in V7 C.
  83.          */
  84.         while (*t) if (fputc(*t++,fp) == EOF) goto diskfull;
  85.         if (fputc('\n',fp) == EOF) goto diskfull;
  86.     }
  87.     if (exiting && (lastl == UNKNOWN|| !goteof)) {
  88.         copying=YES;
  89.         copybytestart= ftell(textfp);
  90.         d = '\n';
  91.         while ( (c= fgetc(textfp)) != EOF)
  92.             if ((d = fputc(c,fp)) == EOF) goto diskfull;
  93.         if (d != '\n')
  94.             if (fputc('\n',fp) == EOF) goto diskfull;
  95.     }
  96.     if (fclose(fp) == EOF) { /*does flushing as well*/
  97.         error("Can't close file");
  98.         goto reposition;
  99.     }
  100.     if (copying) fclose(textfp);
  101.     return(YES);
  102. diskfull:
  103.     error("Disk full");
  104. reposition:
  105.     if (copying) {
  106.         fclose(fp);
  107.         unlink(nname);
  108.         fseek(textfp,copybytestart,0);
  109.     }
  110.     return(FAIL);
  111. }
  112.  
  113. exists(nname)
  114. char *nname;
  115. {
  116.     char c;
  117.  
  118.     c='y';
  119.     if (checkexists(nname)) {
  120.         putmess("OK to replace ");
  121.         putstr(nname);
  122.         putstr(" ? ");
  123.         putch((c=getlow()));
  124.         putret();
  125.     }
  126.     return(c == 'y');
  127. }
  128.  
  129. checkexists(nname)    /*return YES if file 'nname' exists, else NO */
  130. char *nname;
  131. {
  132.     return ((access(nname,4)== 0)? YES : NO);
  133. }
  134.  
  135. scans(answer,maxlen)
  136. char *answer;
  137. int maxlen;
  138. {
  139.     int c, n;
  140.  
  141.     maxlen--;
  142.     n=0;
  143.     while (n < maxlen) {
  144.         switch((c=getscankey())) {
  145.         case LEFTKEY    :
  146.         case DELLEFT    :
  147.             if (n) {
  148.                 putch(BACKSP);
  149.                 putch(' ');
  150.                 putch(BACKSP);
  151.                 n--;
  152.                 answer--;
  153.             }
  154.             break;
  155.         case CR        :
  156.         case ESCKEY    :
  157.             n=maxlen;
  158.             break;
  159.         case RETRIEVE    :
  160.             if (n == 0) {
  161.  
  162.                 while (*answer) {
  163.                     dispch(*answer++);
  164.                     n++;
  165.                 }
  166.                 break;
  167.             }
  168.         default        :
  169.             dispch(c);
  170.             *answer++ =c;
  171.             n++;
  172.             break;
  173.         }
  174.     }
  175.     *answer='\0';
  176.     putret();
  177.     return (c & ~PARBIT) ;
  178. }
  179.  
  180. retag(nname,tag)    /*puts a new suffix on a file name*/
  181. register char *nname, *tag;
  182. {
  183.     for (; *nname && *nname != '.'; nname++);
  184.     if (!*nname)*nname='.';
  185.     for (++nname; (*nname= *tag); nname++, tag++);
  186. }
  187.  
  188.  
  189.  
  190.