*/
static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
{
- char *msg;
+ int n;
+ size_t size = 100;
+ char *p = malloc(size);
- VSPRINTF(fmt, msg);
- return msg;
+ if (!p)
+ return NULL;
+ while (1) {
+ char *q;
+ 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 */
+ q = realloc(p, size);
+ if (!q) {
+ free(p);
+ return NULL;
+ }
+ }
+ return p;
}
/**
*
* \sa lseek(2).
*/
-int para_lseek(int fd, off_t *offset, int whence)
+static int __lseek(int fd, off_t *offset, int whence)
{
*offset = lseek(fd, *offset, whence);
int ret = -E_LSEEK;
*
* \sa write(2).
*/
-ssize_t para_write(int fd, const void *buf, size_t size)
+static ssize_t __write(int fd, const void *buf, size_t size)
{
ssize_t ret;
*
* \sa para_write().
*/
-ssize_t para_write_all(int fd, const void *buf, size_t size)
+static ssize_t write_all(int fd, const void *buf, size_t size)
{
// DEBUG_LOG("writing %zu bytes\n", size);
const char *b = buf;
while (size) {
- ssize_t ret = para_write(fd, b, size);
+ ssize_t ret = __write(fd, b, size);
// DEBUG_LOG("ret: %zd\n", ret);
if (ret < 0)
return ret;
if (ret < 0)
return ret;
fd = ret;
- ret = para_write_all(fd, buf, size);
+ ret = write_all(fd, buf, size);
if (ret < 0)
goto out;
ret = 1;
return ret;
fd = ret;
if (header && header_size) {
- ret = para_write_all(fd, header, header_size);
+ ret = write_all(fd, header, header_size);
if (ret < 0)
goto out;
}
- ret = para_write_all(fd, data, data_size);
+ ret = write_all(fd, data, data_size);
if (ret < 0)
goto out;
if (new_pos) {
off_t offset = 0;
- ret = para_lseek(fd, &offset, SEEK_END);
+ ret = __lseek(fd, &offset, SEEK_END);
if (ret < 0)
goto out;
// DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);