X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=misc.c;h=fa67f0c99a644942f26c2b43af43ca3f85282370;hb=9e5f6d00416e4107a5c1b1e30874a2a0119e0f08;hp=eae1e90178b37ee344fa4f16766936118b6a4137;hpb=12a57d9405d7910566d5ff888d743d29b0716554;p=pkg%2Fabook.git diff --git a/misc.c b/misc.c index eae1e90..fa67f0c 100644 --- a/misc.c +++ b/misc.c @@ -2,7 +2,7 @@ /* * $Id$ * - * by JH + * by JH * * Copyright (C) Jaakko Heinonen */ @@ -16,6 +16,9 @@ #include #include #include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif #include "misc.h" #ifdef ABOOK_SRC # include "abook.h" @@ -29,39 +32,13 @@ #include -char * -revstr(char *str) -{ - char *s, *s2; - - s = s2 = strdup(str); - - while( *str ) - str++; - - while( *s ) - *--str = *s++; - - free(s2); - return str; -} - -char * -strupper(char *str) -{ - char *tmp = str; - - while( ( *str = toupper( *str ) ) ) - str++; - - return tmp; -} - char * strlower(char *str) { char *tmp = str; + assert(str != NULL); + while( ( *str = tolower ( *str ) ) ) str++; @@ -116,6 +93,8 @@ mkstr (const char *format, ... ) (char *) malloc (size); #endif + assert(format != NULL); + for(;;) { int n; MY_VA_START(format); @@ -123,10 +102,13 @@ mkstr (const char *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 @@ -145,8 +127,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); @@ -178,7 +159,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; @@ -187,16 +168,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 = (char *)malloc(size)) == NULL) + return NULL; while( getcwd(dir, size) == NULL && errno == ERANGE ) - dir = realloc(dir, size *=2); + if( (dir = (char *)realloc(dir, size *=2)) == NULL) + return NULL; return dir; } @@ -229,7 +226,7 @@ getaline(FILE *f) 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); @@ -237,7 +234,8 @@ getaline(FILE *f) break; /* the whole line has been read */ for (inc = size, p = NULL; inc > mininc; inc /= 2) - if ((p = abook_realloc(buf, size + inc)) != NULL) + if ((p = (char *)abook_realloc(buf, size + inc)) != + NULL) break; size += inc; @@ -253,7 +251,7 @@ getaline(FILE *f) buf[--len] = '\0'; if (size - len > mucho) { /* a plenitude of unused memory? */ - p = abook_realloc(buf, len+1); + p = (char *)abook_realloc(buf, len+1); if (p != NULL) { buf = p; size = len+1;