lost the SCCS id's...
[unix-history] / usr / src / bin / ed / ed.c
index 09bf1e1..3c554ae 100644 (file)
@@ -1,21 +1,29 @@
-/*     ed.c    4.2     82/12/24        */
+#ifndef lint
+static char sccsid[] = "@(#)ed.c       4.14 (Berkeley) %G%";
+#endif
 
 /*
  * Editor
  */
 
 /*
  * Editor
  */
-
+#include <sys/param.h>
+#include <sys/wait.h>
 #include <signal.h>
 #include <sgtty.h>
 #undef CEOF
 #include <signal.h>
 #include <sgtty.h>
 #undef CEOF
+#include <fcntl.h>
+#include <time.h>
 #include <setjmp.h>
 #include <setjmp.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "pathnames.h"
+
 #define        NULL    0
 #define        NULL    0
-#define        FNSIZE  64
 #define        LBSIZE  512
 #define        ESIZE   128
 #define        GBSIZE  256
 #define        NBRA    5
 #define        EOF     -1
 #define        LBSIZE  512
 #define        ESIZE   128
 #define        GBSIZE  256
 #define        NBRA    5
 #define        EOF     -1
-#define        KSIZE   9
 
 #define        CBRA    1
 #define        CCHR    2
 
 #define        CBRA    1
 #define        CCHR    2
@@ -36,8 +44,8 @@ char  T[]     = "TMP";
 
 int    peekc;
 int    lastc;
 
 int    peekc;
 int    lastc;
-char   savedfile[FNSIZE];
-char   file[FNSIZE];
+char   savedfile[MAXPATHLEN];
+char   file[MAXPATHLEN];
 char   linebuf[LBSIZE];
 char   rhsbuf[LBSIZE/2];
 char   expbuf[ESIZE+4];
 char   linebuf[LBSIZE];
 char   rhsbuf[LBSIZE/2];
 char   expbuf[ESIZE+4];
@@ -54,23 +62,17 @@ char        *linebp;
 int    ninbuf;
 int    io;
 int    pflag;
 int    ninbuf;
 int    io;
 int    pflag;
-long   lseek();
-int    (*oldhup)();
-int    (*oldquit)();
+sig_t  oldhup;
+sig_t  oldquit;
 int    vflag   = 1;
 int    vflag   = 1;
-int    xflag;
-int    xtflag;
-int    kflag;
-char   key[KSIZE + 1];
-char   crbuf[512];
-char   perm[768];
-char   tperm[768];
+
+
 int    listf;
 int    col;
 char   *globp;
 int    tfile   = -1;
 int    tline;
 int    listf;
 int    col;
 char   *globp;
 int    tfile   = -1;
 int    tline;
-char   *tfname;
+char   tfname[sizeof(_PATH_TMP) + 20];
 char   *loc1;
 char   *loc2;
 char   *locs;
 char   *loc1;
 char   *loc2;
 char   *locs;
@@ -96,17 +98,16 @@ int *address();
 char   *getline();
 char   *getblock();
 char   *place();
 char   *getline();
 char   *getblock();
 char   *place();
-char   *mktemp();
-char   *malloc();
-char   *realloc();
 jmp_buf        savej;
 
 jmp_buf        savej;
 
+void onintr(), quit(), onhup();
+
 main(argc, argv)
 main(argc, argv)
-char **argv;
+       int argc;
+       char **argv;
 {
        register char *p1, *p2;
 {
        register char *p1, *p2;
-       extern int onintr(), quit(), onhup();
-       int (*oldintr)();
+       sig_t oldintr;
 
        oldquit = signal(SIGQUIT, SIG_IGN);
        oldhup = signal(SIGHUP, SIG_IGN);
 
        oldquit = signal(SIGQUIT, SIG_IGN);
        oldhup = signal(SIGHUP, SIG_IGN);
@@ -126,17 +127,10 @@ char **argv;
                        vflag = 1;
                        break;
 
                        vflag = 1;
                        break;
 
-               case 'x':
-                       xflag = 1;
-                       break;
                }
                argv++;
                argc--;
        }
                }
                argv++;
                argc--;
        }
