home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / UTILS / S / SMC21LIB.LZH / FPRINTF.C < prev    next >
Text File  |  2000-06-30  |  2KB  |  75 lines

  1.  
  2. #define NOCCARGC
  3. /*
  4. ** Yes, it's right.  These function do not call ones which need arg counts.
  5. */
  6. #include stdio.h
  7. /*
  8. ** fprintf(fd, ctlstring, arg, arg, ....) - Formatted print.
  9. ** Operates as described by Kernighan and Ritchie.
  10. ** b, c, d, o, s, u, and x specifications are supported.
  11. ** Note: b (binary) is a non-standard extension.
  12. */
  13. fprintf(argc) int argc; {
  14.   int *nxtarg;
  15.   nxtarg = CCARGC() + &argc;
  16.   return(_print(*(--nxtarg), --nxtarg));
  17.   }
  18.  
  19. /*
  20. ** printf(ctlstring, arg, arg, ...) - Formatted print.
  21. ** See note to fprintf.
  22. */
  23. printf(argc) int argc; {
  24.   return(_print(stdout, CCARGC() + &argc - 1));
  25.   }
  26.  
  27. /*
  28. ** _print(fd, ctlstring, arg, arg, ...)
  29. ** Called by fprintf() and printf().
  30. */
  31. _print(fd, nxtarg) int fd, *nxtarg; {
  32.   int arg, left, pad, cc, len, maxchr, width;
  33.   char *ctl, *sptr, str[17];
  34.   cc = 0;
  35.   ctl = *nxtarg--;
  36.   while(*ctl) {
  37.     if(*ctl!='%') {fputc(*ctl++, fd); ++cc; continue;}
  38.     else ++ctl;
  39.     if(*ctl=='%') {fputc(*ctl++, fd); ++cc; continue;}
  40.     if(*ctl=='-') {left = 1; ++ctl;} else left = 0;
  41.     if(*ctl=='0') pad = '0'; else pad = ' ';
  42.     if(isdigit(*ctl)) {
  43.       width = atoi(ctl++);
  44.       while(isdigit(*ctl)) ++ctl;
  45.       }
  46.     else width = 0;
  47.     if(*ctl=='.') {
  48.       maxchr = atoi(++ctl);
  49.       while(isdigit(*ctl)) ++ctl;
  50.       }
  51.     else maxchr = 0;
  52.     arg = *nxtarg--;
  53.     sptr = str;
  54.     switch(*ctl++) {
  55.       case 'c': str[0] = arg; str[1] = NULL; break;
  56.       case 's': sptr = arg; break;
  57.       case 'd': itoa(arg,str); break;
  58.       case 'b': itoab(arg,str,2); break;
  59.       case 'o': itoab(arg,str,8); break;
  60.       case 'u': itoab(arg,str,10); break;
  61.       case 'x': itoab(arg,str,16); break;
  62.       default: return (cc);
  63.       }
  64.     len = strlen(sptr);
  65.     if(maxchr && maxchr<len) len = maxchr;
  66.     if(width>len) width = width - len; else width = 0;
  67.     if(!left) while(width--) {fputc(pad, fd); ++cc;}
  68.     while(len--) {fputc(*sptr++,fd); ++cc;}
  69.     if(left) while(width--) {fputc(pad,fd); ++cc;}
  70.     }
  71.   return(cc);
  72.   }
  73.  
  74.  
  75.