custr.c revision 196c7f05d2deba7404e90ad67f3861185c78ca2d
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.
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow * Copyright 2014, Joyent, Inc.
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulowcustr_append(custr_t *cus, const char *news)
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:
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow (void) memcpy(cus->cus_data + cus->cus_strlen, news, len + 1);
196c7f05d2deba7404e90ad67f3861185c78ca2dJoshua M. Clulow if ((t = calloc(1, sizeof (*t))) == NULL) {