]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] standard: provide a new 'my_strndup' function
authorWilly Tarreau <w@1wt.eu>
Sun, 10 May 2009 13:41:18 +0000 (15:41 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 10 May 2009 13:41:18 +0000 (15:41 +0200)
This function is only offered by GNU extensions and is sometimes
useful during configuration parsing.

include/common/standard.h
src/standard.c

index 6e7e9634a9bdcc9110311b53f03361d9ab553ad7..0bc28aafc4b39b51169199071407004193fb3a1a 100644 (file)
@@ -302,4 +302,7 @@ static inline unsigned int mul32hi(unsigned int a, unsigned int b)
        return ((unsigned long long)a * b) >> 32;
 }
 
+/* copies at most <n> characters from <src> and always terminates with '\0' */
+char *my_strndup(const char *src, int n);
+
 #endif /* _COMMON_STANDARD_H */
index 906f2a730f5ccd0457324eae7a9c587e8436d09b..de5b6644321b1cc727417c2f97cb6ea46e4712ce 100644 (file)
@@ -654,6 +654,23 @@ const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
        return NULL;
 }
 
+/* copies at most <n> characters from <src> and always terminates with '\0' */
+char *my_strndup(const char *src, int n)
+{
+       int len = 0;
+       char *ret;
+
+       while (len < n && src[len])
+               len++;
+
+       ret = (char *)malloc(len + 1);
+       if (!ret)
+               return ret;
+       memcpy(ret, src, len);
+       ret[len] = '\0';
+       return ret;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8