#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
+#include <errno.h>
/* default strtoxx_or_err() exit code */
#ifndef STRTOXX_EXIT_CODE
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) \
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);
}
/**
/**
* 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)
*/
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);
}
/**
/**
* 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);
}
/**
*/
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);
}
/**
*/
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);
}
/**
*/
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);
}
/**
*/
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);
}
/**