diff options
author | Karel Zak | 2013-04-10 17:16:01 +0200 |
---|---|---|
committer | Karel Zak | 2013-04-10 17:16:01 +0200 |
commit | 2b88d3d72b6dde337ec7a3a437c74364f02b15ec (patch) | |
tree | afc37710ad96c575b42dd14f922fd07ef27da459 /include/xalloc.h | |
parent | tests: update lscpu tests (diff) | |
download | kernel-qcow2-util-linux-2b88d3d72b6dde337ec7a3a437c74364f02b15ec.tar.gz kernel-qcow2-util-linux-2b88d3d72b6dde337ec7a3a437c74364f02b15ec.tar.xz kernel-qcow2-util-linux-2b88d3d72b6dde337ec7a3a437c74364f02b15ec.zip |
lib/xalloc: add xstrndup()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/xalloc.h')
-rw-r--r-- | include/xalloc.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/include/xalloc.h b/include/xalloc.h index 1f29621ef..6342793e4 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -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, ...) { |