X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=str.c;h=cca898d6aa0e4fdcf05eaea528b7d223ef054ccf;hp=d6c768d0bc90e3a0873f1aca8312512c9195bd91;hb=96c620fcf383faffb16c89c2ddd9b0857de6dfbb;hpb=8daf8f3836e745ba07f1aa01252279588574c95a diff --git a/str.c b/str.c index d6c768d..cca898d 100644 --- a/str.c +++ b/str.c @@ -20,36 +20,6 @@ #include "err.h" #include "str.h" -/** - * Write a message to a dynamically allocated string. - * - * \param fmt Usual format string. - * \param p Result pointer. - * - * \sa printf(3). */ -#define VSPRINTF(fmt, p) \ -{ \ - int n; \ - size_t size = 100; \ - p = dss_malloc(size); \ - while (1) { \ - va_list ap; \ - /* Try to print in the allocated space. */ \ - va_start(ap, fmt); \ - n = vsnprintf(p, size, fmt, ap); \ - va_end(ap); \ - /* If that worked, return the string. */ \ - if (n > -1 && n < size) \ - break; \ - /* Else try again with more space. */ \ - if (n > -1) /* glibc 2.1 */ \ - size = n + 1; /* precisely what is needed */ \ - else /* glibc 2.0 */ \ - size *= 2; /* twice the old size */ \ - p = dss_realloc(p, size); \ - } \ -} - /** * dss' version of realloc(). * @@ -165,15 +135,30 @@ __must_check __malloc char *dss_strdup(const char *s) __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...) { char *msg; + size_t size = 100; + + msg = dss_malloc(size); + while (1) { + int n; + va_list ap; - VSPRINTF(fmt, msg); - return msg; + /* Try to print in the allocated space. */ + va_start(ap, fmt); + n = vsnprintf(msg, size, fmt, ap); + va_end(ap); + /* If that worked, return the string. */ + if (n < size) + return msg; + /* Else try again with more space. */ + size = n + 1; /* precisely what is needed */ + msg = dss_realloc(msg, size); + } } /** * Get the home directory of the current user. * - * \return A dynammically allocated string that must be freed by the caller. If + * \return A dynamically allocated string that must be freed by the caller. If * the home directory could not be found, this function returns "/tmp". */ __must_check __malloc char *get_homedir(void) @@ -214,7 +199,7 @@ int dss_atoi64(const char *str, int64_t *value) /** * Get the logname of the current user. * - * \return A dynammically allocated string that must be freed by the caller. On + * \return A dynamically allocated string that must be freed by the caller. On * errors, the string "unknown user" is returned, i.e. this function never * returns \p NULL. * @@ -233,7 +218,7 @@ __must_check __malloc char *dss_logname(void) * \param argv_ptr Pointer to the list of substrings. * \param delim Delimiter. * - * This function modifies \a args by replacing each occurance of \a delim by + * This function modifies \a args by replacing each occurrence of \a delim by * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically * and these pointers are initialized to point to the broken-up substrings * within \a args. A pointer to this array is returned via \a argv_ptr. @@ -242,7 +227,7 @@ __must_check __malloc char *dss_logname(void) */ unsigned split_args(char *args, char *** const argv_ptr, const char *delim) { - char *p = args; + char *p; char **argv; size_t n = 0, i, j;