Research V6 development
authorKen Thompson <ken@research.uucp>
Wed, 14 May 1975 00:49:30 +0000 (19:49 -0500)
committerKen Thompson <ken@research.uucp>
Wed, 14 May 1975 00:49:30 +0000 (19:49 -0500)
Work on file usr/source/s1/cpall.c
Work on file usr/source/s1/chgrp.s
Work on file usr/source/s1/cron.c
Work on file usr/source/s1/cp.c
Work on file usr/source/s1/cdb2.c

Co-Authored-By: Dennis Ritchie <dmr@research.uucp>
Synthesized-from: v6

usr/source/s1/cdb2.c [new file with mode: 0644]
usr/source/s1/chgrp.s [new file with mode: 0644]
usr/source/s1/cp.c [new file with mode: 0644]
usr/source/s1/cpall.c [new file with mode: 0644]
usr/source/s1/cron.c [new file with mode: 0644]

diff --git a/usr/source/s1/cdb2.c b/usr/source/s1/cdb2.c
new file mode 100644 (file)
index 0000000..08d9365
--- /dev/null
@@ -0,0 +1,384 @@
+#
+/*
+ * C debugger -- part 2
+ */
+
+char   ssymbol[];
+int    dotinc;
+int    dot;
+
+
+psymoff(v, lim)
+{
+       register char *w;
+
+       w = vallook(v);
+       if (w > lim) {
+               printf("%.1o", v);
+               return;
+       }
+       printf("%.8s", ssymbol);
+       if (w)
+               printf("+%d", w);
+}
+
+#define        ISP     1
+#define        DOUBLE  0
+#define        SINGLE  1
+#define        SINGLW  2
+#define        MULDIV  4
+#define        BRANCH  5
+#define        NOADDR  6
+#define        FLTREV  7
+#define        FLTNOR  8
+#define        SPECL1  9
+#define        SPECL2  10
+#define        SPECL3  11
+#define        SPECL4  12
+#define        SPECL5  13
+#define        SPECL6  14
+#define        SPECL7  15
+#define        SPECL8  18
+#define        SPECL9  19
+
+struct optab {
+       int     mask;
+       int     val;
+       int     itype;
+       char    *iname;
+} optab[] {
+       0107777, 0010000, DOUBLE, "mov",
+       0107777, 0020000, DOUBLE, "cmp",
+       0107777, 0030000, DOUBLE, "bit",
+       0107777, 0040000, DOUBLE, "bic",
+       0107777, 0050000, DOUBLE, "bis",
+       0007777, 0060000, DOUBLE, "add",
+       0007777, 0160000, DOUBLE, "su",
+       0100077, 0005000, SINGLE, "clr",
+       0100077, 0005100, SINGLE, "com",
+       0100077, 0005200, SINGLE, "inc",
+       0100077, 0005300, SINGLE, "dec",
+       0100077, 0005400, SINGLE, "neg",
+       0100077, 0005500, SINGLE, "adc",
+       0100077, 0005600, SINGLE, "sbc",
+       0100077, 0005700, SINGLE, "tst",
+       0100077, 0006000, SINGLE, "ror",
+       0100077, 0006100, SINGLE, "rol",
+       0100077, 0006200, SINGLE, "asr",
+       0100077, 0006300, SINGLE, "asl",
+       0000077, 0000100, SINGLE, "jmp",
+       0000077, 0000300, SINGLE, "swab",
+       0000077, 0170100, SINGLW, "ldfps",
+       0000077, 0170200, SINGLW, "stfps",
+       0000077, 0170300, SINGLW, "stst",
+       0000077, 0170400, SINGLW, "clrf",
+       0000077, 0170500, SINGLW, "tstf",
+       0000077, 0170600, SINGLW, "absf",
+       0000077, 0170700, SINGLW, "negf",
+       0000077, 0006700, SINGLW, "sxt",
+       0000077, 0006600, SINGLW, "mtpi",
+       0000077, 0106600, SINGLW, "mtpd",
+       0000077, 0006500, SINGLW, "mfpi",
+       0000077, 0106500, SINGLW, "mfpd",
+       0000777, 0070000, MULDIV, "mul",
+       0000777, 0071000, MULDIV, "div",
+       0000777, 0072000, MULDIV, "ash",
+       0000777, 0073000, MULDIV, "ashc",
+       0000377, 0000400, BRANCH, "br",
+       0000377, 0001000, BRANCH, "bne",
+       0000377, 0001400, BRANCH, "beq",
+       0000377, 0002000, BRANCH, "bge",
+       0000377, 0002400, BRANCH, "blt",
+       0000377, 0003000, BRANCH, "bgt",
+       0000377, 0003400, BRANCH, "ble",
+       0000377, 0100000, BRANCH, "bpl",
+       0000377, 0100400, BRANCH, "bmi",
+       0000377, 0101000, BRANCH, "bhi",
+       0000377, 0101400, BRANCH, "blos",
+       0000377, 0102000, BRANCH, "bvc",
+       0000377, 0102400, BRANCH, "bvs",
+       0000377, 0103000, BRANCH, "bhis",
+       0000377, 0103400, BRANCH, "blo",
+       0000000, 0000000, NOADDR, "halt",
+       0000000, 0000001, NOADDR, "wait",
+       0000000, 0000002, NOADDR, "rti",
+       0000000, 0000004, NOADDR, "iot",
+       0000000, 0000005, NOADDR, "reset",
+       0000377, 0171000, FLTREV, "mulf",
+       0000377, 0171400, FLTREV, "modf",
+       0000377, 0172000, FLTREV, "addf",
+       0000377, 0172400, FLTREV, "movf",
+       0000377, 0173000, FLTREV, "subf",
+       0000377, 0173400, FLTREV, "cmpf",
+       0000377, 0174000, FLTNOR, "movf",
+       0000377, 0174400, FLTREV, "divf",
+       0000377, 0175000, FLTNOR, "movei",
+       0000377, 0175400, FLTNOR, "movfi",
+       0000377, 0176000, FLTNOR, "movfo",
+       0000377, 0176400, FLTREV, "movie",
+       0000377, 0177000, FLTREV, "movif",
+       0000377, 0177400, FLTREV, "movof",
+       0000000, 0170000, NOADDR, "cfcc",
+       0000000, 0170001, NOADDR, "setf",
+       0000000, 0170002, NOADDR, "seti",
+       0000000, 0170011, NOADDR, "setd",
+       0000000, 0170012, NOADDR, "setl",
+       0000777, 0004000, SPECL1, "jsr",
+       0000777, 0074000, SPECL1, "xor",
+       0000007, 0000200, SPECL2, "rts",
+       0000017, 0000240, SPECL3, "cflg",
+       0000017, 0000260, SPECL3, "sflg",
+       0000377, 0104000, SPECL4, "emt",
+       0000377, 0104400, SPECL5, "sys",
+       0000077, 0006400, SPECL7, "mark",
+       0000777, 0077000, SPECL8, "sob",
+       0000007, 0000230, SPECL9, "spl",
+       0177777, 0000000, SPECL6, "oct",
+};
+
+struct systab {
+       int     argc;
+       char    *sname;
+} systab[] {
+       1, "indir",
+       0, "exit",
+       0, "fork",
+       2, "read",
+       2, "write",
+       2, "open",
+       0, "close",
+       0, "wait",
+       2, "creat",
+       2, "link",
+       1, "unlink",
+       2, "exec",
+       1, "chdir",
+       0, "time",
+       3, "mknod",
+       2, "chmod",
+       2, "chown",
+       1, "break",
+       2, "stat",
+       2, "seek",
+       0, "getpid",
+       3, "mount",
+       1, "umount",
+       0, "setuid",
+       0, "getuid",
+       0, "stime",
+       3, "ptrace",
+       0, "27",
+       1, "fstat",
+       0, "29",
+       1, "smdate",
+       1, "stty",
+       1, "gtty",
+       0, "33",
+       0, "nice",
+       0, "sleep",
+       0, "sync",
+       1, "kill",
+       0, "switch",
+       0, "39",
+       0, "40",
+       0, "dup",
+       0, "pipe",
+       1, "times",
+       4, "profil",
+       0, "45",
+       0, "setgid",
+       0, "getgid",
+       2, "signal",
+       0, "49",
+       0, "50",
+       0, "51",
+       0, "52",
+       0, "53",
+       0, "54",
+       0, "55",
+       0, "56",
+       0, "57",
+       0, "58",
+       0, "59",
+       0, "60",
+       0, "61",
+       0, "62",
+       0, "63",
+};
+
+char   *regname[] { "r0", "r1", "r2", "r3", "r4", "r5", "sp", "pc"};
+
+printins(f)
+{
+       register ins, w;
+       register struct optab *p;
+
+       dotinc = 2;
+       ins = cget(dot, ISP);
+       if (vallook(dot)==0)
+               printf("%.8s:", ssymbol);
+       printf("\t");
+       for (p=optab;; p++)
+               if ((ins & ~p->mask) == p->val)
+                       break;
+       printf("%s", p->iname);
+       switch (p->itype) {
+
+       /* rts */
+       case SPECL2:
+               ins =& 07;
+
+       case SINGLE:
+               if (ins < 0)
+                       printf("b");
+
+       case SINGLW:
+               printf("\t");
+               paddr(ins);
+               return;
+
+       case FLTREV:
+               ins =& 0377;
+
+       case MULDIV:
+               ins = ((ins>>6)&07) | ((ins<<6)&07700);
+               goto doub;
+
+       case FLTNOR:
+               ins =& 0377;
+
+       /* jsr, xor */
+       case SPECL1:
+               ins =& 0777;
+               goto doub;
+
+       case DOUBLE:
+               if (ins<0)
+                       printf("b");
+       doub:
+               printf("\t");
+               paddr(ins>>6);
+               printf(",");
+               paddr(ins);
+               return;
+
+       case NOADDR:
+               return;
+
+       /* sob */
+       case SPECL8:
+               printf("\t");
+               paddr((ins>>6)&07);
+               printf(",");
+               ins = - (ins&077);
+               goto bran;
+
+       case BRANCH:
+               printf("\t");
+       bran:
+               ins =& 0377;
+               if (ins&0200)
+                       ins =| 0177400;
+               ins = dot + (ins<<1) + 2;
+               psymoff(ins, 010000);
+               return;
+
+       /* emt */
+       case SPECL4:
+               ins =& 0377;
+       /* mark */
+       case SPECL7:
+               ins =& 077;
+       /* spl */
+       case SPECL9:
+               ins =& 07;
+               printf("\t%d", ins);
+               return;
+
+       /* sys */
+       case SPECL5:
+               printf("\t%s", systab[ins =& 077].sname);
+               if (ins==0 && f==0) {   /* indir */
+                       w = dot;
+                       dot = cget(dot+2, ISP);
+                       printf(" {");
+                       printins(1);
+                       printf("}");
+                       dotinc = 4;
+                       dot = w;
+                       return;
+               }
+               w = systab[ins].argc;
+               while (w--) {
+                       printf("; ");
+                       psymoff(cget(dot+dotinc, ISP), 010000);
+                       dotinc =+ 2;
+               }
+               return;
+
+       default:
+               printf("\t%.1o", ins);
+       }
+}
+
+paddr(aa)
+{
+       register a, r;
+
+       a = aa;
+       r = a&07;
+       a =& 070;
+       if (r==7 && a&020) {
+               if (a&010)
+                       printf("*");
+               if (a&040)
+                       psymoff(cget(dot+dotinc, ISP)+dot+dotinc+2, 010000);
+               else {
+                       printf("$");
+                       psymoff(cget(dot+dotinc, ISP), 010000);
+               }
+               dotinc =+ 2;
+               return;
+       }
+       r = regname[r];
+       switch (a) {
+       /* r */
+       case 000:
+               printf("%s", r);
+               return;
+
+       /* (r) */
+       case 010:
+               printf("(%s)", r);
+               return;
+
+       /* *(r)+ */
+       case 030:
+               printf("*");
+
+       /* (r)+ */
+       case 020:
+               printf("(%s)+", r);
+               return;
+
+       /* *-(r) */
+       case 050:
+               printf("*");
+
+       /* -(r) */
+       case 040:
+               printf("-(%s)", r);
+               return;
+
+       /* *x(r) */
+       case 070:
+               printf("*");
+
+       /* x(r) */
+       case 060:
+               psymoff(cget(dot+dotinc, ISP), 010000);
+               dotinc =+ 2;
+               printf("(%s)", r);
+               return;
+       }
+}
diff --git a/usr/source/s1/chgrp.s b/usr/source/s1/chgrp.s
new file mode 100644 (file)
index 0000000..eaab5a9
--- /dev/null
@@ -0,0 +1,106 @@
+/ chgrp -- change group
+
+
+       .globl  fopen, getc, mesg
+
+       mov     sp,r5
+       mov     (r5),r4
+       cmp     r4,$3
+       bge     1f
+       jsr     r5,mesg; <chown uid f1 ...\n\0>; .even
+1:
+       add     $4,r5
+       mov     (r5),r3
+       cmpb    (r3),$'0
+       blt     1f
+       cmpb    (r3),$'9
+       bgt     1f
+       jsr     r5,cvnum; geta
+       br      do
+1:
+       mov     $uids,r0
+       jsr     r5,fopen; ubuf
+       bec     1f
+       jsr     r5,mesg; <Can't open /etc/uids\n\0>; .even
+       sys     exit
+1:
+       mov     r3,r2
+2:
+       jsr     r5,getc; ubuf
+       bcc     3f
+who:
+       jsr     r5,mesg; <Who?\n\0>; .even
+       sys     exit
+3:
+       cmp     r0,$':
+       beq     3f
+       cmpb    (r2)+,r0
+       beq     2b
+2:
+       jsr     r5,getc; ubuf
+       bcs     who
+       cmp     r0,$'\n
+       bne     2b
+       br      1b
+3:
+       tstb    (r2)
+       bne     2b
+3:
+       jsr     r5,getc; ubuf
+       cmpb    r0,$':
+       bne     3b
+       jsr     r5,cvnum; getc
+do:
+       sub     $2,r4
+       swab    r1
+       mov     r1,0f+2
+       tst     (r5)+
+1:
+       mov     (r5),0f
+       mov     (r5)+,9f
+       sys     stat; 9:..; statb
+       movb    statb+7,0f+2
+       sys     chown; 0:..; 0
+       bec     2f
+       mov     0b,r0
+       mov     r0,0f
+       clr     0f+2
+3:
+       tstb    (r0)+
+       beq     3f
+       inc     0f+2
+       br      3b
+3:
+       mov     $1,r0
+       sys     write; 0:..; ..
+       jsr     r5,mesg; <?\n\0>; .even
+2:
+       dec     r4
+       bgt     1b
+       sys     exit
+
+cvnum:
+       clr     r1
+1:
+       jsr     r5,*(r5); ubuf
+       bcs     1f
+       sub     $'0,r0
+       cmp     r0,$9.
+       bhi     1f
+       mpy     $10.,r1
+       add     r0,r1
+       br      1b
+1:
+       tst     (r5)+
+       rts     r5
+
+geta:
+       movb    (r3)+,r0
+       tst     (r5)+
+       rts     r5
+
+uids:  </etc/group\0>
+
+       .bss
+statb: .=.+36.
+ubuf:  .=.+518.
diff --git a/usr/source/s1/cp.c b/usr/source/s1/cp.c
new file mode 100644 (file)
index 0000000..4da67c7
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * cp oldfile newfile
+ */
+
+main(argc,argv)
+char **argv;
+{
+       static int buf[256];
+       int fold, fnew, n;
+       register char *p1, *p2, *bp;
+       int mode;
+
+       if(argc != 3) {
+               write(1, "Usage: cp oldfile newfile\n", 26);
+               exit(1);
+       }
+       if((fold = open(argv[1], 0)) < 0) {
+               write(1, "Cannot open old file.\n", 22);
+               exit(1);
+       }
+       fstat(fold, buf);
+       mode = buf[2];
+       /* is target a directory? */
+       if (stat(argv[2], buf+50)>=0 && (buf[52]&060000)==040000) {
+               p1 = argv[1];
+               p2 = argv[2];
+               bp = buf+100;
+               while(*bp++ = *p2++);
+               bp[-1] = '/';
+               p2 = bp;
+               while(*bp = *p1++)
+                       if(*bp++ == '/')
+                               bp = p2;
+               argv[2] = buf+100;
+       }
+       if (stat(argv[2], buf+50) >= 0) {
+               if (buf[0]==buf[50] && buf[1]==buf[51]) {
+                       write(1, "Copying file to itself.\n", 24);
+                       exit(1);
+               }
+       }
+       if ((fnew = creat(argv[2], mode)) < 0) {
+               write(1, "Can't create new file.\n", 23);
+               exit(1);
+       }
+       while(n = read(fold,  buf,  512)) {
+       if(n < 0) {
+               write(1, "Read error\n", 11);
+               exit(1);
+       } else
+               if(write(fnew, buf, n) != n){
+                       write(1, "Write error.\n", 13);
+                       exit(1);
+               }
+       }
+       exit(0);
+}
diff --git a/usr/source/s1/cpall.c b/usr/source/s1/cpall.c
new file mode 100644 (file)
index 0000000..2349e8d
--- /dev/null
@@ -0,0 +1,27 @@
+char   buf[100];
+
+main(argc, argv)
+char **argv;
+{
+       register i;
+       register char *c1, *c2;
+
+       if(argc < 3) {
+               write(2, "arg count\n", 10);
+               exit();
+       }
+       argc--;
+       c1 = buf;
+       c2 = argv[argc];
+       while(*c1++ = *c2++);
+       c1[-1] = '/';
+       *c1++ = '.';
+       *c1 = '\0';
+       for(i=1; i<argc; i++) {
+               if(fork()==0) {
+                       execl("/bin/cp", "cp", argv[i], buf);
+                       exit();
+               }
+               wait();
+       }
+}
diff --git a/usr/source/s1/cron.c b/usr/source/s1/cron.c
new file mode 100644 (file)
index 0000000..8fa2db8
--- /dev/null
@@ -0,0 +1,234 @@
+#define        ANY     -1
+#define        LIST    -2
+#define        RANGE   -3
+#define        EOF     -4
+char   *crontab        "/usr/lib/crontab";
+char   *crontmp        "/tmp/crontmp";
+char   *aend;
+char   *itime[2];
+int    *loct;
+int    *localtime();
+int    reset();
+int    flag;
+
+main()
+{
+       register i, t;
+       register char *cp;
+       extern char end[];
+
+       setuid(1);
+       itime[0] = fork();
+       if(itime[0])
+               exit();
+       setexit();
+       signal(1, reset);
+       time(itime);
+       while(*localtime(itime))
+               if(itime[1]-- == 0)
+                       itime[0]--;
+
+loop:
+       init();
+       for(i=60; i; i--) {
+               loct = localtime(itime);
+               for(cp = end; *cp != EOF;) {
+                       flag = 0;
+                       cp = cmp(cp, loct[1]); /* minute */
+                       cp = cmp(cp, loct[2]); /* hour */
+                       cp = cmp(cp, loct[3]); /* day */
+                       cp = cmp(cp, loct[4]); /* month */
+                       cp = cmp(cp, loct[6]); /* day of week */
+                       if(flag == 0) {
+                               slp();
+                               ex(cp);
+                       }
+                       while(*cp++ != 0)
+                               ;
+               }
+               t = itime[1] + 60;
+               if(t < itime[1])
+                       itime[0]++;
+               itime[1] = t;
+       }
+       slp();
+       goto loop;
+}
+
+cmp(p, v)
+char *p;
+{
+       register char *cp;
+
+       cp = p;
+       switch(*cp++) {
+
+       case ANY:
+               return(cp);
+
+       case LIST:
+               while(*cp != LIST)
+                       if(*cp++ == v) {
+                               while(*cp++ != LIST)
+                                       ;
+                               return(cp);
+                       }
+               flag++;
+               return(cp+1);
+
+       case RANGE:
+               if(*cp > v || cp[1] < v)
+                       flag++;
+               return(cp+2);
+       }
+       if(cp[-1] != v)
+               flag++;
+       return(cp);
+}
+
+slp()
+{
+       register i;
+       int     t[2];
+
+       time(t);
+       i = itime[1] - t[1];
+       if(i > 0)
+               sleep(i);
+}
+
+ex(s)
+char *s;
+{
+       register i;
+
+       if(fork()) {
+               wait();
+               return;
+       }
+       for(i=0; s[i]; i++);
+       close(0);
+       creat(crontmp, 0600);
+       write(0, s, i);
+       close(0);
+       open(crontmp, 0);
+       unlink(crontmp);
+       if(fork())
+               exit();
+       execl("/bin/sh", "sh", "-t", 0);
+       exit();
+}
+
+init()
+{
+       int ib[259], t[10];
+       register i, c;
+       register char *cp;
+       char *ocp;
+       int n;
+       extern char end[];
+
+       if(fopen(crontab, ib) < 0) {
+               write(1, "cannot open table\n", 18);
+               exit();
+       }
+       cp = end;
+       if(aend == 0)
+               aend = cp;
+
+loop:
+       ocp = cp;
+       if(cp+100 > aend) {
+               aend =+ 512;
+               brk(aend);
+       }
+       for(i=0;; i++) {
+               do
+                       c = getc(ib);
+               while(c == ' ' || c == '\t');
+               if(c <= 0 || c == '\n')
+                       goto ignore;
+               if(i == 5)
+                       break;
+               if(c == '*') {
+                       *cp++ = ANY;
+                       continue;
+               }
+               n = 0;
+               while(c >= '0' && c <= '9') {
+                       n = n*10 + c-'0';
+                       c = getc(ib);
+               }
+               if(n < 0 || n > 100)
+                       goto ignore;
+               if(c == ',')
+                       goto list;
+               if(c == '-')
+                       goto range;
+               if(c != '\t' && c != ' ')
+                       goto ignore;
+               *cp++ = n;
+               continue;
+
+       list:
+               *cp++ = LIST;
+               *cp++ = n;
+       list1:
+               n = 0;
+               c = getc(ib);
+               while(c >= '0' && c <= '9') {
+                       n = n*10 + c-'0';
+                       c = getc(ib);
+               }
+               if(n < 0 || n > 100)
+                       goto ignore;
+               *cp++ = n;
+               if(c == ',')
+                       goto list1;
+               if(c != '\t' && c != ' ')
+                       goto ignore;
+               *cp++ = LIST;
+               continue;
+
+       range:
+               *cp++ = RANGE;
+               *cp++ = n;
+               n = 0;
+               c = getc(ib);
+               while(c >= '0' && c <= '9') {
+                       n = n*10 + c-'0';
+                       c = getc(ib);
+               }
+               if(n < 0 || n > 100)
+                       goto ignore;
+               if(c != '\t' && c != ' ')
+                       goto ignore;
+               *cp++ = n;
+       }
+       while(c != '\n') {
+               if(c <= 0)
+                       goto ignore;
+               if(c == '%')
+                       c = '\n';
+               *cp++ = c;
+               c = getc(ib);
+       }
+       *cp++ = '\n';
+       *cp++ = 0;
+       goto loop;
+
+ignore:
+       cp = ocp;
+       while(c != '\n') {
+               if(c <= 0) {
+                       close(ib[0]);
+                       *cp++ = EOF;
+                       *cp++ = EOF;
+                       aend = cp;
+                       brk(aend);
+                       return;
+               }
+               c = getc(ib);
+       }
+       goto loop;
+}