diff --git a/tools/wrc/genres.c b/tools/wrc/genres.c index 7871c793ac4..3d401804148 100644 --- a/tools/wrc/genres.c +++ b/tools/wrc/genres.c @@ -1741,23 +1741,8 @@ char *prep_nid_for_label(const name_id_t *nid) */ char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan) { - int nlen; - char *buf; - char *ret; - char lanbuf[6]; - - sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0); - buf = prep_nid_for_label(nid); - nlen = strlen(buf) + strlen(lanbuf); - nlen += strlen(base) + 4; /* three time '_' and '\0' */ - ret = xmalloc(nlen); - strcpy(ret, "_"); - strcat(ret, base); - strcat(ret, "_"); - strcat(ret, buf); - strcat(ret, "_"); - strcat(ret, lanbuf); - return ret; + char *buf = prep_nid_for_label(nid); + return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0); } /* diff --git a/tools/wrc/utils.c b/tools/wrc/utils.c index c9445b3a070..5adebbadebe 100644 --- a/tools/wrc/utils.c +++ b/tools/wrc/utils.c @@ -192,6 +192,25 @@ void *xrealloc(void *p, size_t size) return res; } +char *strmake( const char* fmt, ... ) +{ + int n; + size_t size = 100; + va_list ap; + + for (;;) + { + char *p = xmalloc( size ); + va_start( ap, fmt ); + n = vsnprintf( p, size, fmt, ap ); + va_end( ap ); + if (n == -1) size *= 2; + else if ((size_t)n >= size) size = n + 1; + else return p; + free( p ); + } +} + char *xstrdup(const char *str) { char *s; diff --git a/tools/wrc/utils.h b/tools/wrc/utils.h index ced55abc3af..19daa09b3a9 100644 --- a/tools/wrc/utils.h +++ b/tools/wrc/utils.h @@ -33,6 +33,7 @@ char *xstrdup(const char *str); #define __attribute__(X) #endif +char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); int parser_error(const char *s, ...) __attribute__((format (printf, 1, 2))); int parser_warning(const char *s, ...) __attribute__((format (printf, 1, 2))); void internal_error(const char *file, int line, const char *s, ...) __attribute__((format (printf, 3, 4), noreturn)); diff --git a/tools/wrc/wrc.c b/tools/wrc/wrc.c index 99285dbc991..bbeba8333fd 100644 --- a/tools/wrc/wrc.c +++ b/tools/wrc/wrc.c @@ -270,12 +270,7 @@ static int load_file( const char *input_name, const char *output_name ) exit(0); } - if (output_name && output_name[0]) - { - name = xmalloc( strlen(output_name) + 8 ); - strcpy( name, output_name ); - strcat( name, ".XXXXXX" ); - } + if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name ); else name = xstrdup( "wrc.XXXXXX" ); if ((fd = mkstemps( name, 0 )) == -1)