summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcustr
diff options
context:
space:
mode:
Diffstat (limited to 'usr/src/lib/libcustr')
-rw-r--r--usr/src/lib/libcustr/Makefile43
-rw-r--r--usr/src/lib/libcustr/Makefile.com38
-rw-r--r--usr/src/lib/libcustr/amd64/Makefile19
-rw-r--r--usr/src/lib/libcustr/common/custr.c202
-rw-r--r--usr/src/lib/libcustr/common/libcustr.h82
-rw-r--r--usr/src/lib/libcustr/common/llib-lcustr19
-rw-r--r--usr/src/lib/libcustr/common/mapfile-vers46
-rw-r--r--usr/src/lib/libcustr/i386/Makefile18
-rw-r--r--usr/src/lib/libcustr/sparc/Makefile18
-rw-r--r--usr/src/lib/libcustr/sparcv9/Makefile19
10 files changed, 504 insertions, 0 deletions
diff --git a/usr/src/lib/libcustr/Makefile b/usr/src/lib/libcustr/Makefile
new file mode 100644
index 0000000000..19b08dc521
--- /dev/null
+++ b/usr/src/lib/libcustr/Makefile
@@ -0,0 +1,43 @@
+#
+# 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.
+#
+
+#
+# Copyright 2018, Joyent, Inc.
+#
+
+include $(SRC)/lib/Makefile.lib
+
+HDRS = libcustr.h
+HDRDIR = common
+
+SUBDIRS = $(MACH)
+$(BUILD64)SUBDIRS += $(MACH64)
+
+all := TARGET= all
+clean := TARGET= clean
+clobber := TARGET= clobber
+install := TARGET= install
+lint := TARGET= lint
+
+.KEEP_STATE:
+
+all clean clobber install lint: $(SUBDIRS)
+
+install_h: $(ROOTHDRS)
+
+check: $(CHECKHDRS)
+
+$(SUBDIRS): FRC
+ @cd $@; pwd; $(MAKE) $(TARGET)
+
+FRC:
+
+include $(SRC)/lib/Makefile.targ
diff --git a/usr/src/lib/libcustr/Makefile.com b/usr/src/lib/libcustr/Makefile.com
new file mode 100644
index 0000000000..747026da89
--- /dev/null
+++ b/usr/src/lib/libcustr/Makefile.com
@@ -0,0 +1,38 @@
+#
+# 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.
+#
+
+#
+# Copyright 2018, Joyent, Inc.
+#
+
+LIBRARY = libcustr.a
+VERS = .1
+OBJECTS = custr.o
+
+include $(SRC)/lib/Makefile.lib
+
+# Things out of /sbin like dladm require custr, so it should go into /lib so
+# they can work in case /usr is split
+include $(SRC)/lib/Makefile.rootfs
+
+LIBS = $(DYNLIB) $(LINTLIB)
+LDLIBS += -lc
+CPPFLAGS += -D__EXTENSIONS__
+
+SRCDIR = ../common
+
+.KEEP_STATE:
+
+all: $(LIBS)
+
+lint: lintcheck
+
+include $(SRC)/lib/Makefile.targ
diff --git a/usr/src/lib/libcustr/amd64/Makefile b/usr/src/lib/libcustr/amd64/Makefile
new file mode 100644
index 0000000000..4b3c5dd187
--- /dev/null
+++ b/usr/src/lib/libcustr/amd64/Makefile
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+#
+# Copyright 2018, Joyent, Inc.
+#
+
+include ../Makefile.com
+include $(SRC)/lib/Makefile.lib.64
+
+install: all $(ROOTLIBS64) $(ROOTLINKS64)
diff --git a/usr/src/lib/libcustr/common/custr.c b/usr/src/lib/libcustr/common/custr.c
new file mode 100644
index 0000000000..429fedf8e9
--- /dev/null
+++ b/usr/src/lib/libcustr/common/custr.c
@@ -0,0 +1,202 @@
+/*
+ * 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 <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <sys/debug.h>
+
+#include "libcustr.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/libcustr/common/libcustr.h b/usr/src/lib/libcustr/common/libcustr.h
new file mode 100644
index 0000000000..7671390d7f
--- /dev/null
+++ b/usr/src/lib/libcustr/common/libcustr.h
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+
+/*
+ * Copyright 2018, Joyent, Inc.
+ */
+
+#ifndef _LIBCUSTR_H
+#define _LIBCUSTR_H
+
+#include <stdarg.h>
+#include <sys/types.h>
+
+/* dynamic string utilities */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct custr custr_t;
+
+/*
+ * Allocate and free a "custr_t" dynamic string object. Returns 0 on success
+ * and -1 otherwise.
+ */
+int custr_alloc(custr_t **);
+void custr_free(custr_t *);
+
+/*
+ * Allocate a "custr_t" dynamic string object that operates on a fixed external
+ * buffer.
+ */
+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.
+ */
+int custr_appendc(custr_t *, char);
+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.
+ */
+int custr_append_printf(custr_t *, const char *, ...);
+int custr_append_vprintf(custr_t *, const char *, va_list);
+
+/*
+ * Determine the length in bytes, not including the NUL terminator, of the
+ * dynamic string.
+ */
+size_t custr_len(custr_t *);
+
+/*
+ * Clear the contents of a dynamic string. Does not free the underlying
+ * memory.
+ */
+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.
+ */
+const char *custr_cstr(custr_t *str);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LIBCUSTR_H */
diff --git a/usr/src/lib/libcustr/common/llib-lcustr b/usr/src/lib/libcustr/common/llib-lcustr
new file mode 100644
index 0000000000..5c08550594
--- /dev/null
+++ b/usr/src/lib/libcustr/common/llib-lcustr
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+
+/*
+ * Copyright 2018, Joyent, Inc.
+ */
+
+/*LINTLIBRARY*/
+/*PROTOLIB1*/
+
+#include <libcustr.h>
diff --git a/usr/src/lib/libcustr/common/mapfile-vers b/usr/src/lib/libcustr/common/mapfile-vers
new file mode 100644
index 0000000000..369771929a
--- /dev/null
+++ b/usr/src/lib/libcustr/common/mapfile-vers
@@ -0,0 +1,46 @@
+#
+# 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.
+#
+
+#
+# Copyright 2018, Joyent, Inc.
+#
+
+#
+# MAPFILE HEADER START
+#
+# WARNING: STOP NOW. DO NOT MODIFY THIS FILE.
+# Object versioning must comply with the rules detailed in
+#
+# usr/src/lib/README.mapfiles
+#
+# You should not be making modifications here until you've read the most current
+# copy of that file. If you need help, contact a gatekeeper for guidance.
+#
+# MAPFILE HEADER END
+#
+
+$mapfile_version 2
+
+SYMBOL_VERSION ILLUMOSprivate {
+ global:
+ custr_alloc;
+ custr_alloc_buf;
+ custr_append;
+ custr_appendc;
+ custr_append_printf;
+ custr_append_vprintf;
+ custr_cstr;
+ custr_free;
+ custr_len;
+ custr_reset;
+ local:
+ *;
+};
diff --git a/usr/src/lib/libcustr/i386/Makefile b/usr/src/lib/libcustr/i386/Makefile
new file mode 100644
index 0000000000..f32d7a5b91
--- /dev/null
+++ b/usr/src/lib/libcustr/i386/Makefile
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+#
+# Copyright 2018, Joyent, Inc.
+#
+
+include ../Makefile.com
+
+install: all $(ROOTLIBS) $(ROOTLINKS) $(ROOTLINT)
diff --git a/usr/src/lib/libcustr/sparc/Makefile b/usr/src/lib/libcustr/sparc/Makefile
new file mode 100644
index 0000000000..f32d7a5b91
--- /dev/null
+++ b/usr/src/lib/libcustr/sparc/Makefile
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+#
+# Copyright 2018, Joyent, Inc.
+#
+
+include ../Makefile.com
+
+install: all $(ROOTLIBS) $(ROOTLINKS) $(ROOTLINT)
diff --git a/usr/src/lib/libcustr/sparcv9/Makefile b/usr/src/lib/libcustr/sparcv9/Makefile
new file mode 100644
index 0000000000..4b3c5dd187
--- /dev/null
+++ b/usr/src/lib/libcustr/sparcv9/Makefile
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+#
+# Copyright 2018, Joyent, Inc.
+#
+
+include ../Makefile.com
+include $(SRC)/lib/Makefile.lib.64
+
+install: all $(ROOTLIBS64) $(ROOTLINKS64)