From d2379cecf22c9e32d07b371fb05615d9bb2917aa Mon Sep 17 00:00:00 2001 From: Jaakko Heinonen Date: Wed, 19 Dec 2001 20:20:55 +0000 Subject: [PATCH] Minor fixes/cleanups --- conff.c | 32 +++++++++++++++++++++----------- conff.h | 2 +- misc.c | 17 +++++++++++++---- ui.c | 2 +- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/conff.c b/conff.c index b924293..7614fc2 100644 --- a/conff.c +++ b/conff.c @@ -53,36 +53,44 @@ 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 ignored - * NOTE: with REPLACE_KEY flag the node will be deleted in OOM - * situation - */ if( (new_item = malloc(sizeof(struct conff_node))) == NULL ) - return; - + 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 * @@ -158,6 +166,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; diff --git a/conff.h b/conff.h index c52a2f3..26963c9 100644 --- a/conff.h +++ b/conff.h @@ -34,7 +34,7 @@ struct conff_node }; -void conff_add_key(struct conff_node **ptr, char *key, +int conff_add_key(struct conff_node **ptr, char *key, char *value, int flags); char *conff_get_value(struct conff_node *node, char *key); #ifdef DEBUG diff --git a/misc.c b/misc.c index 29ba739..1eadf97 100644 --- a/misc.c +++ b/misc.c @@ -37,6 +37,8 @@ revstr(char *str) { char *s, *s2; + assert(str != NULL); + s = s2 = strdup(str); while( *str ) @@ -54,6 +56,8 @@ strupper(char *str) { char *tmp = str; + assert(str != NULL); + while( ( *str = toupper( *str ) ) ) str++; @@ -65,6 +69,8 @@ strlower(char *str) { char *tmp = str; + assert(str != NULL); + while( ( *str = tolower ( *str ) ) ) str++; @@ -119,6 +125,8 @@ mkstr (const char *format, ... ) (char *) malloc (size); #endif + assert(format != NULL); + for(;;) { int n; MY_VA_START(format); @@ -151,8 +159,7 @@ strconcat (const char *str, ...) MY_VA_LOCAL_DECL; char *s, *concat; - if(str == NULL) - return NULL; + assert(str != NULL); l = 1 + strlen (str); MY_VA_START(str); @@ -213,10 +220,12 @@ my_getcwd() char *dir = NULL; int size = 100; - dir = malloc(size); + if( (dir = malloc(size)) == NULL) + return NULL; while( getcwd(dir, size) == NULL && errno == ERANGE ) - dir = realloc(dir, size *=2); + if( (dir = realloc(dir, size *=2)) == NULL) + return NULL; return dir; } diff --git a/ui.c b/ui.c index 1af0ee3..9dfdd82 100644 --- a/ui.c +++ b/ui.c @@ -237,7 +237,7 @@ statusline_addstr(char *str) * parameters: * (char *str) * if n >= 0 str is a pointer which points a place where to store - * the string, else str is ingnored + * the string, else str is ignored * (int n) * the maximum length of the string * If n < 0 function will allocate needed space for the string. -- 2.39.2