/*
- * misc.c
- * by JH <jheinonen@bigfoot.com>
+ * $Id$
+ *
+ * by JH <jheinonen@users.sourceforge.net>
*
* Copyright (C) Jaakko Heinonen
*/
#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"
{
char *s, *s2;
+ assert(str != NULL);
+
s = s2 = strdup(str);
while( *str )
{
char *tmp = str;
+ assert(str != NULL);
+
while( ( *str = toupper( *str ) ) )
str++;
{
char *tmp = str;
+ assert(str != NULL);
+
while( ( *str = tolower ( *str ) ) )
str++;
(char *) malloc (size);
#endif
+ assert(format != NULL);
+
for(;;) {
int n;
MY_VA_START(format);
format, ap);
MY_VA_END;
- if (n > -1)
+ if (n > -1 && n < size)
return buffer;
- size *= 2;
+ if (n > -1)
+ size = n + 1;
+ else
+ size *= 2;
buffer =
#ifdef ABOOK_SRC
MY_VA_LOCAL_DECL;
char *s, *concat;
- if(str == NULL)
- return NULL;
+ assert(str != NULL);
l = 1 + strlen (str);
MY_VA_START(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;
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;
}
len = 0;
size = thres;
- buf = abook_malloc(size);
+ buf = (char *)abook_malloc(size);
while (fgets(buf+len, size-len, f) != NULL) {
len += strlen(buf+len);