Research V6 development
authorDennis Ritchie <dmr@research.uucp>
Wed, 14 May 1975 00:43:23 +0000 (19:43 -0500)
committerDennis Ritchie <dmr@research.uucp>
Wed, 14 May 1975 00:43:23 +0000 (19:43 -0500)
Work on file usr/source/iolib/cgetc.c
Work on file usr/source/iolib/cflush.c
Work on file usr/source/iolib/cfree.c
Work on file usr/source/iolib/cerror.c
Work on file usr/source/iolib/cexit.c
Work on file usr/source/iolib/cclose.c
Work on file usr/source/iolib/calloc.c
Work on file usr/source/iolib/alloc.c
Work on file usr/source/iolib/ceof.c
Work on file usr/source/iolib/puts.c
Work on file usr/source/iolib/gets.c
Work on file usr/source/iolib/copen.c
Work on file usr/source/iolib/clenf.c
Work on file usr/source/iolib/iehzap.c
Work on file usr/source/iolib/nexch.c
Work on file usr/source/iolib/revput.c
Work on file usr/source/iolib/maktab.c
Work on file usr/source/iolib/dummy.s
Work on file usr/source/iolib/makbuf.c
Work on file usr/source/iolib/relvec.c
Work on file usr/source/iolib/nodig.c
Work on file usr/source/iolib/cwrd.c
Work on file usr/source/iolib/getch.c
Work on file usr/source/iolib/ftoa.c
Work on file usr/source/iolib/printf.c
Work on file usr/source/iolib/getvec.c
Work on file usr/source/iolib/ciodec.c
Work on file usr/source/iolib/cputc.c
Work on file usr/source/iolib/putch.c
Work on file usr/source/iolib/scan1.c
Work on file usr/source/iolib/wdleng.c
Work on file usr/source/iolib/system.c
Work on file usr/source/iolib/scan2.c
Work on file usr/source/iolib/tmpnam.c
Work on file usr/source/iolib/unprnt.s
Work on file usr/source/iolib/scan3.c
Work on file usr/source/iolib/unget.c

Synthesized-from: v6

37 files changed:
usr/source/iolib/alloc.c [new file with mode: 0644]
usr/source/iolib/calloc.c [new file with mode: 0644]
usr/source/iolib/cclose.c [new file with mode: 0644]
usr/source/iolib/ceof.c [new file with mode: 0644]
usr/source/iolib/cerror.c [new file with mode: 0644]
usr/source/iolib/cexit.c [new file with mode: 0644]
usr/source/iolib/cflush.c [new file with mode: 0644]
usr/source/iolib/cfree.c [new file with mode: 0644]
usr/source/iolib/cgetc.c [new file with mode: 0644]
usr/source/iolib/ciodec.c [new file with mode: 0644]
usr/source/iolib/clenf.c [new file with mode: 0644]
usr/source/iolib/copen.c [new file with mode: 0644]
usr/source/iolib/cputc.c [new file with mode: 0644]
usr/source/iolib/cwrd.c [new file with mode: 0644]
usr/source/iolib/dummy.s [new file with mode: 0644]
usr/source/iolib/ftoa.c [new file with mode: 0644]
usr/source/iolib/getch.c [new file with mode: 0644]
usr/source/iolib/gets.c [new file with mode: 0644]
usr/source/iolib/getvec.c [new file with mode: 0644]
usr/source/iolib/iehzap.c [new file with mode: 0644]
usr/source/iolib/makbuf.c [new file with mode: 0644]
usr/source/iolib/maktab.c [new file with mode: 0644]
usr/source/iolib/nexch.c [new file with mode: 0644]
usr/source/iolib/nodig.c [new file with mode: 0644]
usr/source/iolib/printf.c [new file with mode: 0644]
usr/source/iolib/putch.c [new file with mode: 0644]
usr/source/iolib/puts.c [new file with mode: 0644]
usr/source/iolib/relvec.c [new file with mode: 0644]
usr/source/iolib/revput.c [new file with mode: 0644]
usr/source/iolib/scan1.c [new file with mode: 0644]
usr/source/iolib/scan2.c [new file with mode: 0644]
usr/source/iolib/scan3.c [new file with mode: 0644]
usr/source/iolib/system.c [new file with mode: 0644]
usr/source/iolib/tmpnam.c [new file with mode: 0644]
usr/source/iolib/unget.c [new file with mode: 0644]
usr/source/iolib/unprnt.s [new file with mode: 0644]
usr/source/iolib/wdleng.c [new file with mode: 0644]

