* safestrncpy counts the copied string anyway, and nobody needs the returned pointer
[citadel.git] / libcitadel / lib / tools.c
index 17981796e041b619c4969d33539668e74bac632d..c056fe94d8ea4a4b1ecf50c81dfac15a77c79ddc 100644 (file)
@@ -73,8 +73,14 @@ const byte dtable[256] = {
        128, 128, 0
 };
 
-
-char *safestrncpy(char *dest, const char *src, size_t n)
+/**
+ * \brief copy a string into a buffer of a known size. abort if we exceed the limits
+ * \param dest the targetbuffer
+ * \param src the source string
+ * \param n the size od dest
+ * \returns the number of characters copied if dest is big enough, -n if not.
+ */
+int safestrncpy(char *dest, const char *src, size_t n)
 {
        int i = 0;
 
@@ -85,11 +91,11 @@ char *safestrncpy(char *dest, const char *src, size_t n)
 
        do {
                dest[i] = src[i];
-               if (dest[i] == 0) return(dest);
+               if (dest[i] == 0) return i;
                ++i;
        } while (i<n);
        dest[n - 1] = 0;
-       return dest;
+       return -i;
 }