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;
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;
}
int i = 0;
while (i++, i < 100) {
- snprintf(name, len, "/tmp/ctdl.%4lx.%04x",
+ snprintf(name, len, "/tmp/ctdl.%04lx.%04x",
(long)getpid(),
rand()
);
}
-/**
- * \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)
{
}
}
+
+/*
+ * 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] = '_';
+ }
+ }
+}