diff options
-rw-r--r-- | include/strutils.h | 13 | ||||
-rw-r--r-- | libfdisk/src/ask.c | 2 | ||||
-rw-r--r-- | libmount/src/fs.c | 78 | ||||
-rw-r--r-- | libmount/src/tab.c | 26 |
4 files changed, 22 insertions, 97 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) \ diff --git a/libfdisk/src/ask.c b/libfdisk/src/ask.c index fd0555e30..cbf5e9738 100644 --- a/libfdisk/src/ask.c +++ b/libfdisk/src/ask.c @@ -106,7 +106,7 @@ const char *fdisk_ask_get_query(struct fdisk_ask *ask) int fdisk_ask_set_query(struct fdisk_ask *ask, const char *str) { assert(ask); - return !strdup_to_struct_member(ask, query, str) ? -ENOMEM : 0; + return strdup_to_struct_member(ask, query, str); } /** diff --git a/libmount/src/fs.c b/libmount/src/fs.c index 2bab7d6af..df00504ba 100644 --- a/libmount/src/fs.c +++ b/libmount/src/fs.c @@ -518,27 +518,15 @@ const char *mnt_fs_get_target(struct libmnt_fs *fs) /** * mnt_fs_set_target: * @fs: fstab/mtab/mountinfo entry - * @target: mountpoint + * @tgt: mountpoint * - * This function creates a private copy (strdup()) of @target. + * This function creates a private copy (strdup()) of @tgt. * * Returns: 0 on success or negative number in case of error. */ -int mnt_fs_set_target(struct libmnt_fs *fs, const char *target) +int mnt_fs_set_target(struct libmnt_fs *fs, const char *tgt) { - char *p = NULL; - - if (!fs) - return -EINVAL; - if (target) { - p = strdup(target); - if (!p) - return -ENOMEM; - } - free(fs->target); - fs->target = p; - - return 0; + return strdup_to_struct_member(fs, target, tgt); } static int mnt_fs_get_flags(struct libmnt_fs *fs) @@ -980,19 +968,7 @@ const char *mnt_fs_get_attributes(struct libmnt_fs *fs) */ int mnt_fs_set_attributes(struct libmnt_fs *fs, const char *optstr) { - char *p = NULL; - - if (!fs) - return -EINVAL; - if (optstr) { - p = strdup(optstr); - if (!p) - return -ENOMEM; - } - free(fs->attrs); - fs->attrs = p; - - return 0; + return strdup_to_struct_member(fs, attrs, optstr); } /** @@ -1098,24 +1074,13 @@ const char *mnt_fs_get_root(struct libmnt_fs *fs) /** * mnt_fs_set_root: * @fs: mountinfo entry - * @root: path + * @path: root path * * Returns: 0 on success or negative number in case of error. */ -int mnt_fs_set_root(struct libmnt_fs *fs, const char *root) +int mnt_fs_set_root(struct libmnt_fs *fs, const char *path) { - char *p = NULL; - - if (!fs) - return -EINVAL; - if (root) { - p = strdup(root); - if (!p) - return -ENOMEM; - } - free(fs->root); - fs->root = p; - return 0; + return strdup_to_struct_member(fs, root, path); } /** @@ -1196,18 +1161,7 @@ const char *mnt_fs_get_bindsrc(struct libmnt_fs *fs) */ int mnt_fs_set_bindsrc(struct libmnt_fs *fs, const char *src) { - char *p = NULL; - - if (!fs) - return -EINVAL; - if (src) { - p = strdup(src); - if (!p) - return -ENOMEM; - } - free(fs->bindsrc); - fs->bindsrc = p; - return 0; + return strdup_to_struct_member(fs, bindsrc, src); } /** @@ -1326,19 +1280,7 @@ const char *mnt_fs_get_comment(struct libmnt_fs *fs) */ int mnt_fs_set_comment(struct libmnt_fs *fs, const char *comm) { - char *p = NULL; - - if (!fs) - return -EINVAL; - if (comm) { - p = strdup(comm); - if (!p) - return -ENOMEM; - } - - free(fs->comment); - fs->comment = p; - return 0; + return strdup_to_struct_member(fs, comment, comm); } /** diff --git a/libmount/src/tab.c b/libmount/src/tab.c index a8d835e04..b09c79f0d 100644 --- a/libmount/src/tab.c +++ b/libmount/src/tab.c @@ -287,18 +287,7 @@ const char *mnt_table_get_intro_comment(struct libmnt_table *tb) */ int mnt_table_set_intro_comment(struct libmnt_table *tb, const char *comm) { - char *p = NULL; - - if (!tb) - return -EINVAL; - if (comm) { - p = strdup(comm); - if (!p) - return -ENOMEM; - } - free(tb->comm_intro); - tb->comm_intro = p; - return 0; + return strdup_to_struct_member(tb, comm_intro, comm); } /** @@ -339,18 +328,7 @@ const char *mnt_table_get_trailing_comment(struct libmnt_table *tb) */ int mnt_table_set_trailing_comment(struct libmnt_table *tb, const char *comm) { - char *p = NULL; - - if (!tb) - return -EINVAL; - if (comm) { - p = strdup(comm); - if (!p) - return -ENOMEM; - } - free(tb->comm_tail); - tb->comm_tail = p; - return 0; + return strdup_to_struct_member(tb, comm_tail, comm); } /** |