diff options
| author | Jason King <jason.king@joyent.com> | 2018-02-16 16:21:53 +0000 |
|---|---|---|
| committer | Jason King <jason.king@joyent.com> | 2018-08-15 23:06:03 +0000 |
| commit | 8cbcb396d3e69d6050674883de9feed1716fc2f5 (patch) | |
| tree | 71638c1ba639685df66811eb2c9c490c8ff3f5be /usr/src/lib/libcmdutils | |
| parent | 005d090d5d39829784302a3c8574b81b88fc69d0 (diff) | |
| download | illumos-joyent-custr.tar.gz | |
OS-6920 Split the custr functions into their own librarycustr
Diffstat (limited to 'usr/src/lib/libcmdutils')
| -rw-r--r-- | usr/src/lib/libcmdutils/Makefile.com | 3 | ||||
| -rw-r--r-- | usr/src/lib/libcmdutils/common/custr.c | 201 | ||||
| -rw-r--r-- | usr/src/lib/libcmdutils/common/mapfile-vers | 21 | ||||
| -rw-r--r-- | usr/src/lib/libcmdutils/libcmdutils.h | 56 |
4 files changed, 14 insertions, 267 deletions
diff --git a/usr/src/lib/libcmdutils/Makefile.com b/usr/src/lib/libcmdutils/Makefile.com index 9cb35f8795..72e7330eba 100644 --- a/usr/src/lib/libcmdutils/Makefile.com +++ b/usr/src/lib/libcmdutils/Makefile.com @@ -21,12 +21,13 @@ # # Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. # Copyright (c) 2013 RackTop Systems. +# Copyright 2018, Joyent, Inc. # LIBRARY= libcmdutils.a VERS= .1 CMD_OBJS= avltree.o sysattrs.o writefile.o process_xattrs.o uid.o gid.o \ - custr.o nicenum.o + nicenum.o COM_OBJS= list.o OBJECTS= $(CMD_OBJS) $(COM_OBJS) diff --git a/usr/src/lib/libcmdutils/common/custr.c b/usr/src/lib/libcmdutils/common/custr.c deleted file mode 100644 index 95d44e431f..0000000000 --- a/usr/src/lib/libcmdutils/common/custr.c +++ /dev/null @@ -1,201 +0,0 @@ -/* - * This file and its contents are supplied under the terms of the - * Common Development and Distribution License ("CDDL"), version 1.0. - * You may only use this file in accordance with the terms of version - * 1.0 of the CDDL. - * - * A full copy of the text of the CDDL should have accompanied this - * source. A copy of the CDDL is also available via the Internet at - * http://www.illumos.org/license/CDDL. - */ - -/* - * String utility functions with dynamic memory management. - */ - -/* - * Copyright 2016 Joyent, Inc. - */ - -#include <stdlib.h> -#include <err.h> -#include <string.h> -#include <stdio.h> -#include <stdarg.h> -#include <sys/debug.h> - -#include "libcmdutils.h" - -typedef enum { - CUSTR_FIXEDBUF = 0x01 -} custr_flags_t; - -struct custr { - size_t cus_strlen; - size_t cus_datalen; - char *cus_data; - custr_flags_t cus_flags; -}; - -#define STRING_CHUNK_SIZE 64 - -void -custr_reset(custr_t *cus) -{ - if (cus->cus_data == NULL) - return; - - cus->cus_strlen = 0; - cus->cus_data[0] = '\0'; -} - -size_t -custr_len(custr_t *cus) -{ - return (cus->cus_strlen); -} - -const char * -custr_cstr(custr_t *cus) -{ - if (cus->cus_data == NULL) { - VERIFY(cus->cus_strlen == 0); - VERIFY(cus->cus_datalen == 0); - - /* - * This function should never return NULL. If no buffer has - * been allocated, return a pointer to a zero-length string. - */ - return (""); - } - return (cus->cus_data); -} - -int -custr_append_vprintf(custr_t *cus, const char *fmt, va_list ap) -{ - int len = vsnprintf(NULL, 0, fmt, ap); - size_t chunksz = STRING_CHUNK_SIZE; - - if (len == -1) - return (len); - - while (chunksz < len) { - chunksz *= 2; - } - - if (len + cus->cus_strlen + 1 >= cus->cus_datalen) { - char *new_data; - size_t new_datalen = cus->cus_datalen + chunksz; - - if (cus->cus_flags & CUSTR_FIXEDBUF) { - errno = EOVERFLOW; - return (-1); - } - - /* - * Allocate replacement memory: - */ - if ((new_data = malloc(new_datalen)) == NULL) { - return (-1); - } - - /* - * Copy existing data into replacement memory and free - * the old memory. - */ - if (cus->cus_data != NULL) { - (void) memcpy(new_data, cus->cus_data, - cus->cus_strlen + 1); - free(cus->cus_data); - } - - /* - * Swap in the replacement buffer: - */ - cus->cus_data = new_data; - cus->cus_datalen = new_datalen; - } - /* - * Append new string to existing string: - */ - len = vsnprintf(cus->cus_data + cus->cus_strlen, - (uintptr_t)cus->cus_data - (uintptr_t)cus->cus_strlen, fmt, ap); - if (len == -1) - return (len); - cus->cus_strlen += len; - - return (0); -} - -int -custr_appendc(custr_t *cus, char newc) -{ - return (custr_append_printf(cus, "%c", newc)); -} - -int -custr_append_printf(custr_t *cus, const char *fmt, ...) -{ - va_list ap; - int ret; - - va_start(ap, fmt); - ret = custr_append_vprintf(cus, fmt, ap); - va_end(ap); - - return (ret); -} - -int -custr_append(custr_t *cus, const char *name) -{ - return (custr_append_printf(cus, "%s", name)); -} - -int -custr_alloc(custr_t **cus) -{ - custr_t *t; - - if ((t = calloc(1, sizeof (*t))) == NULL) { - *cus = NULL; - return (-1); - } - - *cus = t; - return (0); -} - -int -custr_alloc_buf(custr_t **cus, void *buf, size_t buflen) -{ - int ret; - - if (buflen == 0 || buf == NULL) { - errno = EINVAL; - return (-1); - } - - if ((ret = custr_alloc(cus)) != 0) - return (ret); - - (*cus)->cus_data = buf; - (*cus)->cus_datalen = buflen; - (*cus)->cus_strlen = 0; - (*cus)->cus_flags = CUSTR_FIXEDBUF; - (*cus)->cus_data[0] = '\0'; - - return (0); -} - -void -custr_free(custr_t *cus) -{ - if (cus == NULL) - return; - - if ((cus->cus_flags & CUSTR_FIXEDBUF) == 0) - free(cus->cus_data); - free(cus); -} diff --git a/usr/src/lib/libcmdutils/common/mapfile-vers b/usr/src/lib/libcmdutils/common/mapfile-vers index 0ac77eb010..2a3e95f95c 100644 --- a/usr/src/lib/libcmdutils/common/mapfile-vers +++ b/usr/src/lib/libcmdutils/common/mapfile-vers @@ -21,6 +21,7 @@ # # Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. # Copyright (c) 2013 RackTop Systems. +# Copyright 2018, Joyent, Inc. # # @@ -42,16 +43,16 @@ $mapfile_version 2 SYMBOL_VERSION SUNWprivate_1.1 { global: add_tnode; - custr_alloc; - custr_alloc_buf; - custr_append; - custr_appendc; - custr_append_printf; - custr_append_vprintf; - custr_cstr; - custr_free; - custr_len; - custr_reset; + custr_alloc { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_alloc_buf { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_append { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_appendc { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_append_printf { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_append_vprintf { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_cstr { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_free { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_len { TYPE = FUNCTION; FILTER = libcustr.so.1 }; + custr_reset { TYPE = FUNCTION; FILTER = libcustr.so.1 }; destroy_tree; findnextgid; findnextuid; diff --git a/usr/src/lib/libcmdutils/libcmdutils.h b/usr/src/lib/libcmdutils/libcmdutils.h index 5f9957b861..c9a61aab4d 100644 --- a/usr/src/lib/libcmdutils/libcmdutils.h +++ b/usr/src/lib/libcmdutils/libcmdutils.h @@ -26,7 +26,7 @@ * Copyright (c) 2013 RackTop Systems. */ /* - * Copyright 2017 Joyent, Inc. + * Copyright 2018 Joyent, Inc. */ /* @@ -162,60 +162,6 @@ extern int findnextuid(uid_t, uid_t, uid_t *); */ extern int findnextgid(gid_t, gid_t, gid_t *); - - - /* dynamic string utilities */ - -typedef struct custr custr_t; - -/* - * Allocate and free a "custr_t" dynamic string object. Returns 0 on success - * and -1 otherwise. - */ -extern int custr_alloc(custr_t **); -extern void custr_free(custr_t *); - -/* - * Allocate a "custr_t" dynamic string object that operates on a fixed external - * buffer. - */ -extern int custr_alloc_buf(custr_t **, void *, size_t); - -/* - * Append a single character, or a NUL-terminated string of characters, to a - * dynamic string. Returns 0 on success and -1 otherwise. The dynamic string - * will be unmodified if the function returns -1. - */ -extern int custr_appendc(custr_t *, char); -extern int custr_append(custr_t *, const char *); - -/* - * Append a format string and arguments as though the contents were being parsed - * through snprintf. Returns 0 on success and -1 otherwise. The dynamic string - * will be unmodified if the function returns -1. - */ -extern int custr_append_printf(custr_t *, const char *, ...); -extern int custr_append_vprintf(custr_t *, const char *, va_list); - -/* - * Determine the length in bytes, not including the NUL terminator, of the - * dynamic string. - */ -extern size_t custr_len(custr_t *); - -/* - * Clear the contents of a dynamic string. Does not free the underlying - * memory. - */ -extern void custr_reset(custr_t *); - -/* - * Retrieve a const pointer to a NUL-terminated string version of the contents - * of the dynamic string. Storage for this string should not be freed, and - * the pointer will be invalidated by any mutations to the dynamic string. - */ -extern const char *custr_cstr(custr_t *str); - #define NN_DIVISOR_1000 (1U << 0) /* Minimum size for the output of nicenum, including NULL */ |
