add m_strpad to the string functions.
[apps/madmutt.git] / lib-lib / str.h
index 882a103..fd80950 100644 (file)
@@ -301,6 +301,25 @@ static inline ssize_t m_strputc(char *dst, ssize_t n, int c) {
     return 1;
 }
 
+/** \brief Sets a portion of a string to a defined character, à la memset.
+ *
+ * \param[in]  dst  pointer to the buffer.
+ * \param[in]  n    size of that buffer, (negative values allowed).
+ * \param[in]  c    the char to use in the padding.
+ * \param[in]  len  length of the padding.
+ * \return MAX(0, len).
+ */
+__attribute__((nonnull(1)))
+static inline ssize_t m_strpad(char *dst, ssize_t n, int c, ssize_t len)
+{
+    ssize_t dlen = MIN(n - 1, len);
+    if (dlen > 0) {
+        memset(dst, c, dlen);
+        dst[dlen] = '\0';
+    }
+    return MAX(0, len);
+}
+
 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
     __attribute__((nonnull(1)));