Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / ircd_snprintf.c
index 646dc160cd489ed19c48f92efa21538e91e19f90..00d07827e9c379d38b0e54249b51b7e3c8998721 100644 (file)
 #include <string.h>
 #include <time.h>
 
-/* for testing purposes */
-#ifndef SIZEOF_VOID_P
-# warning SIZEOF_VOID_P not defined yet!
-# define SIZEOF_VOID_P 4
-# define SIZEOF_LONG   4
-#endif
-
 /* Inhibit complaints when we use GCC extensions */
-#ifdef __GNUC__
+#if defined(__GNUC__) && defined(HAVE_LONG_LONG)
 # define EXTENSION __extension__
 #else
 # define EXTENSION
@@ -85,7 +78,7 @@ struct BufData {
   size_t       buf_size;       /* maximum size of buffer */
   size_t       buf_overflow;   /* how much buffer has been overflowed */
   size_t       buf_loc;        /* where we are in the buffer */
-  size_t       limit;          /* max # of chars to convert */
+  short                limit;          /* max # of chars to convert */
   size_t       overflow;       /* how much we overflowed the limit */
 };
 
@@ -1411,7 +1404,7 @@ static char *HEX[] = {
 };
 
 /* Add a character to the buffer */
-static __inline__ void
+static void
 addc(struct BufData *buf_p, int c)
 {
   int overflow = 0;
@@ -1432,7 +1425,7 @@ addc(struct BufData *buf_p, int c)
 }
 
 /* Add a string to the buffer */
-static __inline__ void
+static void
 adds(struct BufData *buf_p, int s_len, const char *s)
 {
   int overflow = 0;
@@ -1460,7 +1453,7 @@ adds(struct BufData *buf_p, int s_len, const char *s)
 }
 
 /* Add padding */
-static __inline__ void
+static void
 do_pad(struct BufData *buf_p, int padlen, char *pad)
 {
   /* do chunks of PAD_LENGTH first */
@@ -1472,7 +1465,7 @@ do_pad(struct BufData *buf_p, int padlen, char *pad)
 }
 
 /* Find string length up to maxlen */
-static __inline__ int
+static int
 my_strnlen(const char *str, int maxlen)
 {
   int len = 0;
@@ -1604,21 +1597,13 @@ doprintf(struct Client *dest, struct BufData *buf_p, const char *fmt,
        if (state <= OPT) {
          state = OPT;
          if (fld_s.flags & TYPE_LONG) /* We support 'll' */
-           fld_s.flags |= TYPE_QUAD;
+           fld_s.flags |= TYPE_QUAD | TYPE_LONGDOUBLE;
          else if (!(fld_s.flags & TYPE_MASK))
            fld_s.flags |= TYPE_LONG;
        }
        continue;
 
-      case 'q': /* it's a quad */
-       if (state <= OPT) {
-         state = OPT;
-         if (!(fld_s.flags & TYPE_MASK))
-           fld_s.flags |= TYPE_QUAD;
-       }
-       continue;
-
-      case 'L': /* it's a quad or long double */
+      case 'q':  case 'L': /* it's a quad or long double */
        if (state <= OPT) {
          state = OPT;
          if (!(fld_s.flags & TYPE_MASK))
@@ -1835,9 +1820,6 @@ doprintf(struct Client *dest, struct BufData *buf_p, const char *fmt,
       char intbuf[INTBUF_LEN], **table = 0, *tstr;
       int ibuf_loc = INTBUF_LEN, ilen, zlen = 0, plen = 0, elen = 0;
 
-      if (fld_s.prec < 0) /* default precision is 1 */
-       fld_s.prec = 1;
-
       if (fld_s.base == BASE_OCTAL) /* select string table to use */
        table = octal;
       else if (fld_s.base == BASE_DECIMAL)
@@ -1848,6 +1830,15 @@ doprintf(struct Client *dest, struct BufData *buf_p, const char *fmt,
          elen = 2; /* account for the length of 0x */
       }
 
+      if (fld_s.prec < 0) { /* default precision is 1 */
+       if ((fld_s.flags & (FLAG_MINUS | FLAG_ZERO)) == FLAG_ZERO &&
+           fld_s.width) {
+         fld_s.prec = fld_s.width - elen;
+         fld_s.width = 0;
+       } else
+         fld_s.prec = 1;
+      }
+
       /* If there's a sign flag, account for it */
       if (fld_s.flags & (FLAG_PLUS | FLAG_SPACE | INFO_NEGATIVE))
        elen++;
@@ -1936,7 +1927,7 @@ doprintf(struct Client *dest, struct BufData *buf_p, const char *fmt,
     } else if ((fld_s.flags & CONV_MASK) == CONV_STRING ||
               fld_s.value.v_ptr == 0) { /* spaces or null pointers */
       int slen, plen;
-      char *str = fld_s.value.v_ptr;
+      char *str = (char*) fld_s.value.v_ptr;
 
       if (!str) /* NULL pointers print "(null)" */
        str = "(null)";
@@ -1953,7 +1944,7 @@ doprintf(struct Client *dest, struct BufData *buf_p, const char *fmt,
        do_pad(buf_p, plen, spaces); /* post-padding */
     } else if ((fld_s.flags & CONV_MASK) == CONV_VARARGS) {
       struct BufData buf_s = BUFDATA_INIT;
-      struct VarData *vdata = fld_s.value.v_ptr;
+      struct VarData *vdata = (struct VarData*) fld_s.value.v_ptr;
       int plen, tlen;
 
       buf_s.buf = buf_p->buf + buf_p->buf_loc;
@@ -2001,12 +1992,13 @@ doprintf(struct Client *dest, struct BufData *buf_p, const char *fmt,
       vdata->vd_chars = buf_s.buf_loc; /* return relevant data */
       vdata->vd_overflow = SNP_MAX(buf_s.buf_overflow, buf_s.overflow);
     } else if ((fld_s.flags & CONV_MASK) == CONV_CLIENT) {
-      struct Client *cptr = fld_s.value.v_ptr;
+      struct Client *cptr = (struct Client*) fld_s.value.v_ptr;
       char *str1 = 0, *str2 = 0;
       int slen1 = 0, slen2 = 0, plen = 0;
 
-      if (IsServer(dest)) {
-       if (IsServer(cptr))
+      /* &me is used if it's not a definite server */
+      if (dest && (IsServer(dest) || IsMe(dest))) {
+       if (IsServer(cptr) || IsMe(cptr))
          str1 = cptr->yxx;
        else {
          str1 = cptr->user->server->yxx;