]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
bmstrcasestr() now returns NULL if either of its supplied
[citadel.git] / libcitadel / lib / tools.c
index 17981796e041b619c4969d33539668e74bac632d..dd630420fa4b79b57ded590ddcab02414e5192dd 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;
 }
 
 
@@ -766,6 +772,9 @@ char *bmstrcasestr(char *text, char *pattern) {
        size_t textlen;
        size_t patlen;
 
+       if (!text) return(NULL);
+       if (!pattern) return(NULL);
+
        textlen = strlen (text);
        patlen = strlen (pattern);
 
@@ -889,10 +898,10 @@ int pattern2(char *search, char *patn)
 }
 
 
-/**
- * \brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
- * \param buf the string to modify
- * \param len length of the string. 
+/*
+ * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
+ * buf - the string to modify
+ * len - length of the string. 
  */
 void stripltlen(char *buf, int *len)
 {
@@ -910,3 +919,21 @@ void stripltlen(char *buf, int *len)
        }
 }
 
+
+/*
+ * Convert all whitespace characters in a supplied string to underscores
+ */
+void convert_spaces_to_underscores(char *str)
+{
+       int len;
+       int i;
+
+       if (!str) return;
+
+       len = strlen(str);
+       for (i=0; i<len; ++i) {
+               if (isspace(str[i])) {
+                       str[i] = '_';
+               }
+       }
+}