diff options
author | Karel Zak <kzak@redhat.com> | 2013-04-10 17:16:01 +0200 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2013-04-10 17:16:01 +0200 |
commit | 2b88d3d72b6dde337ec7a3a437c74364f02b15ec (patch) | |
tree | afc37710ad96c575b42dd14f922fd07ef27da459 /include | |
parent | 78c70e23b5b533e2a48b0dc4bdf7d000ea918bb1 (diff) | |
download | util-linux-2b88d3d72b6dde337ec7a3a437c74364f02b15ec.tar.gz |
lib/xalloc: add xstrndup()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include')
-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 1f29621e..6342793e 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, ...) { |