home *** CD-ROM | disk | FTP | other *** search
- /*
-
-
- ┌──────────────────────────────────┐
- │ File Name: ██ list1.c ██ │
- └──────────────────────────────────┘
-
-
- The following C source is taken from the TELECON SYSTEMS
- standard I/O library. Permission is hereby granted for personal,
- non-comercial use of this code or any derivitives.
-
- Copyright (c) 1981, TELECON SYSTEMS. All rights reserved.
-
- COMMON portion of the STANDARD I/O LIBRARY.
-
- 27-jan-84
-
- Table of content:
-
- - printf()
- - fprintf()
- - sprintf()
-
- - strlen()
- */
-
- #define CONSOLE 0
- #define CODE -1
- #define DEVICE 0
- #define MEMORY 1
-
- #define linesize 133
- #define linemax 132
-
- static char *obptr,outbfr[40];
- static int field,fldskip,leftjust,longflag,padchar,precision,
- pushback = 0;
- char line[linesize],*lineptr,
- *stderr = CONSOLE,*stdin = CONSOLE,*stdout = CONSOLE;
- int ag1fl = DEVICE,echo = 1,enablout = 1,uconly = 0;
-
- typedef unsigned FILE;
- #define EOF -1
- #define NULL 0
- /* Macros. */
-
- #define islower(c) (c >= 'a' && c <= 'z')
- #define toupper(c) (c &= 0x5F)
- #define isdigit(c) (c >= '0' && c <= '9')
-
- /* printf - format the input stream and output it to the
- "standard" output. */
-
- printf(fmtstr,list)
- char *fmtstr;
- unsigned list;
- {
-
- outf(fmtstr,&list);
- }
-
- /* fprintf - format the input stream and output it to the
- specified file. */
-
- fprintf(file,fmtstr,list)
- char *file,*fmtstr;
- unsigned list;
- {
- FILE *temp;
-
- /* Save the current reference in 'stdout' and substitute
- the specified file reference. */
-
- temp = stdout;
- stdout = file;
-
- outf(fmtstr,&list);
-
- /* Now restore the original reference in 'stdout'. */
-
- stdout = temp;
- }
-
- /* sprintf - format the input stream and output it to the
- specified buffer. */
-
- sprintf(buffer,fmtstr,list)
- char *buffer,*fmtstr;
- unsigned list;
- {
- FILE *temp;
- int flag;
-
- /* Save the current reference in 'stdout' and substitute
- the buffer address. */
-
- temp = stdout;
- stdout = buffer;
- flag = ag1fl;
- ag1fl = MEMORY;
-
- outf(fmtstr,&list);
-
- /* Now restore the original reference in 'stdout'. */
-
- stdout = temp;
- ag1fl = flag;
- }
-
- /* outf - common formatting and output function for "printf", "fprintf"
- and "sprintf". */
-
- outf(fmtstr,list)
- char *fmtstr;
- unsigned *list;
- {
-
- int c,j,k;
-
- while(c = *fmtstr++)
- {
- if(c != '%') { putchar(c); continue; }
- obptr = outbfr;
- field = leftjust = longflag = precision = 0;
- padchar = ' ';
-
- while(c = *fmtstr++)
- {
- if(c == '-') leftjust = 1;
- else if(c == '0') padchar = '0';
- else if(isdigit(c))
- { --fmtstr; field = atoint(&fmtstr); }
- else if(c == '.') precision = atoint(&fmtstr);
- else if((c |= 0x20) == 'l') longflag = 1;
- else
- {
- switch(c)
- {
- case 'c':
- obstore(*list++);
- bfrout(outbfr);
- break;
-
- case 'd':
- if(longflag)
- outfixed((long *) *list++,10);
- else
- outfixed((long) *list++,10);
- bfrout(outbfr);
- break;
-
- case 'e':
- case 'g':
- outefloat((float *) *list++);
- bfrout(outbfr);
- break;
- case 'f':
- outffloat((float *) *list++);
- bfrout(outbfr);
- break;
-
- case 'o':
- if(longflag)
- outfixed((long *) *list++,8);
- else outfixed(0,*list++,8);
- bfrout(outbfr);
- break;
-
- case 's':
- bfrout(*list++);
- break;
-
- case 'x':
- if(longflag)
- outfixed((long *) *list++,16);
- else outfixed(0,*list++,16);
- bfrout(outbfr);
- break;
- default:
- putchar(c);
- }
- break;
- }
- }
- }
- }
-
- /* Subroutines used by outf() */
-
- outfixed(value,base)
- long value;
- int base;
- {
- long i;
-
- if(base == 10)
- {
- if(value < 0)
- {
- obstore('-');
- value = -value;
- }
- }
- switch(base)
- {
- case 8: i = value >> 3;
- break;
- case 10: if((i = value / 10) < 0) i = -i;
- break;
- case 16: i = value >> 4;
- break;
- }
- if(i) outfixed(i,base);
-
- switch(base)
- {
- case 8: value &= 7;
- break;
- case 10: if((value %= 10) < 0) value = -value;
- break;
- case 16: value &= 0xF;
- break;
- }
- if(value > 9) value += 7;
- obstore((int) value + '0');
- }
-
- /* 'e' format, floating point output conversion. */
-
- outefloat(fv)
- float fv;
- {
- int exponent,sign;
-
- exponent = sign = 0;
-
- if(fv < 0.0) { sign = 1; fv = -fv; }
-
- while(1)
- {
- if(fv < 1.0) fv *= 10.0, --exponent;
- else if(fv >= 10.0) fv /= 10.0, ++exponent;
- else break;
- }
- if(sign) fv = -fv;
- outffloat(fv);
- obstore('E');
- outfixed((long) exponent,10);
- }
-
- /* 'f' format, floating point output conversion. */
-
- outffloat(fv)
- float fv;
- {
- long fixed;
-
- if(!precision) precision = 6;
- if(fv < 0.0) { fv = -fv; obstore('-'); }
- outfixed(fixed = (long)fv,10);
- obstore('.');
-
- while(precision)
- {
- fv -= (float)fixed;
- fv *= 10.0;
- fixed = (long)fv;
- obstore((int)fixed + '0');
- --precision;
- }
- }
-
- /* Output buffer, store routine. */
-
- obstore(ascii)
- int ascii;
- {
- *obptr++ = ascii;
- }
-
- /* Buffer output routine. */
-
- bfrout(outbfr)
- char *outbfr;
- {
- int c,length;
-
- *obptr = NULL;
- length = strlen(outbfr);
-
- if(!precision) precision = length;
-
- length = field - precision;
- if(!leftjust) padout(length);
- while((c = *outbfr++) && precision--) putchar(c);
- if(leftjust) padout(length);
- }
-
- /* Output field, pad routine. */
-
- padout(length)
- int length;
- {
- if(length < 0) return;
- while(length--) putchar(padchar);
- }
-
- int strlen(str1)
- char str1[];
- {
- int j;
-
- j = 0;
- while(str1[j]) ++j;
- return j;
- }
-
- putchar(c)
- char c;
- {
-
- if(enablout)
- {
- if(c == '\n') putchar('\r');
- if(uconly)
- if(islower(c)) toupper(c);
- if(stdout)
- {
- if(ag1fl == MEMORY) *stdout++ = c;
- else if((putc(c,stdout)) == EOF)
- {
- fclose(stdout);
- stdout = NULL;
- }
- }
- else (putach(c));
- }
- return c;
- }
-