home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / kip / send.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-17  |  2.4 KB  |  142 lines

  1. /*
  2.  *  (c) 1986, Kinetics, Inc.
  3.  *  May be used but not sold without permission.
  4.  *
  5.  *  $Header: send.c,v 4.1 88/11/01 19:52:15 sw0l Exp $
  6.  */
  7.  
  8. /*
  9.  * Kernel/standalone sendf routines:
  10.  *    - implement a reasonablly full-bodied printf except output to
  11.  *      appletalk with the following capabilities:
  12.  *        %d %D %o %O %x %X %u %U %c %s
  13.  *    - the numeric output descriptors can also optionally have a fill
  14.  *      width and zero filling ala standard printf (i.e. %2x, %02x)
  15.  */
  16.  
  17. #include "gw.h"
  18. #include "fp/pbuf.h"
  19. #include "fp/cmdmacro.h"
  20.  
  21. char outbuf[256];
  22. short dlen;
  23. char *ocp;
  24. struct fp_atwrite the_pkt[5];
  25.  
  26. char    tohex[]    = "0123456789ABCDEF";
  27.  
  28. sendch(cc)
  29. int cc;
  30. {
  31.     switch (cc) {
  32.         case -1:        /* flush */
  33.             /* send a diagnostic/debug appletalk packet */
  34.             dlen = ocp - outbuf;    /* length of diagnostic msg */
  35.             the_pkt[3].fpw_length = dlen;
  36.             dlen += 1 + 2;    /* add length of "D" and dlen */
  37.             K_ATWRITE(the_pkt);
  38.             break;
  39.         
  40.         case 0:            /* initialize */
  41.             dlen = 0;
  42.             ocp = outbuf;
  43.             break;
  44.         
  45.         default:        /* any other char */
  46.             *ocp++ = cc;
  47.             break;
  48.     }
  49.     return;
  50. }
  51.  
  52. /*
  53.  * sendn:
  54.  *    - send an unsigned long in base "base", using an output width of
  55.  *      "width", and zero filling if "zfill" is '0'.
  56.  */
  57. sendn(n, base, width, zfill)
  58. register unsigned long n;
  59. register int base, width, zfill;
  60. {
  61.     register int dig;
  62.     register char c;
  63.     char buf[30];
  64.  
  65.     dig = 0;
  66.     if (n) {
  67.         while (n) {
  68.             buf[dig++] = tohex[n % base];
  69.             n /= base;
  70.         }
  71.     } else
  72.         buf[dig++] = '0';
  73.  
  74.     /* pad to width, then output result */
  75.  
  76.     while (dig < width) {
  77.         if (zfill == '0')
  78.             buf[dig++] = '0';
  79.         else
  80.             buf[dig++] = ' ';
  81.     }
  82.     while (dig) {
  83.         c = buf[--dig];
  84.         sendch(c);
  85.     }
  86. }
  87.  
  88. /*VARARGS*/
  89. sendf(fmt, x1)
  90. char *fmt;
  91. int x1;
  92. {
  93.     int *adx = &x1;
  94.     int c, base, width, zfill;
  95.     char *s;
  96.     int i;
  97.  
  98.     sendch(0);    /* init sendch */
  99.     for (;;) {
  100.         while ((c = *fmt++) != '%') {
  101.             if (c == '\0') {
  102.                 sendch(-1);    /* do fflush */
  103.                 return;
  104.             }
  105.             sendch(c);
  106.         }
  107.  
  108.         c = *fmt++;
  109.         width = 0;
  110.         zfill = c;
  111.         while ((c >= '0') && (c <= '9')) {
  112.             width = width*10  + (c - '0');
  113.             c = *fmt++;
  114.         }
  115.         switch (c) {
  116.           case 0:
  117.             sendch(-1);
  118.             return;
  119.           case 'd': case 'u': case 'D': case 'U':
  120.             sendn(*adx, 10, width, zfill);
  121.             break;
  122.           case 'o': case 'O':
  123.             sendn(*adx, 8, width, zfill);
  124.             break;
  125.           case 'x': case 'X':
  126.             sendn(*adx, 16, width, zfill);
  127.             break;
  128.           case 's':
  129.             s = (char *)*adx;
  130.             while (c = *s++) {
  131.                 sendch(c);
  132.             }
  133.             break;
  134.           case 'c':
  135.             sendch(*adx);
  136.             break;
  137.         }
  138.         adx++;
  139.     }
  140. }
  141.  
  142.