]> git.deb.at Git - pkg/abook.git/blobdiff - misc.c
Minor fixes/cleanups
[pkg/abook.git] / misc.c
diff --git a/misc.c b/misc.c
index 225d1563f9e048b981bff605bb7283e6ad9c189e..1eadf97b19e4edc9fc43ce76196475df1e3bec69 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -2,7 +2,7 @@
 /*
  * $Id$
  *
- * by JH <jheinonen@bigfoot.com>
+ * by JH <jheinonen@users.sourceforge.net>
  *
  * Copyright (C) Jaakko Heinonen
  */
@@ -16,6 +16,9 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <errno.h>
+#ifdef HAVE_CONFIG_H
+#      include "config.h"
+#endif
 #include "misc.h"
 #ifdef ABOOK_SRC
 #      include "abook.h"
@@ -34,6 +37,8 @@ revstr(char *str)
 {
        char *s, *s2;
 
+       assert(str != NULL);
+
        s = s2 = strdup(str);
 
        while( *str )
@@ -51,6 +56,8 @@ strupper(char *str)
 {
        char *tmp = str;
 
+       assert(str != NULL);
+
        while( ( *str = toupper( *str ) ) )
                str++;
        
@@ -62,6 +69,8 @@ strlower(char *str)
 {
        char *tmp = str;
 
+       assert(str != NULL);
+
        while( ( *str = tolower ( *str ) ) )
                str++;
 
@@ -116,6 +125,8 @@ mkstr (const char *format, ... )
                (char *) malloc (size);
 #endif
        
+       assert(format != NULL);
+
        for(;;) {
                int n;
                MY_VA_START(format);
@@ -148,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);
@@ -181,7 +191,7 @@ strconcat (const char *str, ...)
 
 
 int
-safe_strcmp(const char *s1, const char * s2)
+safe_strcmp(const char *s1, const char *s2)
 {
        if (s1 == NULL && s2 == NULL) return 0;
        if (s1 == NULL) return -1;
@@ -190,16 +200,32 @@ safe_strcmp(const char *s1, const char * s2)
        return strcmp(s1, s2);
 }
 
+int
+safe_strcoll(const char *s1, const char *s2)
+{
+#ifdef HAVE_STRCOLL
+       if (s1 == NULL && s2 == NULL) return 0;
+       if (s1 == NULL) return -1;
+       if (s2 == NULL) return 1;
+
+       return strcoll(s1, s2);
+#else /* fall back to strcmp */
+       return safe_strcmp(s1, s2);
+#endif
+}
+
 char *
 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;
 }