-       if(xflag){
-               getkey();
-               kflag = crinit(key, perm);
-       }
 
        if (argc>1) {
                p1 = *argv;
 
        if (argc>1) {
                p1 = *argv;
@@ -146,7 +140,9 @@ char **argv;
                globp = "r";
        }
        zero = (int *)malloc(nlall*sizeof(int));
                globp = "r";
        }
        zero = (int *)malloc(nlall*sizeof(int));
-       tfname = mktemp("/tmp/eXXXXX");
+       (void)strcpy(tfname, _PATH_TMP);
+       (void)strcat(tfname, "_edXXXXXX");
+       (void)mktemp(tfname);
        init();
        if (((int)oldintr&01) == 0)
                signal(SIGINT, onintr);
        init();
        if (((int)oldintr&01) == 0)
                signal(SIGINT, onintr);
@@ -344,14 +340,6 @@ commands()
                        fchange = 0;
                continue;
 
                        fchange = 0;
                continue;
 
-       case 'x':
-               setnoaddr();
-               newline();
-               xflag = 1;
-               puts("Entering encrypting mode!");
-               getkey();
-               kflag = crinit(key, perm);
-               continue;
 
 
        case '=':
 
 
        case '=':
@@ -565,18 +553,20 @@ exfile()
        }
 }
 
        }
 }
 
+void
 onintr()
 {
 onintr()
 {
-       signal(SIGINT, onintr);
+       /* not necessary: (void)signal(SIGINT, onintr); */
        putchr('\n');
        lastc = '\n';
        error(Q);
 }
 
        putchr('\n');
        lastc = '\n';
        error(Q);
 }
 
