+ if (node_cmp == node)
+ return pos;
+
+ pos++;
+ node_cmp = node_cmp->next;
+ }
+
+ return 0;
+}
+
+TreeInfo *getTreeInfoFromPos(TreeInfo *node, int pos)
+{
+ TreeInfo *node_default = node;
+ int pos_cmp = 0;
+
+ while (node)
+ {
+ if (pos_cmp == pos)
+ return node;
+
+ pos_cmp++;
+ node = node->next;
+ }
+
+ return node_default;
+}
+
+TreeInfo *getTreeInfoFromIdentifier(TreeInfo *node, char *identifier)
+{
+ if (identifier == NULL)
+ return NULL;
+
+ while (node)
+ {
+ if (node->node_group)
+ {
+ TreeInfo *node_group;
+
+ node_group = getTreeInfoFromIdentifier(node->node_group, identifier);
+
+ if (node_group)
+ return node_group;
+ }
+ else if (!node->parent_link)
+ {
+ if (strcmp(identifier, node->identifier) == 0)
+ return node;
+ }
+
+ node = node->next;
+ }
+
+ return NULL;
+}
+
+void dumpTreeInfo(TreeInfo *node, int depth)
+{
+ int i;
+
+ printf("Dumping TreeInfo:\n");
+
+ while (node)
+ {
+ for (i = 0; i < (depth + 1) * 3; i++)
+ printf(" ");
+
+#if 1
+ printf("filename == '%s' ['%s', '%s'] [%d])\n",
+ node->filename, node->fullpath, node->basepath, node->user_defined);
+#else
+ printf("filename == '%s' (%s) [%s] (%d)\n",
+ node->filename, node->name, node->identifier, node->sort_priority);
+#endif
+
+ if (node->node_group != NULL)
+ dumpTreeInfo(node->node_group, depth + 1);
+
+ node = node->next;
+ }
+}
+
+void sortTreeInfo(TreeInfo **node_first,
+ int (*compare_function)(const void *, const void *))
+{
+ int num_nodes = numTreeInfo(*node_first);
+ TreeInfo **sort_array;
+ TreeInfo *node = *node_first;
+ int i = 0;
+
+ if (num_nodes == 0)
+ return;
+
+ /* allocate array for sorting structure pointers */
+ sort_array = checked_calloc(num_nodes * sizeof(TreeInfo *));
+
+ /* writing structure pointers to sorting array */
+ while (i < num_nodes && node) /* double boundary check... */
+ {
+ sort_array[i] = node;
+
+ i++;
+ node = node->next;
+ }
+
+ /* sorting the structure pointers in the sorting array */
+ qsort(sort_array, num_nodes, sizeof(TreeInfo *),
+ compare_function);
+
+ /* update the linkage of list elements with the sorted node array */
+ for (i = 0; i < num_nodes - 1; i++)
+ sort_array[i]->next = sort_array[i + 1];
+ sort_array[num_nodes - 1]->next = NULL;
+
+ /* update the linkage of the main list anchor pointer */
+ *node_first = sort_array[0];
+
+ free(sort_array);
+
+ /* now recursively sort the level group structures */
+ node = *node_first;
+ while (node)
+ {
+ if (node->node_group != NULL)
+ sortTreeInfo(&node->node_group, compare_function);
+
+ node = node->next;
+ }
+}
+
+
+/* ========================================================================= */
+/* some stuff from "files.c" */
+/* ========================================================================= */
+
+#if defined(PLATFORM_WIN32)
+#ifndef S_IRGRP
+#define S_IRGRP S_IRUSR
+#endif
+#ifndef S_IROTH
+#define S_IROTH S_IRUSR
+#endif
+#ifndef S_IWGRP
+#define S_IWGRP S_IWUSR
+#endif
+#ifndef S_IWOTH
+#define S_IWOTH S_IWUSR
+#endif
+#ifndef S_IXGRP
+#define S_IXGRP S_IXUSR
+#endif
+#ifndef S_IXOTH
+#define S_IXOTH S_IXUSR
+#endif
+#ifndef S_IRWXG
+#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
+#endif
+#ifndef S_ISGID
+#define S_ISGID 0
+#endif
+#endif /* PLATFORM_WIN32 */
+
+/* file permissions for newly written files */
+#define MODE_R_ALL (S_IRUSR | S_IRGRP | S_IROTH)
+#define MODE_W_ALL (S_IWUSR | S_IWGRP | S_IWOTH)
+#define MODE_X_ALL (S_IXUSR | S_IXGRP | S_IXOTH)
+
+#define MODE_W_PRIVATE (S_IWUSR)
+#define MODE_W_PUBLIC (S_IWUSR | S_IWGRP)
+#define MODE_W_PUBLIC_DIR (S_IWUSR | S_IWGRP | S_ISGID)
+
+#define DIR_PERMS_PRIVATE (MODE_R_ALL | MODE_X_ALL | MODE_W_PRIVATE)
+#define DIR_PERMS_PUBLIC (MODE_R_ALL | MODE_X_ALL | MODE_W_PUBLIC_DIR)
+
+#define FILE_PERMS_PRIVATE (MODE_R_ALL | MODE_W_PRIVATE)
+#define FILE_PERMS_PUBLIC (MODE_R_ALL | MODE_W_PUBLIC)
+
+char *getUserDataDir(void)
+{
+ static char *userdata_dir = NULL;
+
+ if (userdata_dir == NULL)
+ userdata_dir = getPath2(getHomeDir(), program.userdata_directory);
+
+ return userdata_dir;
+}
+
+char *getCommonDataDir(void)
+{
+ static char *common_data_dir = NULL;
+
+#if defined(PLATFORM_WIN32)
+ if (common_data_dir == NULL)
+ {
+ char *dir = checked_malloc(MAX_PATH + 1);
+
+ if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, 0, dir))
+ && strcmp(dir, "") != 0) /* empty for Windows 95/98 */
+ common_data_dir = getPath2(dir, program.userdata_directory);
+ else
+ common_data_dir = options.rw_base_directory;
+ }
+#else
+ if (common_data_dir == NULL)
+ common_data_dir = options.rw_base_directory;
+#endif
+
+ return common_data_dir;
+}
+
+char *getSetupDir()
+{
+ return getUserDataDir();
+}
+
+static mode_t posix_umask(mode_t mask)
+{
+#if defined(PLATFORM_UNIX)
+ return umask(mask);
+#else
+ return 0;
+#endif
+}
+
+static int posix_mkdir(const char *pathname, mode_t mode)
+{
+#if defined(PLATFORM_WIN32)
+ return mkdir(pathname);
+#else
+ return mkdir(pathname, mode);
+#endif
+}
+
+void createDirectory(char *dir, char *text, int permission_class)
+{
+ /* leave "other" permissions in umask untouched, but ensure group parts
+ of USERDATA_DIR_MODE are not masked */
+ mode_t dir_mode = (permission_class == PERMS_PRIVATE ?
+ DIR_PERMS_PRIVATE : DIR_PERMS_PUBLIC);
+ mode_t normal_umask = posix_umask(0);
+ mode_t group_umask = ~(dir_mode & S_IRWXG);
+ posix_umask(normal_umask & group_umask);
+
+ if (access(dir, F_OK) != 0)
+ if (posix_mkdir(dir, dir_mode) != 0)
+ Error(ERR_WARN, "cannot create %s directory '%s'", text, dir);
+
+ posix_umask(normal_umask); /* reset normal umask */
+}
+
+void InitUserDataDirectory()
+{
+ createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE);
+}
+
+void SetFilePermissions(char *filename, int permission_class)
+{
+ chmod(filename, (permission_class == PERMS_PRIVATE ?
+ FILE_PERMS_PRIVATE : FILE_PERMS_PUBLIC));
+}
+
+char *getCookie(char *file_type)
+{
+ static char cookie[MAX_COOKIE_LEN + 1];
+
+ if (strlen(program.cookie_prefix) + 1 +
+ strlen(file_type) + strlen("_FILE_VERSION_x.x") > MAX_COOKIE_LEN)
+ return "[COOKIE ERROR]"; /* should never happen */
+
+ sprintf(cookie, "%s_%s_FILE_VERSION_%d.%d",
+ program.cookie_prefix, file_type,
+ program.version_major, program.version_minor);
+
+ return cookie;
+}
+
+int getFileVersionFromCookieString(const char *cookie)
+{
+ const char *ptr_cookie1, *ptr_cookie2;
+ const char *pattern1 = "_FILE_VERSION_";
+ const char *pattern2 = "?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_pattern1 = strlen(pattern1);
+ const int len_pattern2 = strlen(pattern2);
+ const int len_pattern = len_pattern1 + len_pattern2;
+ int version_major, version_minor;
+
+ if (len_cookie <= len_pattern)
+ return -1;
+
+ ptr_cookie1 = &cookie[len_cookie - len_pattern];
+ ptr_cookie2 = &cookie[len_cookie - len_pattern2];
+
+ if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
+ return -1;
+
+ if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
+ ptr_cookie2[1] != '.' ||
+ ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
+ return -1;
+
+ version_major = ptr_cookie2[0] - '0';
+ version_minor = ptr_cookie2[2] - '0';
+
+ return VERSION_IDENT(version_major, version_minor, 0, 0);
+}
+
+boolean checkCookieString(const char *cookie, const char *template)
+{
+ const char *pattern = "_FILE_VERSION_?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_template = strlen(template);
+ const int len_pattern = strlen(pattern);
+
+ if (len_cookie != len_template)
+ return FALSE;
+
+ if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+
+/* ------------------------------------------------------------------------- */
+/* setup file list and hash handling functions */
+/* ------------------------------------------------------------------------- */
+
+char *getFormattedSetupEntry(char *token, char *value)
+{
+ int i;
+ static char entry[MAX_LINE_LEN];
+
+ /* if value is an empty string, just return token without value */
+ if (*value == '\0')
+ return token;
+
+ /* start with the token and some spaces to format output line */
+ sprintf(entry, "%s:", token);
+ for (i = strlen(entry); i < TOKEN_VALUE_POSITION; i++)
+ strcat(entry, " ");
+
+ /* continue with the token's value */
+ strcat(entry, value);
+
+ return entry;
+}
+
+SetupFileList *newSetupFileList(char *token, char *value)
+{
+ SetupFileList *new = checked_malloc(sizeof(SetupFileList));
+
+ new->token = getStringCopy(token);
+ new->value = getStringCopy(value);
+
+ new->next = NULL;
+
+ return new;
+}
+
+void freeSetupFileList(SetupFileList *list)
+{
+ if (list == NULL)
+ return;
+
+ if (list->token)
+ free(list->token);
+ if (list->value)
+ free(list->value);
+ if (list->next)
+ freeSetupFileList(list->next);
+ free(list);
+}
+
+char *getListEntry(SetupFileList *list, char *token)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strcmp(list->token, token) == 0)
+ return list->value;
+ else
+ return getListEntry(list->next, token);
+}
+
+SetupFileList *setListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strcmp(list->token, token) == 0)
+ {
+ if (list->value)
+ free(list->value);
+
+ list->value = getStringCopy(value);
+
+ return list;
+ }
+ else if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return setListEntry(list->next, token, value);
+}
+
+SetupFileList *addListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return addListEntry(list->next, token, value);
+}
+
+#ifdef DEBUG
+static void printSetupFileList(SetupFileList *list)
+{
+ if (!list)
+ return;
+
+ printf("token: '%s'\n", list->token);
+ printf("value: '%s'\n", list->value);
+
+ printSetupFileList(list->next);
+}
+#endif
+
+#ifdef DEBUG
+DEFINE_HASHTABLE_INSERT(insert_hash_entry, char, char);
+DEFINE_HASHTABLE_SEARCH(search_hash_entry, char, char);
+DEFINE_HASHTABLE_CHANGE(change_hash_entry, char, char);
+DEFINE_HASHTABLE_REMOVE(remove_hash_entry, char, char);
+#else
+#define insert_hash_entry hashtable_insert
+#define search_hash_entry hashtable_search
+#define change_hash_entry hashtable_change
+#define remove_hash_entry hashtable_remove
+#endif
+
+static unsigned int get_hash_from_key(void *key)
+{
+ /*
+ djb2
+
+ This algorithm (k=33) was first reported by Dan Bernstein many years ago in
+ 'comp.lang.c'. Another version of this algorithm (now favored by Bernstein)
+ uses XOR: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why
+ it works better than many other constants, prime or not) has never been
+ adequately explained.
+
+ If you just want to have a good hash function, and cannot wait, djb2
+ is one of the best string hash functions i know. It has excellent
+ distribution and speed on many different sets of keys and table sizes.
+ You are not likely to do better with one of the "well known" functions
+ such as PJW, K&R, etc.
+
+ Ozan (oz) Yigit [http://www.cs.yorku.ca/~oz/hash.html]
+ */
+
+ char *str = (char *)key;
+ unsigned int hash = 5381;
+ int c;
+
+ while ((c = *str++))
+ hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
+
+ return hash;
+}
+
+static int keys_are_equal(void *key1, void *key2)
+{
+ return (strcmp((char *)key1, (char *)key2) == 0);
+}
+
+SetupFileHash *newSetupFileHash()
+{
+ SetupFileHash *new_hash =
+ create_hashtable(16, 0.75, get_hash_from_key, keys_are_equal);
+
+ if (new_hash == NULL)
+ Error(ERR_EXIT, "create_hashtable() failed -- out of memory");
+
+ return new_hash;
+}
+
+void freeSetupFileHash(SetupFileHash *hash)
+{
+ if (hash == NULL)
+ return;
+
+ hashtable_destroy(hash, 1); /* 1 == also free values stored in hash */
+}
+
+char *getHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return NULL;
+
+ return search_hash_entry(hash, token);
+}
+
+void setHashEntry(SetupFileHash *hash, char *token, char *value)
+{
+ char *value_copy;
+
+ if (hash == NULL)
+ return;
+
+ value_copy = getStringCopy(value);
+
+ /* change value; if it does not exist, insert it as new */
+ if (!change_hash_entry(hash, token, value_copy))
+ if (!insert_hash_entry(hash, getStringCopy(token), value_copy))
+ Error(ERR_EXIT, "cannot insert into hash -- aborting");
+}
+
+char *removeHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return NULL;
+
+ return remove_hash_entry(hash, token);
+}
+
+#if 0
+#ifdef DEBUG
+static void printSetupFileHash(SetupFileHash *hash)
+{
+ BEGIN_HASH_ITERATION(hash, itr)
+ {
+ printf("token: '%s'\n", HASH_ITERATION_TOKEN(itr));
+ printf("value: '%s'\n", HASH_ITERATION_VALUE(itr));
+ }
+ END_HASH_ITERATION(hash, itr)
+}
+#endif
+#endif
+
+static void *loadSetupFileData(char *filename, boolean use_hash)
+{
+ char line[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
+ char *token, *value, *line_ptr;
+ void *setup_file_data, *insert_ptr = NULL;
+ boolean read_continued_line = FALSE;
+ FILE *file;
+
+ if (use_hash)
+ setup_file_data = newSetupFileHash();
+ else
+ insert_ptr = setup_file_data = newSetupFileList("", "");
+
+ if (!(file = fopen(filename, MODE_READ)))
+ {
+ Error(ERR_WARN, "cannot open configuration file '%s'", filename);
+ return NULL;
+ }
+
+ while (!feof(file))