Mercurial > hg > nnchat
comparison th_string.c @ 361:b3556ff686fc
Fix th_strdup_vprintf().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 23 Jun 2011 11:10:30 +0300 |
parents | fae4651d37bc |
children | 7f9057e29af2 |
comparison
equal
deleted
inserted
replaced
360:b465a17ffa47 | 361:b3556ff686fc |
---|---|
56 | 56 |
57 /* Simulate a sprintf() that allocates memory | 57 /* Simulate a sprintf() that allocates memory |
58 */ | 58 */ |
59 char * th_strdup_vprintf(const char *fmt, va_list args) | 59 char * th_strdup_vprintf(const char *fmt, va_list args) |
60 { | 60 { |
61 int size = 100; | 61 int size = 64; |
62 char *buf, *nbuf = NULL; | 62 char *buf, *nbuf = NULL; |
63 | 63 |
64 if ((buf = th_malloc(size)) == NULL) | 64 if ((buf = th_malloc(size)) == NULL) |
65 return NULL; | 65 return NULL; |
66 | 66 |
67 fprintf(stderr, "th_strdup_vprintf(\"%s\", ...):\n", fmt); | |
67 while (1) { | 68 while (1) { |
68 int n = vsnprintf(buf, size, fmt, args); | 69 int n; |
70 va_list ap; | |
71 va_copy(ap, args); | |
72 n = vsnprintf(buf, size, fmt, ap); | |
73 va_end(ap); | |
74 | |
69 if (n > -1 && n < size) | 75 if (n > -1 && n < size) |
70 return buf; | 76 return buf; |
71 if (n > -1) | 77 if (n > -1) |
72 size = n + 1; | 78 size = n + 1; |
73 else | 79 else |
74 size *= 2; | 80 size *= 2; |
75 | 81 |
76 if ((nbuf = th_realloc(nbuf, size)) == NULL) { | 82 if ((nbuf = th_realloc(nbuf, size)) == NULL) { |
77 th_free(buf); | 83 th_free(buf); |
78 return NULL; | 84 return NULL; |
79 } | 85 } |
80 | 86 |