Fix another typo.
[adu.git] / user.c
diff --git a/user.c b/user.c
index bde47a7e79b87cc50d5813361d13cfc86746f272..dfa1c103a9211e1a78e5e0502b0537e4a22cb885 100644 (file)
--- a/user.c
+++ b/user.c
@@ -10,7 +10,6 @@
 #include <dirent.h> /* readdir() */
 #include <sys/types.h>
 #include <pwd.h>
-#include "cmdline.h" /* TODO: This file should be independent of command line options */
 #include "user.h"
 #include "fd.h"
 #include "string.h"
@@ -52,9 +51,6 @@ static struct user_info *uid_hash_table;
 /** This is always a power of two. It is set in create_hash_table(). */
 static uint32_t uid_hash_table_size;
 
-/* Array of indices to the entries of \a uid_hash_table. */
-static int *uid_hash_table_sort_idx;
-
 /** The number of used slots in the hash table. */
 static uint32_t num_uids;
 
@@ -261,7 +257,7 @@ static inline int ui_admissible(struct user_info *ui)
        return ui->flags & UI_FL_ADMISSIBLE;
 }
 
-static int open_user_table(struct user_info *ui, int create)
+static int open_user_table(const char *dirname, struct user_info *ui, int create)
 {
        int ret;
        struct passwd *pw;
@@ -270,7 +266,7 @@ static int open_user_table(struct user_info *ui, int create)
        ui->desc->num_columns = NUM_UT_COLUMNS;
        ui->desc->flags = 0;
        ui->desc->column_descriptions = user_table_cols;
-       ui->desc->dir = adu_strdup(conf.database_dir_arg);
+       ui->desc->dir = adu_strdup(dirname);
        ui->desc->name = make_message("%u", (unsigned)ui->uid);
        pw = getpwuid(ui->uid);
        if (pw && pw->pw_name)
@@ -325,8 +321,7 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
        assert(uid_hash_table);
        for (i = 0; i < uid_hash_table_size; i++) {
                int ret;
-               struct user_info *ui = uid_hash_table +
-                       uid_hash_table_sort_idx[i];
+               struct user_info *ui = uid_hash_table + i;
 
                if (!ui_used(ui) || !ui_admissible(ui))
                        continue;
@@ -349,14 +344,9 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
  */
 void create_hash_table(unsigned bits)
 {
-       int i;
-
        uid_hash_table_size = 1 << bits;
        uid_hash_table = adu_calloc(uid_hash_table_size *
                sizeof(struct user_info));
-       uid_hash_table_sort_idx = adu_malloc(uid_hash_table_size * sizeof(int));
-       for (i = 0; i < uid_hash_table_size; i++)
-               uid_hash_table_sort_idx[i] = i;
 }
 
 /**
@@ -394,8 +384,6 @@ void close_user_tables(void)
        }
        free(uid_hash_table);
        uid_hash_table = NULL;
-       free(uid_hash_table_sort_idx);
-       uid_hash_table_sort_idx = NULL;
 }
 
 /*
@@ -447,7 +435,8 @@ static struct user_info *lookup_uid(uint32_t uid)
  *
  * \return Standard.
  */
-int create_user_table(uint32_t uid, struct user_info **ui_ptr)
+int create_user_table(const char *dirname, uint32_t uid,
+               struct user_info **ui_ptr)
 {
        struct user_info *ui = lookup_uid(uid);
 
@@ -458,39 +447,13 @@ int create_user_table(uint32_t uid, struct user_info **ui_ptr)
                return 1;
        ui->uid = uid;
        ui->flags |= UI_FL_SLOT_USED;
-       return open_user_table(ui, 1);
-}
-
-static char *get_uid_list_name(void)
-{
-       return make_message("%s/uid_list", conf.database_dir_arg);
-}
-
-static int (*hash_table_comparator)(struct user_info *a, struct user_info *b);
-
-static int comp_wrapper(const void *a, const void *b)
-{
-       struct user_info *x = uid_hash_table + *(unsigned *)a;
-       struct user_info *y = uid_hash_table + *(unsigned *)b;
-       return hash_table_comparator(x, y);
+       return open_user_table(dirname, ui, 1);
 }
 
-/**
- * Sort the hash table according to a given comparator.
- *
- * \param comp The comparator.
- *
- * The comparator is a user-defined function which must return 1, 0, or -1.
- *
- * \sa qsort(3).
- */
-void sort_hash_table(int (*comp)(struct user_info *, struct user_info *))
+static char *get_uid_list_name(const char *dirname)
 {
-       hash_table_comparator = comp;
-       qsort(uid_hash_table_sort_idx, uid_hash_table_size,
-               sizeof(*uid_hash_table_sort_idx), comp_wrapper);
+       return make_message("%s/uid_list", dirname);
 }
-
 /**
  * Open the osl tables for all admissible uids.
  *
@@ -506,7 +469,8 @@ void sort_hash_table(int (*comp)(struct user_info *, struct user_info *))
  *
  * \return Stamdard.
  */
-int open_admissible_user_tables(struct uid_range *admissible_uids)
+int open_admissible_user_tables(const char *dirname,
+               struct uid_range *admissible_uids)
 {
        struct user_info *ui;
 
@@ -525,7 +489,7 @@ int open_admissible_user_tables(struct uid_range *admissible_uids)
                ui->flags |= UI_FL_ADMISSIBLE;
                if (ui->table)
                        continue;
-               ret = open_user_table(ui, 0);
+               ret = open_user_table(dirname, ui, 0);
                if (ret < 0)
                        return ret;
        }
@@ -544,11 +508,11 @@ int open_admissible_user_tables(struct uid_range *admissible_uids)
  *
  * \return Standard.
  */
-int read_uid_file(void)
+int read_uid_file(const char *dirname)
 {
        size_t size;
        uint32_t n;
-       char *filename = get_uid_list_name(), *map;
+       char *filename = get_uid_list_name(dirname), *map;
        int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
        unsigned bits;
 
@@ -598,7 +562,7 @@ out:
  *
  * \return Standard.
  */
-int write_uid_file(void)
+int write_uid_file(const char *dirname)
 {
        char *buf, *p, *filename;
        size_t size = num_uids * sizeof(uint32_t);
@@ -614,7 +578,7 @@ int write_uid_file(void)
                write_u32(p, ui->uid);
                p += sizeof(uint32_t);
        }
-       filename = get_uid_list_name();
+       filename = get_uid_list_name(dirname);
        ret = adu_write_file(filename, buf, size);
        free(filename);
        free(buf);