From 9f36f6d615ab6eca2e37823213c0f6f3ead25921 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Wed, 27 Sep 2017 12:17:53 +0200 Subject: file_list: Add function to add an entry to the list Add file_list_add_entry() to add a single entry to a file_list. Then use it in file_list_parse_one() instead of open coding adding a new entry. Signed-off-by: Sascha Hauer --- common/file-list.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'common/file-list.c') diff --git a/common/file-list.c b/common/file-list.c index 90c0f429c5..010f8f0617 100644 --- a/common/file-list.c +++ b/common/file-list.c @@ -8,12 +8,26 @@ #define PARSE_NAME 1 #define PARSE_FLAGS 2 +int file_list_add_entry(struct file_list *files, const char *name, const char *filename, + unsigned long flags) +{ + struct file_list_entry *entry = xzalloc(sizeof(*entry)); + + entry->name = xstrdup(name); + entry->filename = xstrdup(filename); + entry->flags = flags; + + list_add_tail(&entry->list, &files->list); + + return 0; +} + static int file_list_parse_one(struct file_list *files, const char *partstr, const char **endstr) { int i = 0, state = PARSE_DEVICE; char filename[PATH_MAX]; char name[PATH_MAX]; - struct file_list_entry *entry = xzalloc(sizeof(*entry)); + unsigned long flags = 0; memset(filename, 0, sizeof(filename)); memset(name, 0, sizeof(name)); @@ -39,13 +53,13 @@ static int file_list_parse_one(struct file_list *files, const char *partstr, con case PARSE_FLAGS: switch (*partstr) { case 's': - entry->flags |= FILE_LIST_FLAG_SAFE; + flags |= FILE_LIST_FLAG_SAFE; break; case 'r': - entry->flags |= FILE_LIST_FLAG_READBACK; + flags |= FILE_LIST_FLAG_READBACK; break; case 'c': - entry->flags |= FILE_LIST_FLAG_CREATE; + flags |= FILE_LIST_FLAG_CREATE; break; default: return -EINVAL; @@ -60,15 +74,11 @@ static int file_list_parse_one(struct file_list *files, const char *partstr, con if (state != PARSE_FLAGS) return -EINVAL; - entry->name = xstrdup(name); - entry->filename = xstrdup(filename); if (*partstr == ',') partstr++; *endstr = partstr; - list_add_tail(&entry->list, &files->list); - - return 0; + return file_list_add_entry(files, name, filename, flags); } struct file_list *file_list_parse(const char *str) -- cgit v1.2.3