+void
 onhup()
 {
 onhup()
 {
-       signal(SIGINT, SIG_IGN);
-       signal(SIGHUP, SIG_IGN);
+       /* not necessary: (void)signal(SIGINT, SIG_IGN); */
+       /* not necessary: (void)signal(SIGHUP, SIG_IGN); */
        if (dol > zero) {
                addr1 = zero+1;
                addr2 = dol;
        if (dol > zero) {
                addr1 = zero+1;
                addr2 = dol;
@@ -673,8 +663,6 @@ getfile()
                        fp = genbuf;
                        while(fp < &genbuf[ninbuf]) {
                                if (*fp++ & 0200) {
                        fp = genbuf;
                        while(fp < &genbuf[ninbuf]) {
                                if (*fp++ & 0200) {
-                                       if (kflag)
-                                               crblock(perm, genbuf, ninbuf+1, count);
                                        break;
                                }
                        }
                                        break;
                                }
                        }
@@ -709,8 +697,6 @@ putfile()
                for (;;) {
                        if (--nib < 0) {
                                n = fp-genbuf;
                for (;;) {
                        if (--nib < 0) {
                                n = fp-genbuf;
-                               if(kflag)
-                                       crblock(perm, genbuf, n, count-n);
                                if(write(io, genbuf, n) != n) {
                                        puts(WRERR);
                                        error(Q);
                                if(write(io, genbuf, n) != n) {
                                        puts(WRERR);
                                        error(Q);
@@ -726,8 +712,6 @@ putfile()
                }
        } while (a1 <= addr2);
        n = fp-genbuf;
                }
        } while (a1 <= addr2);
        n = fp-genbuf;
-       if(kflag)
-               crblock(perm, genbuf, n, count-n);
        if(write(io, genbuf, n) != n) {
                puts(WRERR);
                error(Q);
        if(write(io, genbuf, n) != n) {
                puts(WRERR);
                error(Q);
@@ -747,7 +731,6 @@ int (*f)();
                if ((dol-zero)+1 >= nlall) {
                        int *ozero = zero;
                        nlall += 512;
                if ((dol-zero)+1 >= nlall) {
                        int *ozero = zero;
                        nlall += 512;
-                       free((char *)zero);
                        if ((zero = (int *)realloc((char *)zero, nlall*sizeof(int)))==NULL) {
                                lastc = '\n';
                                zero = ozero;
                        if ((zero = (int *)realloc((char *)zero, nlall*sizeof(int)))==NULL) {
                                lastc = '\n';
                                zero = ozero;
@@ -770,14 +753,15 @@ int (*f)();
 
 callunix()
 {
 
 callunix()
 {
-       register (*savint)(), pid, rpid;
+       register sig_t savint;
+       register int pid, rpid;
        int retcode;
 
        setnoaddr();
        if ((pid = fork()) == 0) {
                signal(SIGHUP, oldhup);
                signal(SIGQUIT, oldquit);
        int retcode;
 
        setnoaddr();
        if ((pid = fork()) == 0) {
                signal(SIGHUP, oldhup);
                signal(SIGQUIT, oldquit);
-               execl("/bin/sh", "sh", "-t", 0);
+               execl(_PATH_BSHELL, "sh", "-t", 0);
                exit(0100);
        }
        savint = signal(SIGINT, SIG_IGN);
                exit(0100);
        }
        savint = signal(SIGINT, SIG_IGN);
@@ -787,6 +771,7 @@ callunix()
        puts("!");
 }
 
        puts("!");
 }
 
+void
 quit()
 {
        if (vflag && fchange && dol!=zero) {
 quit()
 {
        if (vflag && fchange && dol!=zero) {
@@ -914,27 +899,14 @@ getblock(atl, iof)
                return(obuff+off);
        if (iof==READ) {
                if (ichanged) {
                return(obuff+off);
        if (iof==READ) {
                if (ichanged) {
-                       if(xtflag)
-                               crblock(tperm, ibuff, 512, (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, 512, (long)0);
                return(ibuff+off);
        }
        if (oblock>=0) {
                return(ibuff+off);
        }
        if (oblock>=0) {
-               if(xtflag) {
-                       p1 = obuff;
-                       p2 = crbuf;
-                       n = 512;
-                       while(n--)
-                               *p2++ = *p1++;
-                       crblock(tperm, crbuf, 512, (long)0);
-                       blkio(oblock, crbuf, write);
-               } else
                        blkio(oblock, obuff, write);
        }
        oblock = bno;
                        blkio(oblock, obuff, write);
        }
        oblock = bno;
@@ -966,10 +938,6 @@ init()
        ichanged = 0;
        close(creat(tfname, 0600));
        tfile = open(tfname, 2);
        ichanged = 0;
        close(creat(tfname, 0600));
        tfile = open(tfname, 2);
-       if(xflag) {
-               xtflag = 1;
-               makekey(key, tperm);
-       }
        dot = dol = zero;
 }
 
        dot = dol = zero;
 }
 
@@ -1625,141 +1593,4 @@ out:
        }
        linp = lp;
 }
        }
        linp = lp;
 }
-crblock(permp, buf, nchar, startn)
-char *permp;
-char *buf;
-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++;
-       }
-}
 
 
-getkey()
-{
-       struct sgttyb b;
-       int save;
-       int (*sig)();
-       register char *p;
-       register c;
-
-       sig = signal(SIGINT, SIG_IGN);
-       if (gtty(0, &b) == -1)
-               error("Input not tty");
-       save = b.sg_flags;
-       b.sg_flags &= ~ECHO;
-       stty(0, &b);
-       puts("Key:");
-       p = key;
-       while(((c=getchr()) != EOF) && (c!='\n')) {
-               if(p < &key[KSIZE])
-                       *p++ = c;
-       }
-       *p = 0;
-       b.sg_flags = save;
-       stty(0, &b);
-       signal(SIGINT, sig);
-       return(key[0] != 0);
-}
-
-/*
- * 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, pf[2];
-       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];
-       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], buf, 10);
-       if (wait((int *)NULL)==-1 || read(pf[0], buf, 13)!=13)
-               error("crypt: cannot generate key");
-       close(pf[0]);
-       close(pf[1]);
-       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);
-}
-
-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);
-}