diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/ChangeLog | 6 | ||||
-rw-r--r-- | util/compat.c | 107 |
2 files changed, 113 insertions, 0 deletions
diff --git a/util/ChangeLog b/util/ChangeLog index 8c7d995c8..be3a4bf35 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,9 @@ +2009-08-25 Werner Koch <[email protected]> + + * compat.c: Change license to all-permissive. The GPL and the + exception does no make sense here. + (do_strconcat, xstrconcat): New. + 2009-08-18 Werner Koch <[email protected]> * iobuf.c (fd_cache_close): Change debug printf format assuming diff --git a/util/compat.c b/util/compat.c index cd487e5e2..0a6c944d0 100644 --- a/util/compat.c +++ b/util/compat.c @@ -1,5 +1,42 @@ +/* compat.c - Simple compatibility functions + * Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc. + * + * The origin of this code is GnuPG. + * + * This file is free software; as a special exception the author gives + * unlimited permission to copy and/or distribute it, with or without + * modifications, as long as this notice is preserved. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * History: + * 2006-09-28 dshaw Created. Added function hextobyte from GnuPG. + * 2007-04-16 dshaw Added ascii_toupper, ascii_tolower, ascii_strcasecmp, + * ascii_strncasecmp from GnuPG. + * 2009-08-25 wk License changed by GnuPG maintainer from GPL with + * OpenSSL exception to this all permissive license. + * 2009-08-25 wk Wrote new function xstrconcat. + */ + +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> #include <sys/types.h> +/* We require an external malloc function named xtrymalloc. */ +void *xtrymalloc (size_t n); + + +#ifndef DIM +#define DIM(v) (sizeof(v)/sizeof((v)[0])) +#endif + + + int hextobyte (const char *s) { @@ -92,3 +129,73 @@ ascii_strncasecmp (const char *a, const char *b, size_t n) return c1 - c2; } + + +static char * +do_strconcat (const char *s1, va_list arg_ptr) +{ + const char *argv[48]; + size_t argc; + size_t needed; + char *buffer, *p; + const char *r; + + 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) + { + errno = EINVAL; + return NULL; + } + argc++; + } + needed++; + buffer = xtrymalloc (needed); + if (buffer) + { + for (p = buffer, argc=0; argv[argc]; argc++) + { + for (r = argv[argc]; *r; ) + *p++ = *r++; + *p = 0; + } + } + return buffer; +} + + +/* Concatenate the string S1 with all the following strings up to a + NULL. Returns a malloced buffer. */ +char * +xstrconcat (const char *s1, ...) +{ + va_list arg_ptr; + char *result; + + if (!s1) + { + result = xtrymalloc (1); + if (result) + *result = 0; + } + else + { + va_start (arg_ptr, s1); + result = do_strconcat (s1, arg_ptr); + va_end (arg_ptr); + } + if (!result) + { + if (errno == EINVAL) + fputs ("\nfatal: too many args for xstrconcat\n", stderr); + else + fputs ("\nfatal: out of memory\n", stderr); + exit (2); + } + return result; +} + |