Added silc_strncat.
[silc.git] / lib / silcutil / silcstrutil.c
index 904873062130a79133c2350b31410e2fce571c96..b685f0557d0b10613f984d09ffcb6a928030fccb 100644 (file)
@@ -212,8 +212,9 @@ SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
          iconv_close(icd);
          return utf8_size;
        }
-       iconv_close(icd);
       }
+      if (icd != (iconv_t)-1)
+       iconv_close(icd);
     }
 #endif
 
@@ -355,8 +356,9 @@ SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
          iconv_close(icd);
          return bin_size;
        }
-       iconv_close(icd);
       }
+      if (icd != (iconv_t)-1)
+       iconv_close(icd);
     }
 #endif
 
@@ -572,3 +574,25 @@ silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
 
   return TRUE;
 }
+
+/* Concatenates the `src' into `dest'.  If `src_len' is more than the
+   size of the `dest' (minus NULL at the end) the `src' will be
+   truncated to fit. */
+
+char *silc_strncat(char *dest, SilcUInt32 dest_size,
+                  const char *src, SilcUInt32 src_len)
+{
+  int len;
+
+  dest[dest_size - 1] = '\0';
+
+  len = dest_size - 1 - strlen(dest);
+  if (len < src_len) {
+    if (len > 0)
+      strncat(dest, src, len);
+  } else {
+    strncat(dest, src, src_len);
+  }
+
+  return dest;
+}