home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / misc / smc203.ark / PRINTF.C < prev    next >
Encoding:
Text File  |  1983-07-28  |  1.5 KB  |  41 lines

  1.  
  2. /*
  3. ** printf(controlstring, arg, arg, ...) -- formatted print
  4. **        operates as described by Kernighan & Ritchie
  5. **        only d, x, c, s, and u specs are supported.
  6. */
  7. printf(argc) int argc; {
  8.   int i, width, prec, preclen, len, *nxtarg;
  9.   char *ctl, *cx, c, right, str[7], *sptr, pad;
  10.   i = CCARGC();   /* fetch arg count from A reg first */
  11.   nxtarg = &argc + i - 1;
  12.   ctl = *nxtarg;
  13.   while(c = *ctl++) {
  14.     if(c!='%') {cout(c, stdout); continue;}
  15.     if(*ctl=='%') {cout(*ctl++, stdout); continue;}
  16.     cx=ctl;
  17.     if(*cx=='-') {right=0; ++cx;} else right=1;
  18.     if(*cx=='0') {pad='0'; ++cx;} else pad=' ';
  19.     if((i=utoi(cx, &width)) >= 0) cx=cx+i; else continue;
  20.     if(*cx=='.') {
  21.       if((preclen=utoi(++cx, &prec)) >= 0) cx=cx+preclen;
  22.       else continue;
  23.       }
  24.     else preclen=0;
  25.     sptr=str; c = *cx++; i = *(--nxtarg);
  26.     if(c=='d') itod(i, str, 7);
  27.     else if(c=='x') itox(i, str, 7);
  28.     else if(c=='c') {str[0]=i; str[1]=NULL;}
  29.     else if(c=='s') sptr=i;
  30.     else if(c=='u') itou(i, str, 7);
  31.     else continue;
  32.     ctl=cx; /* accept conversion spec */
  33.     if(c!='s') while(*sptr==' ') ++sptr;
  34.     len=-1; while(sptr[++len]); /* get length */
  35.     if((c=='s')&(len>prec)&(preclen>0)) len=prec;
  36.     if(right) while(((width--)-len)>0) cout(pad, stdout);
  37.     while(len) {cout(*sptr++, stdout); --len; --width;}
  38.     while(((width--)-len)>0) cout(pad, stdout);
  39.     }
  40.   }
  41.