Skip to content

Instantly share code, notes, and snippets.

@jclulow
Created September 18, 2019 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclulow/e04cc61dfaf11404f06e0b0f17a775b4 to your computer and use it in GitHub Desktop.
Save jclulow/e04cc61dfaf11404f06e0b0f17a775b4 to your computer and use it in GitHub Desktop.
commit 5b829fb7938a96390e89e4fa81c8fcd486b44025
Author: Joshua M. Clulow <jmc@joyent.com>
Date: Sun May 6 05:40:26 2018 +0000
custr: fixes for Linux
diff --git a/deps/strings/custr.c b/deps/strings/custr.c
index 03a229b..50d5ab7 100644
--- a/deps/strings/custr.c
+++ b/deps/strings/custr.c
@@ -72,34 +72,36 @@ custr_cstr(custr_t *cus)
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);
}
static int
custr_append_vprintf(custr_t *cus, const char *fmt, va_list ap)
{
- int len = vsnprintf(NULL, 0, fmt, ap);
+ va_list tap;
+ int len;
size_t chunksz = STRING_CHUNK_SIZE;
- if (len < 0) {
+ va_copy(tap, ap);
+ if ((len = vsnprintf(NULL, 0, fmt, tap)) < 0) {
return (-1);
}
while (chunksz < (size_t)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);
}
@@ -115,30 +117,31 @@ custr_append_vprintf(custr_t *cus, const char *fmt, va_list ap)
* 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)
{
diff --git a/deps/strings/custr.h b/deps/strings/custr.h
index 5a54924..147f4c9 100644
--- a/deps/strings/custr.h
+++ b/deps/strings/custr.h
@@ -8,30 +8,31 @@
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright (c) 2017, Joyent, Inc.
*/
#ifndef _CUSTR_H
#define _CUSTR_H
/*
* Private copy of custr*() utilities from libcmdutils.
*/
+#include <stdint.h>
#include <stddef.h>
#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.
*/
extern int custr_alloc(custr_t **);
extern void custr_free(custr_t *);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment