]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/configs: add head parameter to configs_refer_filename()
authorKarel Zak <kzak@redhat.com>
Wed, 15 Oct 2025 13:11:25 +0000 (15:11 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 15 Oct 2025 13:11:25 +0000 (15:11 +0200)
Allow configs_refer_filename() to add entries at either the head
or tail of the list by adding a 'head' parameter. This simplifies
the code for adding the main config file, eliminating the need to
add to the tail and then move to the head.

When head=1, use list_add() to prepend to the list.
When head=0, use list_add_tail() to append to the list.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/configs.c

index a7faf6ad248853f9a65826994fbaf55950418f45..7ecd69df656d043d6fc29d1d532794d3ead47ddf 100644 (file)
@@ -74,7 +74,7 @@ static char *main_configs(const char *root,
        return path;
 }
 
-static int configs_refer_filename(struct list_head *list, char *filename)
+static int configs_refer_filename(struct list_head *list, char *filename, int head)
 {
        struct file_element *e;
 
@@ -84,7 +84,10 @@ static int configs_refer_filename(struct list_head *list, char *filename)
 
        INIT_LIST_HEAD(&e->file_list);
        e->filename = filename;
-       list_add_tail(&e->file_list, list);
+       if (head)
+               list_add(&e->file_list, list);
+       else
+               list_add_tail(&e->file_list, list);
        return 0;
 }
 
@@ -147,7 +150,7 @@ static int read_dir(struct list_head *file_list,
                        break;
                }
 
-               ret = configs_refer_filename(file_list, filename);
+               ret = configs_refer_filename(file_list, filename, 0);
                if (ret < 0) {
                        free(filename);
                        break;
@@ -285,18 +288,11 @@ int ul_configs_file_list(struct list_head *file_list,
 
        /* Add main config file at the beginning (highest priority) */
        if (main_file != NULL) {
-               struct list_head *e;
-
-               ret = configs_refer_filename(file_list, main_file);
+               ret = configs_refer_filename(file_list, main_file, 1);
                if (ret < 0) {
                        free(main_file);
                        goto finish;
                }
-
-               /* Move last element (just added) to front */
-               e = file_list->prev;
-               list_del(e);
-               list_add(e, file_list);
        }
 
        counter = list_count_entries(file_list);