]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/xalloc: add xstrndup()
authorKarel Zak <kzak@redhat.com>
Wed, 10 Apr 2013 15:16:01 +0000 (17:16 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 10 Apr 2013 15:16:01 +0000 (17:16 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/xalloc.h

index 1f29621ef709b71d89a87177c88a54af96036453..6342793e4f1279c580f1edc5f3bd7f682789d9c3 100644 (file)
@@ -63,6 +63,21 @@ static inline char *xstrdup(const char *str)
         return ret;
 }
 
+static inline char *xstrndup(const char *str, size_t size)
+{
+        char *ret;
+
+        if (!str)
+                return NULL;
+
+        ret = strndup(str, size);
+
+        if (!ret)
+                err(XALLOC_EXIT_CODE, "cannot duplicate string");
+        return ret;
+}
+
+
 static inline int __attribute__ ((__format__(printf, 2, 3)))
     xasprintf(char **strp, const char *fmt, ...)
 {