]> git.deb.at Git - pkg/abook.git/blobdiff - conff.c
cancel key support added
[pkg/abook.git] / conff.c
diff --git a/conff.c b/conff.c
index 84050665bda8d9660d61de6a5d7758dca8f548b4..80e9c5fb61b4ebcabef92e608f5ce8656d0c1c50 100644 (file)
--- a/conff.c
+++ b/conff.c
@@ -1,27 +1,23 @@
 
 /*
  *
- *  $Id$
+ * $Id$
  *
- *  Copyright (C) Jaakko Heinonen
+ * Copyright (C) Jaakko Heinonen
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/*
- * conff.c version 0.3.0
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
 #include <stdio.h>
@@ -57,36 +53,45 @@ conff_free_node(struct conff_node *node)
        free(node);
 }
 
-void
+/*
+ * conff_add_key
+ *
+ * returns 0 if the key was successfully added
+ */
+
+int
 conff_add_key(struct conff_node **ptr, char *key, char *value, int flags)
 {
        struct conff_node *new_item, *next = NULL;
+       int replace = 0;
 
        assert(key != NULL && value != NULL);
 
        for(; *ptr; ptr = &( (*ptr) -> next) ) 
                if(!strcasecmp(key, (*ptr) -> key ) ) {
                        if (flags & REPLACE_KEY) {
-                               next = (*ptr) -> next;
-                               conff_free_node(*ptr);
+                               replace = 1;
                                break;
                        } else
-                               return;
+                               return 1;
                }
        
-        /*
-         * out of memory - error is ingnored
-         * NOTE: with REPLACE_KEY flag the node will be deleted in OOM
-         * situation
-         */
-       if( (new_item = malloc(sizeof(struct conff_node))) == NULL )
-               return;
-       
+       if( (new_item = (struct conff_node *)malloc(sizeof(struct conff_node)))
+                       == NULL )
+               return 5;
+
+       if(replace) { 
+               next = (*ptr) -> next;
+               conff_free_node(*ptr);
+       }
+
        new_item -> key = strdup(key);
        new_item -> value = strdup(value);
        new_item -> next = next;
 
        *ptr = new_item;
+
+       return 0;
 }
 
 char *
@@ -162,6 +167,8 @@ conff_load_file(struct conff_node **node, char *filename, int flags)
        char *line = NULL, *tmp;
        int i = 0;
 
+       assert(filename != NULL);
+
        if (!(in = fopen(filename, "r")))
                return -1;