* psprintf * * Format text data under the control of fmt (an sprintf-style format string) * and return it in an allocated-on-demand buffer. The buffer is allocated * with palloc in the backend, or malloc in frontend builds. Caller is * responsible to free the buffer when no longer needed, if appropriate. * * Errors are not returned to the caller, but are reported via elog(ERROR) * in the
| 43 | * One should therefore think twice about using this in libpq. |
| 44 | */ |
| 45 | char * |
| 46 | psprintf(const char *fmt,...) |
| 47 | { |
| 48 | int save_errno = errno; |
| 49 | size_t len = 128; /* initial assumption about buffer size */ |
| 50 | |
| 51 | for (;;) |
| 52 | { |
| 53 | char *result; |
| 54 | va_list args; |
| 55 | size_t newlen; |
| 56 | |
| 57 | /* |
| 58 | * Allocate result buffer. Note that in frontend this maps to malloc |
| 59 | * with exit-on-error. |
| 60 | */ |
| 61 | result = (char *) palloc(len); |
| 62 | |
| 63 | /* Try to format the data. */ |
| 64 | errno = save_errno; |
| 65 | va_start(args, fmt); |
| 66 | newlen = pvsnprintf(result, len, fmt, args); |
| 67 | va_end(args); |
| 68 | |
| 69 | if (newlen < len) |
| 70 | return result; /* success */ |
| 71 | |
| 72 | /* Release buffer and loop around to try again with larger len. */ |
| 73 | pfree(result); |
| 74 | len = newlen; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * pvsnprintf |