summaryrefslogtreecommitdiffstats
path: root/include/strutils.h
diff options
context:
space:
mode:
authorKarel Zak2016-04-22 13:59:06 +0200
committerKarel Zak2016-04-22 13:59:06 +0200
commitdeb1c903272d45e8a1975b1da151d4deb3defe3b (patch)
tree72bc20ea7cad9056bc6ae669debd02a8ed89f7b0 /include/strutils.h
parenttests: move getopt to separate directory (diff)
downloadkernel-qcow2-util-linux-deb1c903272d45e8a1975b1da151d4deb3defe3b.tar.gz
kernel-qcow2-util-linux-deb1c903272d45e8a1975b1da151d4deb3defe3b.tar.xz
kernel-qcow2-util-linux-deb1c903272d45e8a1975b1da151d4deb3defe3b.zip
libmount: remove duplicate code
For petty long time we have strdup_to_struct_member() macro to avoid duplicate code when strdup() strings in setter functions. Let's use it for libmount. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/strutils.h')
-rw-r--r--include/strutils.h13
1 files changed, 9 insertions, 4 deletions
diff --git a/include/strutils.h b/include/strutils.h
index e9f792175..9f4339f41 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -7,6 +7,7 @@
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
+#include <errno.h>
/* default strtoxx_or_err() exit code */
#ifndef STRTOXX_EXIT_CODE
@@ -60,20 +61,24 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
dest[n-1] = 0;
}
-static inline char *strdup_to_offset(void *stru, size_t offset, const char *str)
+static inline int strdup_to_offset(void *stru, size_t offset, const char *str)
{
char *n = NULL;
- char **o = (char **) ((char *) stru + offset);
+ char **o;
+ if (!stru)
+ return -EINVAL;
+
+ o = (char **) ((char *) stru + offset);
if (str) {
n = strdup(str);
if (!n)
- return NULL;
+ return -ENOMEM;
}
free(*o);
*o = n;
- return n;
+ return 0;
}
#define strdup_to_struct_member(_s, _m, _str) \