diff --git a/usr/source/iolib/alloc.c b/usr/source/iolib/alloc.c
new file mode 100644 (file)
index 0000000..952f5a7
--- /dev/null
@@ -0,0 +1,75 @@
+#
+/*
+ * C library -- alloc/free
+ */
+
+#define        logical char *
+
+struct fb {
+       logical size;
+       char    *next;
+};
+
+int    freelist[] {
+       0,
+       -1,
+};
+logical        slop    2;
+
+alloc(asize)
+logical asize;
+{
+       register logical size;
+       register logical np;
+       register logical cp;
+
+       if ((size = asize) == 0)
+               return(0);
+       size =+ 3;
+       size =& ~01;
+       for (;;) {
+               for (cp=freelist; (np= cp->next) != -1; cp=np) {
+                       if (np->size>=size) {
+                               if (size+slop >= np->size) {
+                                       cp->next = np->next;
+                                       return(&np->next);
+                               }
+                               cp = cp->next = np+size;
+                               cp->size = np->size - size;
+                               cp->next = np->next;
+                               np->size = size;
+                               return(&np->next);
+                       }
+               }
+               asize = size<1024? 1024: size;
+               if ((cp = sbrk(asize)) == -1) {
+                       return (-1);
+               }
+               cp->size = asize;
+               free(&cp->next);
+       }
+}
+
+free(aptr)
+char *aptr;
+{
+       register logical ptr;
+       register logical cp;
+       register logical np;
+
+       ptr = aptr-2;
+       cp = freelist;
+       while ((np = cp->next) < ptr)
+               cp = np;
+       if (ptr+ptr->size == np) {
+               ptr->size =+ np->size;
+               ptr->next = np->next;
+               np = ptr;
+       } else
+               ptr->next = np;
+       if (cp+cp->size == ptr) {
+               cp->size =+ ptr->size;
+               cp->next = ptr->next;
+       } else
+               cp->next = ptr;
+}
diff --git a/usr/source/iolib/calloc.c b/usr/source/iolib/calloc.c
new file mode 100644 (file)
index 0000000..146db61
--- /dev/null
@@ -0,0 +1,4 @@
+calloc(n, s)
+{
+return(alloc(n*s));
+}
diff --git a/usr/source/iolib/cclose.c b/usr/source/iolib/cclose.c
new file mode 100644 (file)
index 0000000..7d2e94d
--- /dev/null
@@ -0,0 +1,24 @@
+# include "ciodec.c"
+cclose (fn)
+int fn;
+{
+struct fileps *fp;
+extern char *IEH3olbf[10];
+if (fn < 0 || fn > 20) return;
+fp = &IEH3fpts[fn];
+if (fp->nchars > 0 || fp->wrflag == 3)
+       switch (fp->wrflag)
+               {
+               case 3: seek(fn,0,2);
+               case 2: write (fn, fp->buff, fp->nchars);
+                       break;
+               case 1: seek(fn, -(fp->nchars), 1);
+               }
+fp -> nchars = fp->eoferr = 0;
+fp -> wrflag = 0;
+if ( IEH3olbf[fn] != 0 && fp->bsize >4)
+       free(IEH3olbf[fn]);
+IEH3olbf[fn] = 0;
+close (fn);
+}
+char *IEH3olbf[10] {0,0,0,0,0,0,0,0,0,0};
diff --git a/usr/source/iolib/ceof.c b/usr/source/iolib/ceof.c
new file mode 100644 (file)
index 0000000..75cd006
--- /dev/null
@@ -0,0 +1,20 @@
+# include "ciodec.c"
+ceof (fn)
+int fn;
+{
+struct fileps *fp;
+fp = &IEH3fpts[fn];
+if (fp->wrflag > 0 && fp->eoferr == 2)
+       return(1);
+else return(0);
+}
+cerr (fn)
+int fn;
+{
+struct fileps *fp;
+fp = &IEH3fpts[fn];
+if (fp->eoferr == 0) fp->eoferr = 1;
+if (fp->wrflag > 0 && fp->eoferr == 3)
+       return(1);
+else return(0);
+}
diff --git a/usr/source/iolib/cerror.c b/usr/source/iolib/cerror.c
new file mode 100644 (file)
index 0000000..bd197ff
--- /dev/null
@@ -0,0 +1,10 @@
+IEH3err (message, a, b, c, d, e)
+char message[];
+       {
+       extern int cgoof;
+       printf("ERROR ");
+       printf(message, a, b, c, d, e);
+       cputc('\n');
+       cexit(cgoof);
+       }
+cgoof 127;
diff --git a/usr/source/iolib/cexit.c b/usr/source/iolib/cexit.c
new file mode 100644 (file)
index 0000000..dc55f19
--- /dev/null
@@ -0,0 +1,8 @@
+cexit (rcode)
+{
+/* closes all files and exits */
+int i;
+for (i = 0; i < 10; i++)
+       cclose(i);
+exit(rcode);   /* rcode courtesy of sny */
+}
diff --git a/usr/source/iolib/cflush.c b/usr/source/iolib/cflush.c
new file mode 100644 (file)
index 0000000..8c99a10
--- /dev/null
@@ -0,0 +1,19 @@
+# include "ciodec.c"
+cflush (fn)
+int fn;
+{
+struct fileps *fp;
+if (nargs() != 1)
+       IEHzap("cflush");
+if (fn < 0 || fn >15) return;
+fp = &IEH3fpts[fn];
+if (fp->nchars > 0 && fp->wrflag >= 2)
+       {
+       write (fn, fp->buff,fp->nchars);
+       fp->bptr = fp->buff;
+       fp -> nchars = fp->eoferr = 0;
+       }
+if (fp->wrflag == 1)
+       seek(fn, -(fp->nchars),1);
+return;
+}
diff --git a/usr/source/iolib/cfree.c b/usr/source/iolib/cfree.c
new file mode 100644 (file)
index 0000000..db08eb2
--- /dev/null
@@ -0,0 +1,4 @@
+cfree (ptr)
+{
+free(ptr);
+}
diff --git a/usr/source/iolib/cgetc.c b/usr/source/iolib/cgetc.c
new file mode 100644 (file)
index 0000000..19b869a
--- /dev/null
@@ -0,0 +1,31 @@
+# include "ciodec.c"
+char cgetc(fn)
+{
+struct fileps *fp;
+if (nargs() != 1)
+       IEHzap("cgetc  ");
+if (fn <0 || fn >15)
+       IEH3err("cgetc: %d illegal file number",fn);
+fp = &IEH3fpts[fn];
+if (fp == 0 || fp->wrflag > 1)
+       IEH3err("cgetc: %d not open to read",fn);
+if (fp->wrflag == 0)
+       /* file not opened with fopen: try making a buffer */
+       IEH3mbuf (fn, 0);
+if (fp->eoferr > 1)
+       return ('\0');
+if (fp->nchars == 0)
+        switch (fp->nchars = read(fn, fp->bptr=fp->buff, fp->bsize))
+               {
+               case -1: /* error */
+                       if (fp->eoferr == 0)
+                               IEH3err("cgetc: error on %d",fn);
+                       fp->eoferr = 3;
+                       return ('\0');
+               case 0: fp->eoferr = 2;
+                       return ('\0');
+               }
+fp->nchars--;
+return (*(fp->bptr++));
+}
+cin 0;
diff --git a/usr/source/iolib/ciodec.c b/usr/source/iolib/ciodec.c
new file mode 100644 (file)
index 0000000..e94cf0a
--- /dev/null
@@ -0,0 +1,7 @@
+struct fileps
+       {
+       char *buff, *bptr;
+       int nchars, bsize;
+       char eoferr, wrflag;
+       };
+extern struct fileps IEH3fpts[10];
diff --git a/usr/source/iolib/clenf.c b/usr/source/iolib/clenf.c
new file mode 100644 (file)
index 0000000..cbc2e11
--- /dev/null
@@ -0,0 +1,6 @@
+_clenf (s) /* counts length of string */
+char *s;
+{
+int n;
+for (n=0; *s++ != '\0'; n++);
+return (n);}
diff --git a/usr/source/iolib/copen.c b/usr/source/iolib/copen.c
new file mode 100644 (file)
index 0000000..f797b0d
--- /dev/null
@@ -0,0 +1,23 @@
+copen (filename, type)
+char *filename;
+int type;
+{
+int fn;
+switch (type)
+       {
+       case 'r': type = 0;
+       case 0:   fn = open(filename,0);
+               break;
+       case 'a': type = 2;
+       case 2: if ((fn=open(filename,1))>=0)
+                       break;  /* courtesy of sny */
+       /* append defaults to write if file missing */
+       case 'w': type = 1;
+       case 1: fn = creat(filename,0666);
+               break;
+       default: IEH3err("copen: bad file %s",filename);
+       }
+if (fn >= 0)
+       IEH3mbuf(fn,type);
+return(fn);
+}
diff --git a/usr/source/iolib/cputc.c b/usr/source/iolib/cputc.c
new file mode 100644 (file)
index 0000000..38bd2fd
--- /dev/null
@@ -0,0 +1,30 @@
+# include "ciodec.c"
+char cputc(ch, fn)
+{
+struct fileps *fp;
+if (nargs() != 2)
+  IEHzap("cputc  ");
+if (fn<0 || fn>20) return(ch);
+fp = &IEH3fpts[fn];
+if (fp->wrflag == 1)
+       IEH3err("cputc: %d not open",fn);
+if (fp->wrflag == 0)
+       IEH3mbuf(fn,1);
+*(fp->bptr++) = ch;
+if (++(fp->nchars) < fp->bsize)
+       return(ch);
+if (fp->wrflag == 3) /* append, seek to end */
+       {
+       seek(fn, 0, 2);
+       fp->wrflag = 2; /* now just write from here on */
+       }
+if ( write(fn, fp->bptr=fp->buff, fp->nchars) < 0)
+       {
+       if (fp->eoferr == 0)
+               IEH3err("cputc: writing %d",fn);
+       fp->eoferr = 3;
+       }
+fp->nchars = 0;
+return (ch);
+}
+cout 1;
diff --git a/usr/source/iolib/cwrd.c b/usr/source/iolib/cwrd.c
new file mode 100644 (file)
index 0000000..d029164
--- /dev/null
@@ -0,0 +1,18 @@
+cwrite (buff, buffp1, len, fn)
+char *buff, *buffp1; int len, fn;
+{
+int unit, nwr;
+unit = buffp1-buff;
+len =* unit;
+nwr = write (fn, buff, len);
+return (nwr < 0 ? -1 : nwr/unit);
+}
+cread (buff, buffp1, len, fn)
+char *buff, *buffp1; int len, fn;
+{
+int unit, nrd;
+unit = buffp1 - buff;
+len =* unit;
+nrd = read(fn, buff, len);
+return (nrd < 0 ? -1 : nrd/unit);
+}
diff --git a/usr/source/iolib/dummy.s b/usr/source/iolib/dummy.s
new file mode 100644 (file)
index 0000000..6b89860
--- /dev/null
@@ -0,0 +1,4 @@
+.globl ndigit
+.globl _ieh305i
+ndigit:        .=.+2
+_ieh305i:      .=.+2
diff --git a/usr/source/iolib/ftoa.c b/usr/source/iolib/ftoa.c
new file mode 100644 (file)
index 0000000..d71c51e
--- /dev/null
@@ -0,0 +1,84 @@
+ftoa (x, str, prec, format)
+float x;
+char *str;
+{
+/* converts a floating point number to an ascii string */
+/* x is stored into str, which should be at least 30 chars long */
+int ie, i, k, ndig, fstyle;
+double y;
+if (nargs() != 7)
+  IEHzap("ftoa  ");
+ndig = ( prec<=0) ? 7 : (prec > 22 ? 23 : prec+1);
+if  (format == 'f' || format == 'F')
+ fstyle = 1;
+else
+ fstyle = 0;
+/* print in e format unless last arg is 'f' */
+ie = 0;
+/* if x negative, write minus and reverse */
+if ( x < 0)
+  {
+  *str++ = '-';
+  x = -x;
+  }
+
+/* put x in range 1 <= x < 10 */
+if (x > 0.0) while (x < 1.0)
+  {
+  x =* 10.0;
+  ie--;
+  }
+while (x >= 10.0)
+  {
+  x = x/10.0;
+  ie++;
+  }
+
+/* in f format, number of digits is related to size */
+if (fstyle) ndig =+ ie;
+
+/* round. x is between 1 and 10 and ndig will be printed to
+   right of decimal point so rounding is ... */
+for (y = i = 1; i < ndig; i++)
+  y = y/10.;
+x =+ y/2.;
+if (x >= 10.0) {x = 1.0; ie++;} /* repair rounding disasters */
+/* now loop.  put out a digit (obtain by multiplying by
+  10, truncating, subtracting) until enough digits out */
+/* if fstyle, and leading zeros, they go out special */
+if (fstyle && ie<0)
+  {
+  *str++ = '0'; *str++ = '.';
+  if (ndig < 0) ie = ie-ndig; /* limit zeros if underflow */
+  for (i = -1; i > ie; i--)
+    *str++ = '0';
+  }
+for (i=0; i < ndig; i++)
+  {
+  k = x;
+  *str++ = k + '0';
+  if (i == (fstyle ? ie : 0)) /* where is decimal point */
+    *str++ = '.';
+  x =- (y=k);
+  x =* 10.0;
+  }
+
+/* now, in estyle,  put out exponent if not zero */
+if (!fstyle && ie != 0)
+  {
+  *str++ = 'E';
+  if (ie < 0)
+    {
+    ie = -ie;
+    *str++ = '-';
+    }
+  for (k=100; k > ie; k =/10);
+  for (; k > 0; k =/10)
+       {
+       *str++ = ie/k + '0';
+       ie = ie%k;
+       }
+  }
+*str = '\0';
+return;
+}
diff --git a/usr/source/iolib/getch.c b/usr/source/iolib/getch.c
new file mode 100644 (file)
index 0000000..f4e7071
--- /dev/null
@@ -0,0 +1,5 @@
+getchar()
+{
+extern cin;
+return (cgetc(cin));
+}
diff --git a/usr/source/iolib/gets.c b/usr/source/iolib/gets.c
new file mode 100644 (file)
index 0000000..bdeb80e
--- /dev/null
@@ -0,0 +1,13 @@
+gets (s)
+char *s;
+{ /* gets (s) - read a string with cgetc and store in s */
+char *p;
+extern int cin;
+if (nargs () == 2)
+       IEHzap("gets  ");
+p=s;
+while ((*s = cgetc(cin)) != '\n' && *s != '\0') s++;
+if (*p == '\0') return (0);
+*s = '\0';
+return (p);
+}
diff --git a/usr/source/iolib/getvec.c b/usr/source/iolib/getvec.c
new file mode 100644 (file)
index 0000000..2ee722d
--- /dev/null
@@ -0,0 +1,4 @@
+getvec (n)
+{
+return (alloc(n));
+}
diff --git a/usr/source/iolib/iehzap.c b/usr/source/iolib/iehzap.c
new file mode 100644 (file)
index 0000000..8aa8d20
--- /dev/null
@@ -0,0 +1,7 @@
+IEHzap(s)
+{
+write(2,s,6);
+write(2,": argument count wrong\n",23);
+write(2,"That's all, folks\n",18);
+exit();
+}
diff --git a/usr/source/iolib/makbuf.c b/usr/source/iolib/makbuf.c
new file mode 100644 (file)
index 0000000..3a233ff
--- /dev/null
@@ -0,0 +1,36 @@
+# include "ciodec.c"
+IEH3bsz 512;
+IEH3mbuf (fn, type)
+int fn, type;
+{
+struct fileps *fp;
+extern char *IEH3olbf[], *alloc();
+extern int IEHfbak[10];
+int bx[19], size, bloc;
+fp = &IEH3fpts[fn];
+fp->eoferr = fp->nchars = 0;
+fp->wrflag = type+1;
+/* decide whether to buffer or not */
+if (ttyn(fn) != 'x')
+       size = 1;
+else
+if (fstat(fn,bx) > 0 && bx[0] == 40 && type == 0)
+       size = 1;
+else
+       size = IEH3bsz;
+for (fp->buff = 0; size >10 && fp->buff == 0; size =/ 4)
+               if ((bloc = alloc(size+100)) != -1)
+                       {
+                       IEH3olbf[fn] = bloc;
+                       fp->buff = fp->bptr =bloc + 100;
+                       fp->bsize = size;
+                       break;
+                       }
+if (fp->buff == 0)
+       {
+       fp->buff = fp->bptr = &IEHfbak[fn];
+       fp->bsize = size>1 ? 2 : 1;
+       }
+}
+struct fileps IEH3fpts [10];
+int IEHfbak[10];
diff --git a/usr/source/iolib/maktab.c b/usr/source/iolib/maktab.c
new file mode 100644 (file)
index 0000000..8cc0f01
--- /dev/null
@@ -0,0 +1,17 @@
+char IEH3endm[128] {0};
+IEH3mtab (formatp)
+char **formatp;
+{
+/* make up special table of string ending characters */
+int i, normal;
+char ch;
+/* normally all characters end string except those listed */
+normal = 1;
+if (**formatp == '^')
+       {normal = 0; (*formatp)++;}
+for (i= 0; i < 128; i++)
+       IEH3endm[i] = normal;
+while ((ch = *((*formatp)++)) != ']')
+       IEH3endm[ch] = !IEH3endm[ch];
+
+}
diff --git a/usr/source/iolib/nexch.c b/usr/source/iolib/nexch.c
new file mode 100644 (file)
index 0000000..4c52bd4
--- /dev/null
@@ -0,0 +1,10 @@
+IEH3nxch ()
+/* returns next character which is not IEH3spce */
+{
+       char ch, cgetc();
+       extern int IEH3sfil;
+while ((ch = cgetc(IEH3sfil)) > 0 && IEH3spce(ch));
+if  (ch > 0)
+       return (ch);
+return (-1);
+}
diff --git a/usr/source/iolib/nodig.c b/usr/source/iolib/nodig.c
new file mode 100644 (file)
index 0000000..bb4b3ff
--- /dev/null
@@ -0,0 +1,13 @@
+IEH3nodg (ch)
+char ch;
+{
+if (IEH3digt(ch)) return (0);
+switch (ch)
+       {
+       case 'E':
+       case 'e':
+       case '.': case '+': case '-':
+               return (0);
+       }
+return (1);
+}
diff --git a/usr/source/iolib/printf.c b/usr/source/iolib/printf.c
new file mode 100644 (file)
index 0000000..af33a40
--- /dev/null
@@ -0,0 +1,130 @@
+char  *_ptrbf, *_ptrst, *__fmt;
+printf(a1,a2,a3,a4){
+auto char  c, *s,  adj, *ptr,*p, buf[30];
+extern cputc(),_putstr(), cout;
+auto int  *adx, x, n, m, width, prec,i, padchar, fd;
+double zz, *dblptr;
+char (*f)();
+_ptrbf = buf;
+
+fd=cout;
+adx = &a1;
+f = cputc;
+if (a1 == -1)
+  {
+  f = _putstr;
+  _ptrst = a2;
+  adx =+ 2;
+  }
+else if (a1 >= 0 && a1 <= 9)
+  fd = *adx++;
+__fmt = *adx++;
+
+
+while( c = *__fmt++ ){
+   if(c != '%') (*f)(c,fd);
+   else { x = *adx++;
+      if( *__fmt == '-' ){ adj = 'l';  __fmt++; }
+      else adj = 'r';
+   padchar = (*__fmt=='0') ? '0' : ' ';
+      width = __conv();
+      if( *__fmt == '.'){++__fmt; prec = __conv();}
+      else prec = 0;
+
+   s = 0;
+   switch ( c = *__fmt++ ) {
+     case 'D':
+     case 'd':
+       _prt1(x); break;
+     case 'o':
+     case 'O':
+         _prnt8(x); break;
+     case 'x':
+     case 'X':
+          _prntx(x); break;
+      case 'S':
+     case 's':    s=x;
+        break;
+     case 'C':
+     case 'c':   *_ptrbf++ = x&0777;
+         break;
+     case 'E':
+     case 'e':
+     case 'F':
+     case 'f':
+      dblptr = adx-1;
+      zz = *dblptr;
+      adx =+ 3;
+      ftoa (zz, buf, prec, c);
+      prec = 0;
+      s = buf;
+     break;
+     default:   (*f)(c,fd);
+         adx--;
+   }
+   if (s == 0)
+    {*_ptrbf = '\0'; s = buf;}
+   n = _clenf (s);
+   n = (prec<n && prec != 0) ? prec : n;
+   m = width-n;
+   if (adj == 'r') while (m-- > 0) (*f)(padchar,fd);
+   while (n--) (*f)(*s++,fd);
+   while (m-- > 0) (*f)(padchar,fd);
+   _ptrbf = buf;
+   }
+}
+if(a1 == -1) (*f)('\0',fd);
+}
+
+
+_prnt8 (n)
+{ /* print in octal */
+int p, k, sw;
+if (n==0) {*_ptrbf++ = '0'; return;}
+sw = 0;
+for (p=15; p >= 0; p =- 3)
+  if ((k = (n>>p)&07) || sw)
+   {
+    *_ptrbf++ = '0' + k;
+     sw = 1;
+     }
+}
+_prntx (n)
+{
+       int d,a;
+       if (a = n>>4)
+               _prntx ( a & 07777);
+       d = n&017;
+       *_ptrbf++ =  d > 9 ? 'A'+d-10 : '0' + d;
+}
+
+__conv()
+{
+auto c,n;
+n = 0;
+while( ((c = *__fmt++) >= '0') && (c<='9')) n = n*10+c-'0';
+__fmt--;
+return(n);
+}
+
+_putstr(chr,str){
+*_ptrst++ = chr;
+return; ieh305i(); /* force loading of dummy.s */
+}
+_prt1(n)
+{
+int digs[15], *dpt;
+dpt = digs;
+if (n >= 0)
+   n = -n;
+else
+   *_ptrbf++ = '-';
+for (; n != 0; n = n/10)
+ *dpt++ = n%10;
+if (dpt == digs)
+   *dpt++ = 0;
+while (dpt != digs)
+   { --dpt;
+   *_ptrbf++ =  '0' - *dpt;
+}
+}
diff --git a/usr/source/iolib/putch.c b/usr/source/iolib/putch.c
new file mode 100644 (file)
index 0000000..fd438fb
--- /dev/null
@@ -0,0 +1,6 @@
+putchar (c)
+char c;
+{
+extern cout;
+cputc(c,cout);
+}
diff --git a/usr/source/iolib/puts.c b/usr/source/iolib/puts.c
new file mode 100644 (file)
index 0000000..ac0f1b0
--- /dev/null
@@ -0,0 +1,13 @@
+puts(str)
+char *str;
+{
+auto char *p,c;
+auto int f;
+extern int cout;
+if (nargs() != 1)
+   IEHzap("puts  ");
+p = str;
+while( (c = *p++)!= '\0') cputc(c,cout);
+cputc('\n',cout);
+return(str);
+}
diff --git a/usr/source/iolib/relvec.c b/usr/source/iolib/relvec.c
new file mode 100644 (file)
index 0000000..4fedb5e
--- /dev/null
@@ -0,0 +1,4 @@
+relvec (ptr)
+{
+free(ptr);
+}
diff --git a/usr/source/iolib/revput.c b/usr/source/iolib/revput.c
new file mode 100644 (file)
index 0000000..b305464
--- /dev/null
@@ -0,0 +1,10 @@
+# define bufflen 100
+IEH3revp (c)
+char c;
+{
+/* reversed line IEH3outputter */
+extern char *IEH3outp, *IEH3outlim;
+*IEH3outp++ = c;
+if (IEH3outp > IEH3outlim+100)
+       IEH3err("unprint producing too many chars");
+}
diff --git a/usr/source/iolib/scan1.c b/usr/source/iolib/scan1.c
new file mode 100644 (file)
index 0000000..ba87e99
--- /dev/null
@@ -0,0 +1,149 @@
+scanf (p1, p2, p3, p4)
+       int p1, p2, p3, p4;
+{
+/* first arg can be a control string, a file id, or -1 */
+       int ptrs[10], j, ip, flp, k;
+       char *np;
+/*     extern int cin;*/
+extern (*_Igetc)(), (*_Iungc)(), cgetc(), ungetc(), _Igstr(), _Iungs();
+extern char *_Iinpt;
+ip = 0;
+if (p1 == -1)
+  {k = 1; _Iinpt = p2;}
+else if (p1 >= 0 && p1 < 10)
+  k = 0;
+else
+  k = -1;
+if (k <= 0)
+  {_Igetc = cgetc; _Iungc = ungetc;}
+else
+  {_Igetc = _Igstr; _Iungc = _Iungs;}
+j = 0;
+for (np = (&p2)[k]; *np; np++)
+    if (*np == '%' && *(np+1) != '%' && *(np+1) != '*')
+       ptrs[ip++] = (&p3)[(j++)+k];
+return (_Iscan ((k==0 ?  p1 : 0), (&p2)[k], ptrs));
+}
+
+_Iscan (fileid, format, listp)
+       char *format;
+       int *listp;
+{
+       char ch, _Inxch();
+       int nmatch;
+       extern int _Isfil;
+       _Isfil = fileid;
+nmatch = 0;
+while (1) switch (ch= *format++)
+       {
+       case '\0': return (nmatch);
+       case '%': switch (_Isfrm(&format, *listp++))
+                       {
+                       case 0: listp--; break;
+                       case -1: return (nmatch > 0 ? nmatch : -1);
+                       default: nmatch++;
+                       }
+       case ' ':
+       case '\n':
+       case '\t': break;
+       default: if (ch != _Inxch())
+                       return(nmatch);
+       }
+}
+
+int _Isfil 0;
+
+_Ichar (cptr)
+       char *cptr;
+{
+       char ch, _Inxch();
+
+if ((ch = _Inxch()) < 0)
+       return (-1);
+if (cptr == 0)
+       return (0);
+*cptr = ch;
+return (1);
+}
+
+_Iflot (fptr, length)
+       float *fptr;
+       int length;
+{
+       char temp[75];
+       int _Inodg();
+       float x;
+       double atof();
+
+if (_Isstr(temp, length, _Inodg) < 0)
+       return (-1);
+x = atof(temp);
+if (fptr == 0)
+       return (0);
+*fptr = x;
+return (1);
+}
+
+_Inodg (ch)
+char ch;
+{
+if (_Idigt(ch,10) >= 0) return (0);
+switch (ch)
+       {
+       case 'E':
+       case 'e':
+       case '.': case '+': case '-':
+               return (0);
+       }
+return (1);
+}
+
+_Isfrm (spec, pointer)
+       char **spec;
+       int pointer;
+{
+       int length, lflag, _Iestr(), _Ispnd();
+       char ch;
+length = lflag = 0;
+while (1) switch (ch = *((*spec)++))
+       {
+       case '*': pointer=0; break;
+       case '0': case '1': case '2': case '3': case '4':
+       case '5': case '6': case '7': case '8': case '9':
+               length = length*10 + ch - '0' ;
+               lflag++;
+               break;
+       case 'o': /* octal */
+               return(_Iint(pointer, lflag ? length : 100, 8));
+       case 'x': /* hex */
+               return(_Iint(pointer, lflag ? length : 100, 16));
+       case 'd': /* decimal */
+               return (_Iint(pointer, lflag ? length : 100, 10));
+       case 'c': /* character */
+               return (_Ichar(pointer));
+       case 's': /* string */
+               return (_Isstr(pointer, lflag ? length : 100, _Iestr));
+       case 'f':
+       case 'e': /* float */
+               return (_Iflot(pointer, lflag ? length : 100));
+       case 'l': /*  (long) double or int */
+               switch(*(*spec)++)
+                       {
+                       case 'f': case 'F':
+                       case 'e': case 'E':
+                               return (_Ilong (pointer, lflag ? length : 100));
+                       default: printf(2, "long not yet implemented\n");
+                               return(0);
+                       }
+       case '[': /* special strings */
+               _Imtab(spec);
+               return (_Isstr (pointer, lflag ? length : 100, _Ispnd));
+       case '%':
+               if (_Inxch() != '%')
+                       return (-1);
+               return(0);
+       case '\0':
+               _Ierr("scanf: bad format termination\n");
+       default: _Ierr ("scanf: format character %c", ch);
+       }
+}
diff --git a/usr/source/iolib/scan2.c b/usr/source/iolib/scan2.c
new file mode 100644 (file)
index 0000000..730722a
--- /dev/null
@@ -0,0 +1,111 @@
+_Iint (iptr, length, numbase)
+       int *iptr, length;
+{
+       int n, minus, numdig;
+       extern int _Isfil, (*_Iungc)(), (*_Igetc)();
+       int c, dval;
+
+n = minus = numdig = 0;
+switch ((c=_Inxch()))
+       {
+       case '-': minus = 1;
+       case '+': break;
+       default: (*_Iungc)(c,_Isfil);
+       }
+while ((dval=_Idigt(c=((*_Igetc)(_Isfil)), numbase ) ) >= 0 && numdig++ < length)
+       n = n*numbase + dval;
+(*_Iungc)(c,_Isfil);
+if (numdig == 0)
+       return (-1);
+if (iptr == 0)
+       return (0);
+*iptr = minus ? -n : n;
+return (1);
+}
+
+_Idigt (x, base)
+{
+switch (x)
+       {
+       case '0':
+       case '1':
+       case '2':
+       case '3':
+       case '4':
+       case '5':
+       case '6':
+       case '7':
+               return(x-'0');
+       case '8':
+       case '9':
+               if (base > 8)
+                       return(x - '0');
+       case 'a':
+       case 'b':
+       case 'c':
+       case 'd':
+       case 'e':
+       case 'f':
+               if (base >10)
+                       return(x - 'a' + 10);
+       case 'A':
+       case 'B':
+       case 'C':
+       case 'D':
+       case 'E':
+       case 'F':
+               if (base > 10)
+                       return(x-'A' + 10);
+       }
+return(-1);
+}
+
+_Ilong (dptr, length)
+       double *dptr;
+       int length;
+{
+       char temp[75];
+       int _Inodg();
+       double x;
+       double atof();
+
+if (_Isstr(temp, length, _Inodg) < 0)
+       return (-1);
+x = atof(temp);
+if (dptr == 0)
+       return (0);
+*dptr = x;
+return (1);
+}
+
+_Isstr (sptr, length, stopf)
+       char *sptr;
+       int length, (*stopf)();
+{
+       int ch, initlen, _Inxch();
+       extern int _Isfil, (*_Igetc)(), (*_Iungc)();
+
+initlen = length;
+if ((ch=_Inxch()) < 0)
+       return (-1);
+(*_Iungc)(ch,_Isfil);
+while (!((*stopf)(ch=(*_Igetc)(_Isfil))) && length-- > 0)
+       if (sptr != 0)
+               *(sptr++) = ch;
+if (ch >= 0)
+       (*_Iungc)(ch,_Isfil);
+if (length == initlen)
+       return (-1);
+if (sptr == 0)
+       return (0);
+*sptr = '\0';
+return (1);
+}
+
+_Iestr (c)
+char c;
+{
+if (_Ispce(c)) return (1);
+if (c == '\0') return (1);
+return (0);
+}
diff --git a/usr/source/iolib/scan3.c b/usr/source/iolib/scan3.c
new file mode 100644 (file)
index 0000000..4a6a11c
--- /dev/null
@@ -0,0 +1,69 @@
+_Ierr (message, a, b, c, d, e)
+char message[];
+       {
+       extern int cgoof;
+       printf("ERROR ");
+       printf(message, a, b, c, d, e);
+       cputc('\n');
+       cexit(cgoof);
+       }
+char _Iendm[128] {0};
+_Imtab (formatp)
+char **formatp;
+{
+/* make up special table of string ending characters */
+int i, normal;
+char ch;
+/* normally all characters end string except those listed */
+normal = 1;
+if (**formatp == '^')
+       {normal = 0; (*formatp)++;}
+for (i= 0; i < 128; i++)
+       _Iendm[i] = normal;
+while ((ch = *((*formatp)++)) != ']')
+       _Iendm[ch] = !_Iendm[ch];
+
+}
+
+_Inxch ()
+/* returns next character which is not _Ispce */
+{
+       extern int _Isfil, (*_Igetc)();
+        int ch;
+while ((ch = (*_Igetc)(_Isfil)) > 0 && _Ispce(ch));
+if  (ch > 0)
+       return (ch);
+return (-1);
+}
+
+_Ispce (c)
+char c;
+{
+switch (c)
+       {
+       case ' ':
+       case '\n':
+       case '\t': return(1);
+       }
+return(0);
+}
+
+_Ispnd (ch)
+char ch;
+{
+return (_Iendm[ch] > 0);
+}
+
+char *_Iinpt;
+int (*_Igetc)(), (*_Iungc)();
+_Igstr ()
+{
+extern char *_Iinpt;
+return (*_Iinpt++);
+}
+
+_Iungs(ch)
+{
+extern char *_Iinpt;
+*--_Iinpt = ch;
+}
diff --git a/usr/source/iolib/system.c b/usr/source/iolib/system.c
new file mode 100644 (file)
index 0000000..2f5caa0
--- /dev/null
@@ -0,0 +1,8 @@
+system(str)
+ char *str;
+{
+int status;
+  if(fork() == 0)
+    execl("/bin/sh", "sh", "-c", str, 0);
+  wait(&status);
+  }
diff --git a/usr/source/iolib/tmpnam.c b/usr/source/iolib/tmpnam.c
new file mode 100644 (file)
index 0000000..f82f2be
--- /dev/null
@@ -0,0 +1,7 @@
+int IEH0958g 0;
+tmpnam(s)
+       char *s;
+{
+printf(-1,s,"pl%d%c",getpid(),'a'+IEH0958g++);
+return(s);
+}
diff --git a/usr/source/iolib/unget.c b/usr/source/iolib/unget.c
new file mode 100644 (file)
index 0000000..abab7cc
--- /dev/null
@@ -0,0 +1,15 @@
+# include "ciodec.c"
+ungetc (c, fn)
+{
+struct fileps *fp;
+if (nargs() != 2)
+   IEHzap("ungetc");
+/* push back onto input */
+fp = &IEH3fpts[fn];
+if (fp->wrflag == 0)
+       IEH3mbuf(fn,0);
+if (fp->bptr <= fp->buff - 100)
+       IEH3err("ungetc/unprintf: buffer full file %d",fn);
+*--fp->bptr = c;
+fp->nchars++;
+}
diff --git a/usr/source/iolib/unprnt.s b/usr/source/iolib/unprnt.s
new file mode 100644 (file)
index 0000000..a9f681f
--- /dev/null
@@ -0,0 +1,210 @@
+/ C library -- unprintf
+
+
+.globl _unprintf
+.globl ndigix
+
+.globl pfloat
+.globl pscien
+.globl _IEH3revp
+.globl _IEH3bcko
+
+_unprintf:
+       mov     r5,-(sp)
+       mov     sp,r5
+       mov     4(r5),formp             / format
+       mov     r5,r4
+       add     $6,r4                   / arglist
+       sub     $128.,sp                / buffer
+loop:
+       movb    *formp,r0
+       beq     1f
+       inc     formp
+       cmp     r0,$'%
+       beq     2f
+3:
+       mov     r4,-(sp)
+       mov     r0,-(sp)
+       jsr     pc,*$_IEH3revp
+       tst     (sp)+
+       mov     (sp)+,r4
+       br      loop
+1:
+       add     $128.,sp
+       mov     (sp)+,r5
+       jsr     pc,_IEH3bcko
+       rts     pc
+2:
+       clr     rjust
+       clr     ndigix
+       cmpb    *formp,$'-
+       bne     2f
+       inc     formp
+       inc     rjust
+2:
+       jsr     r3,gnum; width
+       clr     ndfnd
+       cmp     r0,$'.
+       bne     1f
+       jsr     r3,gnum; ndigix
+1:
+       mov     sp,r3
+       mov     $swtab,r1
+1:
+       mov     (r1)+,r2
+       beq     3b
+       cmp     r0,(r1)+
+       bne     1b
+       jmp     (r2)
+swtab:
+       decimal;        'd
+       octal;          'o
+       float;          'f
+       scien;          'e
+       charac;         'c
+       string;         's
+       logical;        'l
+       0;  0
+
+decimal:
+       mov     (r4)+,r1
+       bge     1f
+       neg     r1
+       movb    $'-,(r3)+
+       br      1f
+
+logical:
+       mov     (r4)+,r1
+1:
+       jsr     pc,1f
+       br      prbuf
+1:
+       clr     r0
+       div     $10.,r0
+       mov     r1,-(sp)
+       mov     r0,r1
+       beq     1f
+       jsr     pc,1b
+1:
+       mov     (sp)+,r0
+       add     $'0,r0
+       movb    r0,(r3)+
+       rts     pc
+
+charac:
+       movb    (r4)+,(r3)+
+       bne     1f
+       dec     r3
+1:
+       movb    (r4)+,(r3)+
+       br      prbuf
+
+string:
+       mov     ndigix,r1
+1:
+       mov     (r4)+,r2
+1:
+       movb    (r2)+,(r3)+
+       beq     prbuf
+       sob     r1,1b
+       br      prbuf
+
+octal:
+       mov     (r4)+,r1
+       jsr     pc,1f
+       br      prbuf
+1:
+       mov     r1,-(sp)
+       beq     1f
+       ash     $-3,r1
+       bic     $!17777,r1
+       jsr     pc,1b
+1:
+       mov     (sp)+,r0
+       bic     $!7,r0
+       add     $'0,r0
+       movb    r0,(r3)+
+       rts     pc
+
+float:
+       mov     ndfnd,r2
+       jsr     pc,pfloat
+       br      prbuf
+
+scien:
+       mov     ndfnd,r2
+       jsr     pc,pscien
+       br      prbuf
+
+prbuf:
+       cmp     r3,sp
+       blos    1f
+       tstb    -1(r3)
+       bne     1f
+       dec     r3
+       br      prbuf
+1:
+       mov     sp,r2
+       mov     r4,-(sp)
+       mov     $' ,-(sp)
+       mov     width,r1
+       sub     r3,r1
+       clrb    (r3)+
+       add     r2,r1
+       mov     r1,spaces
+       ble     1f
+       tst     rjust
+       bne     1f
+2:
+       jsr     pc,*$_IEH3revp
+       dec     spaces
+       bne     2b
+1:
+       mov     r2,bufp
+1:
+       movb    *bufp,(sp)
+       beq     1f
+       inc     bufp
+       jsr     pc,*$_IEH3revp
+       br      1b
+1:
+       tst     spaces
+       ble     1f
+       tst     rjust
+       beq     1f
+       mov     $' ,(sp)
+2:
+       jsr     pc,*$_IEH3revp
+       dec     spaces
+       bne     2b
+1:
+       tst     (sp)+
+       mov     (sp)+,r4
+       jmp     loop
+
+gnum:
+       clr     ndfnd
+       clr     r1
+1:
+       movb    *formp,r0
+       inc     formp
+       sub     $'0,r0
+       cmp     r0,$9.
+       bhi     1f
+       inc     ndfnd
+       mul     $10.,r1
+       add     r0,r1
+       br      1b
+1:
+       add     $'0,r0
+       mov     r1,*(r3)+
+       rts     r3
+
+       .bss
+ndigix:        .=.+2
+width: .=.+2
+formp: .=.+2
+rjust: .=.+2
+ndfnd: .=.+2
+bufp:  .=.+2
+spaces:        .=.+2
diff --git a/usr/source/iolib/wdleng.c b/usr/source/iolib/wdleng.c
new file mode 100644 (file)
index 0000000..4ce517b
--- /dev/null
@@ -0,0 +1,10 @@
+wdleng ()
+/* returns number of bits in a machine integer */
+/* written so kernighan can tell where he is running */
+{
+int k, leng;
+k = leng = 1;
+while (k =<< 1)
+       leng++;
+return (leng);
+}