No need to loop to increase buffer size when snprintf() tells me exactly how much space I need.
modified: src/nms.c
This commit is contained in:
parent
9b5471838a
commit
4ccde16721
11
src/nms.c
11
src/nms.c
@ -52,15 +52,14 @@ char getMaskChar(void);
|
||||
* const char *format - printf-style format string
|
||||
*/
|
||||
void nmsprintf(const char *format, ...) {
|
||||
int bufferSize = PRINT_BUFFER;
|
||||
int bufferIncrementSize = PRINT_BUFFER;
|
||||
char *nmsprintBuffer = malloc(bufferSize);
|
||||
char *nmsprintBuffer = malloc(PRINT_BUFFER);
|
||||
int fmtSize;
|
||||
|
||||
va_list argp;
|
||||
va_start(argp, format);
|
||||
while (vsnprintf(nmsprintBuffer, bufferSize, format, argp) >= bufferSize) {
|
||||
bufferSize += bufferIncrementSize;
|
||||
nmsprintBuffer = realloc(nmsprintBuffer, bufferSize);
|
||||
if ((fmtSize = vsnprintf(nmsprintBuffer, PRINT_BUFFER, format, argp)) >= PRINT_BUFFER) {
|
||||
nmsprintBuffer = realloc(nmsprintBuffer, fmtSize + 1);
|
||||
vsnprintf(nmsprintBuffer, fmtSize + 1, format, argp);
|
||||
}
|
||||
va_end(argp);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user