#include "list.h" #include <stdlib.h> #include <string.h> void InsertElm(ListElm *head, const char *key, const char *value) { ListElm *ptr, *last_ptr, *new_ptr; int compare; /* Lock list, find insertion point */ lock_mutex(); last_ptr = head; ptr = head->next; while (ptr) { compare = strcmp(ptr->key, key); if (compare == 0) { /* Duplicate key. Ignore */ unlock_mutex(); return; } if (compare > 0) { /* Insert before this element */ break; } last_ptr = ptr; ptr = ptr->next; } /* Create and insert new element */ if ( !(new_ptr = (ListElm *)malloc(sizeof(ListElm))) ) abort(); new_ptr->key = strdup(key); new_ptr->value = strdup(value); if (! new_ptr->key || ! new_ptr->value) abort(); new_ptr->next = ptr; last_ptr->next = new_ptr; unlock_mutex(); }