home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / SMALL_C / FPRINTF.C < prev    next >
Text File  |  1987-10-04  |  2KB  |  76 lines

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