diff options
author | Werner Koch <wk@gnupg.org> | 2017-11-17 11:26:03 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2017-11-17 11:26:03 +0100 |
commit | 0d8d46c76a32176be440b062d2501bbb044fb99d (patch) | |
tree | 7033aa06cf73e40bd8360a8058262bd61d9c0d4f | |
parent | 80c18e1b212cc91946864db7a53da50e9f91b861 (diff) | |
download | libgpg-error-0d8d46c76a32176be440b062d2501bbb044fb99d.tar.gz |
core: New API functions gpgrt_strdup and gpgrt_strconcat.
* src/visibility.c (gpgrt_strdup): New API fucntion.
(gpgrt_strconcat): New API fucntion.
* src/visibility.h: Add corresponding macros.
* src/gpg-error.def.in: Add them.
* src/gpg-error.vers: Add them.
* src/gpgrt-int.h (DIM): New macro.
* src/init.c (_gpgrt_strdup): New.
(_gpgrt_strconcat_core): New.
(_gpgrt_strconcat): New.
--
Signed-off-by: Werner Koch <wk@gnupg.org>
-rw-r--r-- | src/gpg-error.def.in | 2 | ||||
-rw-r--r-- | src/gpg-error.h.in | 2 | ||||
-rw-r--r-- | src/gpg-error.vers | 2 | ||||
-rw-r--r-- | src/gpgrt-int.h | 14 | ||||
-rw-r--r-- | src/init.c | 64 | ||||
-rw-r--r-- | src/visibility.c | 23 | ||||
-rw-r--r-- | src/visibility.h | 6 |
7 files changed, 111 insertions, 2 deletions
diff --git a/src/gpg-error.def.in b/src/gpg-error.def.in index a9cfed9..7c39502 100644 --- a/src/gpg-error.def.in +++ b/src/gpg-error.def.in @@ -186,5 +186,7 @@ EXPORTS gpgrt_realloc @141 gpgrt_malloc @142 gpgrt_calloc @143 + gpgrt_strdup @144 + gpgrt_strconcat @145 ;; end of file with public symbols for Windows. diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 3e7d59b..66e8d08 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -440,6 +440,8 @@ gpg_error_from_syserror (void) void *gpgrt_realloc (void *a, size_t n); void *gpgrt_malloc (size_t n); void *gpgrt_calloc (size_t n, size_t m); +char *gpgrt_strdup (const char *string); +char *gpgrt_strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0); void gpgrt_free (void *a); diff --git a/src/gpg-error.vers b/src/gpg-error.vers index 6276758..074981b 100644 --- a/src/gpg-error.vers +++ b/src/gpg-error.vers @@ -160,6 +160,8 @@ GPG_ERROR_1.0 { gpgrt_realloc; gpgrt_malloc; gpgrt_calloc; + gpgrt_strdup; + gpgrt_strconcat; local: *; diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index 4db330f..c226e2b 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -85,6 +85,14 @@ #endif /*_GPGRT_NEED_AFLOCAL*/ +/* + * Common helper macros. + */ +#ifndef DIM +# define DIM(array) (sizeof (array) / sizeof (*array)) +#endif + + /* * Local error function prototypes. @@ -103,8 +111,12 @@ void _gpgrt_set_alloc_func (void *(*f)(void *a, size_t n)); void *_gpgrt_realloc (void *a, size_t n); void *_gpgrt_malloc (size_t n); -void _gpgrt_free (void *a); void *_gpgrt_calloc (size_t n, size_t m); +char *_gpgrt_strdup (const char *string); +char *_gpgrt_strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0); +void _gpgrt_free (void *a); +/* The next is only to be used by visibility.c. */ +char *_gpgrt_strconcat_core (const char *s1, va_list arg_ptr); const char *_gpg_error_check_version (const char *req_version); @@ -215,6 +215,70 @@ _gpgrt_calloc (size_t n, size_t m) } +char * +_gpgrt_strdup (const char *string) +{ + size_t len = strlen (string); + char *p; + + p = _gpgrt_realloc (NULL, len + 1); + if (p) + strcpy (p, string); + return p; +} + + +/* Helper for _gpgrt_stdconcat and gpgrt_strconcat. */ +char * +_gpgrt_strconcat_core (const char *s1, va_list arg_ptr) +{ + const char *argv[48]; + size_t argc; + size_t needed; + char *buffer, *p; + + argc = 0; + argv[argc++] = s1; + needed = strlen (s1); + while (((argv[argc] = va_arg (arg_ptr, const char *)))) + { + needed += strlen (argv[argc]); + if (argc >= DIM (argv)-1) + { + _gpg_err_set_errno (EINVAL); + return NULL; + } + argc++; + } + needed++; + buffer = _gpgrt_malloc (needed); + if (buffer) + { + for (p = buffer, argc=0; argv[argc]; argc++) + p = stpcpy (p, argv[argc]); + } + return buffer; +} + + +char * +_gpgrt_strconcat (const char *s1, ...) +{ + va_list arg_ptr; + char *result; + + if (!s1) + result = _gpgrt_strdup (""); + else + { + va_start (arg_ptr, s1); + result = _gpgrt_strconcat_core (s1, arg_ptr); + va_end (arg_ptr); + } + return result; +} + + /* The free to be used for data returned by the public API. */ void _gpgrt_free (void *a) diff --git a/src/visibility.c b/src/visibility.c index 6172769..888492a 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -522,6 +522,29 @@ gpgrt_calloc (size_t n, size_t m) return _gpgrt_calloc (n, m); } +char * +gpgrt_strdup (const char *string) +{ + return _gpgrt_strdup (string); +} + +char * +gpgrt_strconcat (const char *s1, ...) +{ + va_list arg_ptr; + char *result; + + if (!s1) + result = _gpgrt_strdup (""); + else + { + va_start (arg_ptr, s1); + result = _gpgrt_strconcat_core (s1, arg_ptr); + va_end (arg_ptr); + } + return result; +} + void gpgrt_free (void *a) { diff --git a/src/visibility.h b/src/visibility.h index 596a6f2..c4f4fd1 100644 --- a/src/visibility.h +++ b/src/visibility.h @@ -121,6 +121,8 @@ MARK_VISIBLE (gpgrt_read_line) MARK_VISIBLE (gpgrt_realloc) MARK_VISIBLE (gpgrt_malloc) MARK_VISIBLE (gpgrt_calloc) +MARK_VISIBLE (gpgrt_strdup) +MARK_VISIBLE (gpgrt_strconcat) MARK_VISIBLE (gpgrt_free) MARK_VISIBLE (gpgrt_fprintf) MARK_VISIBLE (gpgrt_fprintf_unlocked) @@ -262,7 +264,9 @@ MARK_VISIBLE (_gpgrt_log_assert) #define gpgrt_read_line _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_realloc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_malloc _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_calloc _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_calloc _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_strdup _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_strconcat _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_free _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION |