added depend label
[unix-history] / usr / src / usr.bin / ex / ex_temp.c
index 597d881..e0f6870 100644 (file)
@@ -1,4 +1,13 @@
-/* Copyright (c) 1979 Regents of the University of California */
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char *sccsid = "@(#)ex_temp.c   7.6 (Berkeley) %G%";
+#endif not lint
+
 #include "ex.h"
 #include "ex_temp.h"
 #include "ex_vis.h"
 #include "ex.h"
 #include "ex_temp.h"
 #include "ex_vis.h"
 #define        READ    0
 #define        WRITE   1
 
 #define        READ    0
 #define        WRITE   1
 
+#ifndef vms
+#define        EPOSITION       7
+#else
+#define        EPOSITION       13
+#endif
+
 char   tfname[40];
 char   rfname[40];
 int    havetmp;
 char   tfname[40];
 char   rfname[40];
 int    havetmp;
@@ -26,7 +41,8 @@ fileinit()
        if (tline == INCRMT * (HBLKS+2))
                return;
        cleanup(0);
        if (tline == INCRMT * (HBLKS+2))
                return;
        cleanup(0);
-       close(tfile);
+       if (tfile >= 0)
+               close(tfile);
        tline = INCRMT * (HBLKS+2);
        blocks[0] = HBLKS;
        blocks[1] = HBLKS+1;
        tline = INCRMT * (HBLKS+2);
        blocks[0] = HBLKS;
        blocks[1] = HBLKS+1;
@@ -36,33 +52,59 @@ fileinit()
        iblock2 = -1;
        oblock = -1;
        CP(tfname, svalue(DIRECTORY));
        iblock2 = -1;
        oblock = -1;
        CP(tfname, svalue(DIRECTORY));
-       if (stat(tfname, &stbuf)) {
+#ifndef vms
+       if (stat(tfname, &stbuf))
+#else
+       goto vms_no_check_dir;
+#endif
+       {
 dumbness:
                if (setexit() == 0)
                        filioerr(tfname);
                else
                        putNFL();
                cleanup(1);
 dumbness:
                if (setexit() == 0)
                        filioerr(tfname);
                else
                        putNFL();
                cleanup(1);
-               exit(1);
+               ex_exit(1);
        }
        }
+#ifndef        vms
        if ((stbuf.st_mode & S_IFMT) != S_IFDIR) {
                errno = ENOTDIR;
                goto dumbness;
        }
        if ((stbuf.st_mode & S_IFMT) != S_IFDIR) {
                errno = ENOTDIR;
                goto dumbness;
        }
+#else
+vms_no_check_dir:
+#endif
        ichanged = 0;
        ichang2 = 0;
        ichanged = 0;
        ichang2 = 0;
+#ifndef        vms
        ignore(strcat(tfname, "/ExXXXXX"));
        ignore(strcat(tfname, "/ExXXXXX"));
+#else
+       ignore(strcat(tfname, "ExXXXXX"));
+#endif
        for (p = strend(tfname), i = 5, j = getpid(); i > 0; i--, j /= 10)
                *--p = j % 10 | '0';
        for (p = strend(tfname), i = 5, j = getpid(); i > 0; i--, j /= 10)
                *--p = j % 10 | '0';
+#ifdef vms
+       ignore(strcat(tfname, ".txt.1"));
+       unlink(tfname);
+#endif
        tfile = creat(tfname, 0600);
        if (tfile < 0)
                goto dumbness;
        tfile = creat(tfname, 0600);
        if (tfile < 0)
                goto dumbness;
+#ifdef VMUNIX
+       {
+               extern stilinc;         /* see below */
+               stilinc = 0;
+       }
+#endif
        havetmp = 1;
        havetmp = 1;
-       close(tfile);
+       if (tfile >= 0)
+               close(tfile);
        tfile = open(tfname, 2);
        if (tfile < 0)
                goto dumbness;
        tfile = open(tfname, 2);
        if (tfile < 0)
                goto dumbness;
+#ifdef UNIX_SBRK
 /*     brk((char *)fendcore); */
 /*     brk((char *)fendcore); */
+#endif
 }
 
 cleanup(all)
 }
 
 cleanup(all)
@@ -72,12 +114,16 @@ cleanup(all)
                putpad(TE);
                flush();
        }
                putpad(TE);
                flush();
        }
-       if (havetmp)
+       if (havetmp) {
+               if (tfile >= 0)
+                       close(tfile);
                unlink(tfname);
                unlink(tfname);
+       }
        havetmp = 0;
        if (all && rfile >= 0) {
        havetmp = 0;
        if (all && rfile >= 0) {
+               if (rfile >= 0)
+                       close(rfile);
                unlink(rfname);
                unlink(rfname);
-               close(rfile);
                rfile = -1;
        }
 }
                rfile = -1;
        }
 }
@@ -137,8 +183,10 @@ getblock(atl, iof)
        int iof;
 {
        register int bno, off;
        int iof;
 {
        register int bno, off;
+#ifdef CRYPT
         register char *p1, *p2;
         register int n;
         register char *p1, *p2;
         register int n;
+#endif
        
        bno = (atl >> OFFBTS) & BLKMSK;
        off = (atl << SHFT) & LBTMSK;
        
        bno = (atl >> OFFBTS) & BLKMSK;
        off = (atl << SHFT) & LBTMSK;
@@ -160,62 +208,86 @@ getblock(atl, iof)
        if (iof == READ) {
                if (hitin2 == 0) {
                        if (ichang2) {
        if (iof == READ) {
                if (hitin2 == 0) {
                        if (ichang2) {
-                               if(xtflag)
-                                       crblock(tperm, ibuff2, CRSIZE, (long)0);
                                blkio(iblock2, ibuff2, write);
                        }
                        ichang2 = 0;
                        iblock2 = bno;
                        blkio(bno, ibuff2, read);
                                blkio(iblock2, ibuff2, write);
                        }
                        ichang2 = 0;
                        iblock2 = bno;
                        blkio(bno, ibuff2, read);
-                       if(xtflag)
-                               crblock(tperm, ibuff2, CRSIZE, (long)0);
                        hitin2 = 1;
                        return (ibuff2 + off);
                }
                hitin2 = 0;
                if (ichanged) {
                        hitin2 = 1;
                        return (ibuff2 + off);
                }
                hitin2 = 0;
                if (ichanged) {
-                       if(xtflag)
-                               crblock(tperm, ibuff, CRSIZE, (long)0);
                        blkio(iblock, ibuff, write);
                }
                ichanged = 0;
                iblock = bno;
                blkio(bno, ibuff, read);
                        blkio(iblock, ibuff, write);
                }
                ichanged = 0;
                iblock = bno;
                blkio(bno, ibuff, read);
-               if(xtflag)
-                       crblock(tperm, ibuff, CRSIZE, (long)0);
                return (ibuff + off);
        }
        if (oblock >= 0) {
                return (ibuff + off);
        }
        if (oblock >= 0) {
-               if(xtflag) {
-                       /*
-                        * Encrypt block before writing, so some devious
-                        * person can't look at temp file while editing.
-                        */
-                       p1 = obuff;
-                       p2 = crbuf;
-                       n = CRSIZE;
-                       while(n--)
-                               *p2++ = *p1++;
-                       crblock(tperm, crbuf, CRSIZE, (long)0);
-                       blkio(oblock, crbuf, write);
-               } else
                        blkio(oblock, obuff, write);
        }
        oblock = bno;
        return (obuff + off);
 }
 
                        blkio(oblock, obuff, write);
        }
        oblock = bno;
        return (obuff + off);
 }
 
+#ifdef VMUNIX
+#ifdef vms
+#define        INCORB  32
+#else
+#define        INCORB  64
+#endif
+char   incorb[INCORB+1][BUFSIZ];
+#define        pagrnd(a)       ((char *)(((int)a)&~(BUFSIZ-1)))
+int    stilinc;        /* up to here not written yet */
+#endif
+
 blkio(b, buf, iofcn)
        short b;
        char *buf;
        int (*iofcn)();
 {
 
 blkio(b, buf, iofcn)
        short b;
        char *buf;
        int (*iofcn)();
 {
 
+#ifdef VMUNIX
+       if (b < INCORB) {
+               if (iofcn == read) {
+                       bcopy(pagrnd(incorb[b+1]), buf, BUFSIZ);
+                       return;
+               }
+               bcopy(buf, pagrnd(incorb[b+1]), BUFSIZ);
+               if (laste) {
+                       if (b >= stilinc)
+                               stilinc = b + 1;
+                       return;
+               }
+       } else if (stilinc)
+               tflush();
+#endif
        lseek(tfile, (long) (unsigned) b * BUFSIZ, 0);
        if ((*iofcn)(tfile, buf, BUFSIZ) != BUFSIZ)
                filioerr(tfname);
 }
 
        lseek(tfile, (long) (unsigned) b * BUFSIZ, 0);
        if ((*iofcn)(tfile, buf, BUFSIZ) != BUFSIZ)
                filioerr(tfname);
 }
 
+#ifdef VMUNIX
+tlaste()
+{
+
+       if (stilinc)
+               dirtcnt = 0;
+}
+
+tflush()
+{
+       int i = stilinc;
+       
+       stilinc = 0;
+       lseek(tfile, (long) 0, 0);
+       if (write(tfile, pagrnd(incorb[1]), i * BUFSIZ) != (i * BUFSIZ))
+               filioerr(tfname);
+}
+#endif
+
 /*
  * Synchronize the state of the temporary file in case
  * a crash occurs.
 /*
  * Synchronize the state of the temporary file in case
  * a crash occurs.
@@ -226,6 +298,10 @@ synctmp()
        register line *a;
        register short *bp;
 
        register line *a;
        register short *bp;
 
+#ifdef VMUNIX
+       if (stilinc)
+               return;
+#endif
        if (dol == zero)
                return;
        if (ichanged)
        if (dol == zero)
                return;
        if (ichanged)
@@ -264,12 +340,27 @@ oops:
        lseek(tfile, 0l, 0);
        if (write(tfile, (char *) &H, sizeof H) != sizeof H)
                goto oops;
        lseek(tfile, 0l, 0);
        if (write(tfile, (char *) &H, sizeof H) != sizeof H)
                goto oops;
+#ifdef notdef
+       /*
+        * This will insure that exrecover gets as much
+        * back after a crash as is absolutely possible,
+        * but can result in pregnant pauses between commands
+        * when the TSYNC call is made, so...
+        */
+#ifndef vms
+       (void) fsync(tfile);
+#endif
+#endif
 }
 
 TSYNC()
 {
 
 }
 
 TSYNC()
 {
 
-       if (dirtcnt > 12) {
+       if (dirtcnt > MAXDIRT) {        /* mjm: 12 --> MAXDIRT */
+#ifdef VMUNIX
+               if (stilinc)
+                       tflush();
+#endif
                dirtcnt = 0;
                synctmp();
        }
                dirtcnt = 0;
                synctmp();
        }
@@ -302,7 +393,7 @@ struct      rbuf {
        short   rb_prev;
        short   rb_next;
        char    rb_text[BUFSIZ - 2 * sizeof (short)];
        short   rb_prev;
        short   rb_next;
        char    rb_text[BUFSIZ - 2 * sizeof (short)];
-} *rbuf;
+} *rbuf, KILLrbuf, putrbuf, YANKrbuf, regrbuf;
 #ifdef VMUNIX
 short  rused[256];
 #else
 #ifdef VMUNIX
 short  rused[256];
 #else
@@ -320,12 +411,13 @@ regio(b, iofcn)
 
        if (rfile == -1) {
                CP(rfname, tfname);
 
        if (rfile == -1) {
                CP(rfname, tfname);
-               *(strend(rfname) - 7) = 'R';
+               *(strend(rfname) - EPOSITION) = 'R';
                rfile = creat(rfname, 0600);
                if (rfile < 0)
 oops:
                        filioerr(rfname);
                rfile = creat(rfname, 0600);
                if (rfile < 0)
 oops:
                        filioerr(rfname);
-               close(rfile);
+               else
+                       close(rfile);
                rfile = open(rfname, 2);
                if (rfile < 0)
                        goto oops;
                rfile = open(rfname, 2);
                if (rfile < 0)
                        goto oops;
@@ -350,7 +442,7 @@ REGblk()
                                j++, m >>= 1;
                        rused[i] |= (1 << j);
 #ifdef RDEBUG
                                j++, m >>= 1;
                        rused[i] |= (1 << j);
 #ifdef RDEBUG
-                       printf("allocating block %d\n", i * 16 + j);
+                       ex_printf("allocating block %d\n", i * 16 + j);
 #endif
                        return (i * 16 + j);
                }
 #endif
                        return (i * 16 + j);
                }
@@ -374,17 +466,16 @@ int       shread();
 KILLreg(c)
        register int c;
 {
 KILLreg(c)
        register int c;
 {
-       struct rbuf arbuf;
        register struct strreg *sp;
 
        register struct strreg *sp;
 
-       rbuf = &arbuf;
+       rbuf = &KILLrbuf;
        sp = mapreg(c);
        rblock = sp->rg_first;
        sp->rg_first = sp->rg_last = 0;
        sp->rg_flags = sp->rg_nleft = 0;
        while (rblock != 0) {
 #ifdef RDEBUG
        sp = mapreg(c);
        rblock = sp->rg_first;
        sp->rg_first = sp->rg_last = 0;
        sp->rg_flags = sp->rg_nleft = 0;
        while (rblock != 0) {
 #ifdef RDEBUG
-               printf("freeing block %d\n", rblock);
+               ex_printf("freeing block %d\n", rblock);
 #endif
                rused[rblock / 16] &= ~(1 << (rblock % 16));
                regio(rblock, shread);
 #endif
                rused[rblock / 16] &= ~(1 << (rblock % 16));
                regio(rblock, shread);
@@ -407,14 +498,13 @@ int       getREG();
 putreg(c)
        char c;
 {
 putreg(c)
        char c;
 {
-       struct rbuf arbuf;
        register line *odot = dot;
        register line *odol = dol;
        register int cnt;
 
        deletenone();
        appendnone();
        register line *odot = dot;
        register line *odol = dol;
        register int cnt;
 
        deletenone();
        appendnone();
-       rbuf = &arbuf;
+       rbuf = &putrbuf;
        rnleft = 0;
        rblock = 0;
        rnext = mapreg(c)->rg_first;
        rnleft = 0;
        rblock = 0;
        rnext = mapreg(c)->rg_first;
@@ -490,9 +580,9 @@ getREG()
 YANKreg(c)
        register int c;
 {
 YANKreg(c)
        register int c;
 {
-       struct rbuf arbuf;
        register line *addr;
        register struct strreg *sp;
        register line *addr;
        register struct strreg *sp;
+       char savelb[LBSIZE];
 
        if (isdigit(c))
                kshift();
 
        if (isdigit(c))
                kshift();
@@ -500,7 +590,7 @@ YANKreg(c)
                KILLreg(c);
        strp = sp = mapreg(c);
        sp->rg_flags = inopen && cursor && wcursor;
                KILLreg(c);
        strp = sp = mapreg(c);
        sp->rg_flags = inopen && cursor && wcursor;
-       rbuf = &arbuf;
+       rbuf = &YANKrbuf;
        if (sp->rg_last) {
                regio(sp->rg_last, read);
                rnleft = sp->rg_nleft;
        if (sp->rg_last) {
                regio(sp->rg_last, read);
                rnleft = sp->rg_nleft;
@@ -509,6 +599,7 @@ YANKreg(c)
                rblock = 0;
                rnleft = 0;
        }
                rblock = 0;
                rnleft = 0;
        }
+       CP(savelb,linebuf);
        for (addr = addr1; addr <= addr2; addr++) {
                getline(*addr);
                if (sp->rg_flags) {
        for (addr = addr1; addr <= addr2; addr++) {
                getline(*addr);
                if (sp->rg_flags) {
@@ -521,6 +612,7 @@ YANKreg(c)
        }
        rbflush();
        killed();
        }
        rbflush();
        killed();
+       CP(linebuf,savelb);
 }
 
 kshift()
 }
 
 kshift()
@@ -577,10 +669,9 @@ char c;
 char *buf;
 int buflen;
 {
 char *buf;
 int buflen;
 {
-       struct rbuf arbuf;
        register char *p, *lp;
 
        register char *p, *lp;
 
-       rbuf = &arbuf;
+       rbuf = &regrbuf;
        rnleft = 0;
        rblock = 0;
        rnext = mapreg(c)->rg_first;
        rnleft = 0;
        rblock = 0;
        rnext = mapreg(c)->rg_first;
@@ -606,142 +697,3 @@ int buflen;
  * Encryption routines.  These are essentially unmodified from ed.
  */
 
  * Encryption routines.  These are essentially unmodified from ed.
  */
 
-/*
- * crblock: encrypt/decrypt a block of text.
- * buf is the buffer through which the text is both input and
- * output. nchar is the size of the buffer. permp is a work
- * buffer, and startn is the beginning of a sequence.
- */
-crblock(permp, buf, nchar, startn)
-char *permp;
-char *buf;
-int nchar;
-long startn;
-{
-       register char *p1;
-       int n1;
-       int n2;
-       register char *t1, *t2, *t3;
-
-       t1 = permp;
-       t2 = &permp[256];
-       t3 = &permp[512];
-
-       n1 = startn&0377;
-       n2 = (startn>>8)&0377;
-       p1 = buf;
-       while(nchar--) {
-               *p1 = t2[(t3[(t1[(*p1+n1)&0377]+n2)&0377]-n2)&0377]-n1;
-               n1++;
-               if(n1==256){
-                       n1 = 0;
-                       n2++;
-                       if(n2==256) n2 = 0;
-               }
-               p1++;
-       }
-}
-
-/*
- * makekey: initialize buffers based on user key a.
- */
-makekey(a, b)
-char *a, *b;
-{
-       register int i;
-       long t;
-       char temp[KSIZE + 1];
-
-       for(i = 0; i < KSIZE; i++)
-               temp[i] = *a++;
-       time(&t);
-       t += getpid();
-       for(i = 0; i < 4; i++)
-               temp[i] ^= (t>>(8*i))&0377;
-       crinit(temp, b);
-}
-
-/*
- * crinit: besides initializing the encryption machine, this routine
- * returns 0 if the key is null, and 1 if it is non-null.
- */
-crinit(keyp, permp)
-char    *keyp, *permp;
-{
-       register char *t1, *t2, *t3;
-       register i;
-       int ic, k, temp;
-       unsigned random;
-       char buf[13];
-       long seed;
-
-       t1 = permp;
-       t2 = &permp[256];
-       t3 = &permp[512];
-       if(*keyp == 0)
-               return(0);
-       strncpy(buf, keyp, 8);
-       while (*keyp)
-               *keyp++ = '\0';
-
-       buf[8] = buf[0];
-       buf[9] = buf[1];
-       domakekey(buf);
-
-       seed = 123;
-       for (i=0; i<13; i++)
-               seed = seed*buf[i] + i;
-       for(i=0;i<256;i++){
-               t1[i] = i;
-               t3[i] = 0;
-       }
-       for(i=0; i<256; i++) {
-               seed = 5*seed + buf[i%13];
-               random = seed % 65521;
-               k = 256-1 - i;
-               ic = (random&0377) % (k+1);
-               random >>= 8;
-               temp = t1[k];
-               t1[k] = t1[ic];
-               t1[ic] = temp;
-               if(t3[k]!=0) continue;
-               ic = (random&0377) % k;
-               while(t3[ic]!=0) ic = (ic+1) % k;
-               t3[k] = ic;
-               t3[ic] = k;
-       }
-       for(i=0; i<256; i++)
-               t2[t1[i]&0377] = i;
-       return(1);
-}
-
-/*
- * domakekey: the following is the major nonportable part of the encryption
- * mechanism. A 10 character key is supplied in buffer.
- * This string is fed to makekey (an external program) which
- * responds with a 13 character result. This result is placed
- * in buffer.
- */
-domakekey(buffer)
-char *buffer;
-{
-       int pf[2];
-
-       if (pipe(pf)<0)
-               pf[0] = pf[1] = -1;
-       if (fork()==0) {
-               close(0);
-               close(1);
-               dup(pf[0]);
-               dup(pf[1]);
-               execl("/usr/lib/makekey", "-", 0);
-               execl("/lib/makekey", "-", 0);
-               exit(1);
-       }
-       write(pf[1], buffer, 10);
-       if (wait((int *)NULL)==-1 || read(pf[0], buffer, 13)!=13)
-               error("crypt: cannot generate key");
-       close(pf[0]);
-       close(pf[1]);
-       /* end of nonportable part */
-}