X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=misc.c;h=4bfe3d353cd710f5a5abb4f54fead49c16b888b0;hb=8e362f5a1e900ad2cd11192436b5182e8b0e6ef6;hp=7bf2f8fc1380f9a6b6c55f2059725c2da0afb6b2;hpb=3cea3c8809447c5d6658671b78b9af7137fd9b0d;p=pkg%2Fabook.git diff --git a/misc.c b/misc.c index 7bf2f8f..4bfe3d3 100644 --- a/misc.c +++ b/misc.c @@ -19,6 +19,9 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif +#ifdef HANDLE_MULTIBYTE +# include +#endif #include "misc.h" #ifdef ABOOK_SRC # include "abook.h" @@ -32,38 +35,6 @@ #include -char * -revstr(char *str) -{ - char *s, *s2; - - assert(str != NULL); - - s = s2 = strdup(str); - - while( *str ) - str++; - - while( *s ) - *--str = *s++; - - free(s2); - return str; -} - -char * -strupper(char *str) -{ - char *tmp = str; - - assert(str != NULL); - - while( ( *str = toupper( *str ) ) ) - str++; - - return tmp; -} - char * strlower(char *str) { @@ -117,12 +88,12 @@ char * mkstr (const char *format, ... ) { MY_VA_LOCAL_DECL; - int size = 100; + size_t size = 100; char *buffer = #ifdef ABOOK_SRC (char *) abook_malloc (size); #else - (char *) malloc (size); + (char *) xmalloc (size); #endif assert(format != NULL); @@ -146,7 +117,7 @@ mkstr (const char *format, ... ) #ifdef ABOOK_SRC (char *) abook_realloc (buffer, size); #else - (char *) realloc (buffer, size); + (char *) xrealloc (buffer, size); #endif } } @@ -155,7 +126,7 @@ mkstr (const char *format, ... ) char* strconcat (const char *str, ...) { - int l; + unsigned long l; MY_VA_LOCAL_DECL; char *s, *concat; @@ -174,8 +145,10 @@ strconcat (const char *str, ...) #ifdef ABOOK_SRC abook_malloc(l); #else - malloc(l); + xmalloc(l); #endif + if(concat == NULL) + return NULL; strcpy (concat, str); MY_VA_START(str); @@ -218,10 +191,12 @@ char * my_getcwd() { char *dir = NULL; - int size = 100; + size_t size = 100; if( (dir = (char *)malloc(size)) == NULL) return NULL; + + *dir = 0; while( getcwd(dir, size) == NULL && errno == ERANGE ) if( (dir = (char *)realloc(dir, size *=2)) == NULL) @@ -232,8 +207,8 @@ my_getcwd() #define INITIAL_SIZE 128 #ifndef ABOOK_SRC -# define abook_malloc(X) malloc(X) -# define abook_realloc(X, XX) realloc(X, XX) +# define abook_malloc(X) xmalloc(X) +# define abook_realloc(X, XX) xrealloc(X, XX) #endif char * @@ -293,6 +268,28 @@ getaline(FILE *f) return buf; } +int +strwidth(const char *s) +{ + assert(s); +#ifdef HANDLE_MULTIBYTE + return (int)mbswidth(s, 0); +#else + return strlen(s); +#endif +} + +int +bytes2width(const char *s, int width) +{ + assert(s); +#ifdef HANDLE_MULTIBYTE + return mbsnbytes(s, strlen(s), width, 0); +#else + return width; +#endif +} + /************************************************************** * Original: * Patrick Powell Tue Apr 11 09:48:21 PDT 1995