home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 4 Drivers / 04-Drivers.zip / scsiopt2.zip / printk.c < prev    next >
C/C++ Source or Header  |  1997-12-06  |  5KB  |  305 lines

  1. /*
  2.  * $Source: e:/source/driver/perf/RCS/printk.c,v $
  3.  * $Revision: 1.2 $
  4.  * $Date: 1997/12/06 00:43:41 $
  5.  * $Author: vitus $
  6.  *
  7.  * Simple sprintf-like function
  8.  *
  9.  * $Log: printk.c,v $
  10.  * Revision 1.2  1997/12/06 00:43:41  vitus
  11.  * - enabled support for WatCom C
  12.  *
  13.  * Revision 1.1  1997/06/04 23:43:45  vitus
  14.  * Initial revision
  15.  *
  16.  */
  17. #pragma off (unreferenced)
  18. static char vcid[]="$Id: printk.c,v 1.2 1997/12/06 00:43:41 vitus Exp $";
  19. #pragma on (unreferenced)
  20.  
  21.  
  22. #define PRIVATE static
  23. #define PUBLIC
  24.  
  25.  
  26. #define FNULL     ((void _far *)0)
  27.  
  28. typedef char _far *va_list;
  29. #define va_start(ap,v) ap = (va_list)&v + sizeof(v)
  30. #define va_arg(ap,t) ((t _far *)(ap += sizeof(t)))[-1]
  31. #define va_end(ap) ap = FNULL
  32.  
  33.  
  34. /* #define NO_LONGD    */          /* Longflags ignorieren */
  35.  
  36. /* three compile time options:
  37.  *    NO_LONGD    %d and %ld/%D are equal
  38.  */
  39.  
  40. PRIVATE char    nullStr[]=    "(null)";
  41.  
  42. #define MAXDIG        11    /* 32 bits in radix 8 */
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. PRIVATE char _far *
  50. _itoa(char _far *p,unsigned num,int radix)
  51. {
  52.     int  i;
  53.     char _far *q;
  54.  
  55.     q = p + MAXDIG;
  56.     do
  57.     {
  58.     i = (int) (num % radix);
  59.     i += '0';
  60.     if (i > '9') i += 'A' - '0' - 10;
  61.     *(--q) = (char)i;
  62.     }
  63.     while( num /= radix );
  64.     i = p + MAXDIG - q;
  65.     do
  66.     *p++ = *q++;
  67.     while( --i );
  68.     return(p);
  69. }
  70.  
  71.  
  72.  
  73.  
  74. #ifndef NO_LONGD
  75. PRIVATE char _far *
  76. ltoa(char _far *p,unsigned long num,int radix)
  77. {
  78.     int       i;
  79.     char _far *q;
  80.  
  81.     q = p + MAXDIG;
  82.     do
  83.     {
  84.     i = (int)(num % radix);
  85.     i += '0';
  86.     if( i > '9' )
  87.         i += 'A' - '0' - 10;
  88.     *(--q) = (char)i;
  89.     }
  90.     while( num /= radix );
  91.     i = p + MAXDIG - q;
  92.     do
  93.     *p++ = *q++;
  94.     while( --i );
  95.     return(p);
  96. }
  97. #endif
  98.  
  99.  
  100.  
  101.  
  102. #define GETARG(typ)    va_arg(args, typ)
  103.  
  104. PUBLIC void
  105. sprintk(char _far *outs,const char _far *fmt,...)
  106. {
  107.     char buf[MAXDIG + 1];            /* +1 for sign */
  108.     va_list   args;
  109.     char _far *p;
  110.     char _far *s;
  111.     int   c, i;
  112.     short width;
  113.     short ndigit;
  114.     int   ndfnd, ljust, zfill;
  115. #ifndef NO_LONGD
  116.     int   lflag;
  117.     long  l;
  118. #endif
  119.  
  120.     va_start( args, fmt );
  121.     for (;;)
  122.     {
  123.     c = *(fmt++);
  124.     if( c == 0 )                /* Ende des Strings? */
  125.     {
  126.         va_end( args );
  127.         *outs = '\0';
  128.         return;
  129.     }
  130.     if (c != '%')                /* Kein Formatanfang? */
  131.     {
  132.         *outs++ = (char)c;            /* ausgeben */
  133.         continue;                /* und nächstes Zeichen */
  134.     }
  135.     p = buf;
  136.     s = buf;
  137.     ljust = 0;
  138.     if (*fmt == '-')            /* Format beginnt '-' */
  139.     {
  140.         fmt++;
  141.         ljust++;                /* left justified */
  142.     }
  143.     zfill = ' ';                /* Füllzeichen SPC */
  144.     if (*fmt == '0')            /* Format dann '0' */
  145.     {
  146.         fmt++;
  147.         zfill = '0';            /* Füllzeichen '0' */
  148.     }
  149.     for (width = 0;;)
  150.     {
  151.         c = *(fmt++);
  152.         if( c >= '0'  &&  c <= '9' )
  153.         c -= '0';
  154.         else
  155.         {
  156.         if( c == '*' )
  157.             c = GETARG(int);
  158.         else
  159.             break;
  160.         }
  161.         width *= 10;
  162.         width += c;
  163.     }
  164.     ndfnd = 0;
  165.     ndigit = 0;
  166.     if( c == '.' )
  167.     {
  168.         for (;;)
  169.         {
  170.         c = *(fmt++);
  171.         if( c >= '0'  &&  c <= '9' )
  172.             c -= '0';
  173.         else
  174.             if( c == '*' )
  175.             c = GETARG(int);
  176.             else
  177.             break;
  178.         ndigit *= 10;
  179.         ndigit += c;
  180.         ndfnd++;
  181.         }
  182.     }
  183. #ifndef NO_LONGD
  184.     lflag = 0;
  185. #endif
  186.     if( c == 'l' || c == 'L' )
  187.     {
  188. #ifndef NO_LONGD
  189.         lflag++;
  190. #endif
  191.         if( *fmt )
  192.         c = *(fmt++);
  193.     }
  194.     switch( c )
  195.     {
  196.       case 'X':
  197. #ifndef NO_LONGD
  198.         lflag++;
  199. #endif
  200.       case 'x':
  201.         c = 16;
  202.         goto oxu;
  203.  
  204.       case 'U':
  205. #ifndef NO_LONGD
  206.         lflag++;
  207. #endif
  208.       case 'u':
  209.         c = 10;
  210.         goto oxu;
  211.  
  212.       case 'O':
  213. #ifndef NO_LONGD
  214.         lflag++;
  215. #endif
  216.       case 'o':
  217.         c = 8;
  218.       oxu:
  219. #ifndef NO_LONGD
  220.         if( lflag )
  221.         {
  222.         p = ltoa(p, (unsigned long)GETARG(long), c);
  223.         break;
  224.         }
  225. #endif
  226.         p = _itoa(p, (unsigned int)GETARG(int), c);
  227.         break;
  228.  
  229.       case 'D':
  230. #ifndef NO_LONGD
  231.         lflag++;
  232. #endif
  233.       case 'd':
  234. #ifndef NO_LONGD
  235.         if( lflag )
  236.         {
  237.         if( (l=GETARG(long)) < 0 )
  238.         {
  239.             *p++ = '-';
  240.             l = -l;
  241.         }
  242.         p = ltoa(p, (unsigned long)l, 10);
  243.         break;
  244.         }
  245. #endif
  246.         if( (i=GETARG(int)) < 0 )
  247.         {
  248.         *p++ = '-';
  249.         i = -i;
  250.         }
  251.         p = _itoa(p, (unsigned int)i, 10);
  252.         break;
  253.  
  254.       case 'e':
  255.       case 'f':
  256.       case 'g':
  257.         zfill = ' ';
  258.         *p++ = '?';
  259.         break;
  260.  
  261.       case 'c':
  262.         zfill = ' ';
  263.         *p++ = (char)GETARG(int);
  264.         break;
  265.  
  266.       case 's':
  267.         zfill = ' ';
  268.         if ((s = GETARG(char _far *)) == 0) s = nullStr;
  269.         if (ndigit == 0) ndigit = 32767;
  270.         for( p=s; *p && --ndigit >= 0; p++ )
  271.         ;
  272.         break;
  273.  
  274.       default:
  275.         *p++ = (char)c;
  276.         break;
  277.     }
  278.     i = p - s;
  279.     if( (width -= i) < 0 )
  280.         width = 0;
  281.     if( ljust == 0 )
  282.         width = -width;
  283.     if( width < 0 )
  284.     {
  285.         if( *s == '-' && zfill == '0' )
  286.         {
  287.         *outs++ = *s++;
  288.         i--;
  289.         }
  290.         do
  291.         *outs++ = (char)zfill;
  292.         while( ++width != 0 );
  293.     }
  294.     while( --i >= 0 )
  295.         *outs++ = *s++;
  296.     while( width )
  297.     {
  298.         *outs++ = (char)zfill;
  299.         width--;
  300.     }
  301.     }
  302.  
  303.     return;
  304. }
  305.