196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * This file and its contents are supplied under the terms of the
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * Common Development and Distribution License ("CDDL"), version 1.0.
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * You may only use this file in accordance with the terms of version
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * 1.0 of the CDDL.
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * A full copy of the text of the CDDL should have accompanied this
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * source. A copy of the CDDL is also available via the Internet at
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * String utility functions with dynamic memory management.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Copyright 2016 Joyent, Inc.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchitypedef enum {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * This function should never return NULL. If no buffer has
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * been allocated, return a pointer to a zero-length string.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchicustr_append_vprintf(custr_t *cus, const char *fmt, va_list ap)
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow if (len + cus->cus_strlen + 1 >= cus->cus_datalen) {
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow size_t new_datalen = cus->cus_datalen + chunksz;
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * Allocate replacement memory:
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow if ((new_data = malloc(new_datalen)) == NULL) {
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * Copy existing data into replacement memory and free
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * the old memory.
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * Swap in the replacement buffer:
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * Append new string to existing string:
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi len = vsnprintf(cus->cus_data + cus->cus_strlen,
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi (uintptr_t)cus->cus_data - (uintptr_t)cus->cus_strlen, fmt, ap);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (custr_append_printf(cus, "%c", newc));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchicustr_append_printf(custr_t *cus, const char *fmt, ...)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchicustr_append(custr_t *cus, const char *name)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (custr_append_printf(cus, "%s", name));
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow if ((t = calloc(1, sizeof (*t))) == NULL) {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchicustr_alloc_buf(custr_t **cus, void *buf, size_t buflen)