Add some more functions for -liberty and make the library WARNS=6 clean.
authorJoerg Sonnenberger <joerg@dragonflybsd.org>
Sat, 23 Oct 2004 12:15:21 +0000 (12:15 +0000)
committerJoerg Sonnenberger <joerg@dragonflybsd.org>
Sat, 23 Oct 2004 12:15:21 +0000 (12:15 +0000)
As replacement of the missing documentation:
- buildargv, freeargv and dupargv help with managing argv lists as NULL
  terminated lists of strings
- hex_p and hex_value help with the detection and evaluation of hex digests,
  they might be replaced by an inline version later
- concat and reconcat can be used to concat an arbitrary number of strings
- xatexit, xexit and xstrerror just call the corresponding normal functions,
  they already provide the GNU functionality
- xcalloc, xmalloc, xmemdup, xrealloc and xstrdup work as expected but
  terminate the program if the memory allocation can not be fullfilled

15 files changed:
include/libiberty.h
lib/libiberty/Makefile
lib/libiberty/argv.c [new file with mode: 0644]
lib/libiberty/concat.c [copied from lib/libiberty/lrealpath.c with 53% similarity]
lib/libiberty/hex.c [new file with mode: 0644]
lib/libiberty/lbasename.c
lib/libiberty/lrealpath.c
lib/libiberty/xatexit.c [copied from lib/libiberty/lbasename.c with 85% similarity]
lib/libiberty/xcalloc.c [copied from lib/libiberty/lbasename.c with 82% similarity]
lib/libiberty/xexit.c [copied from lib/libiberty/lbasename.c with 85% similarity]
lib/libiberty/xmalloc.c [copied from lib/libiberty/lbasename.c with 83% similarity]
lib/libiberty/xmemdup.c [copied from lib/libiberty/lbasename.c with 83% similarity]
lib/libiberty/xrealloc.c [copied from lib/libiberty/lbasename.c with 82% similarity]
lib/libiberty/xstrdup.c [copied from lib/libiberty/lbasename.c with 83% similarity]
lib/libiberty/xstrerror.c [copied from lib/libiberty/lbasename.c with 87% similarity]

index 2e8d178..29fd9ca 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/include/libiberty.h,v 1.2 2004/06/09 12:32:07 joerg Exp $
+ * $DragonFly: src/include/libiberty.h,v 1.3 2004/10/23 12:15:21 joerg Exp $
  */
 
 #ifndef _LIBIBERTY_H_
 #define _LIBIBERTY_H_
 
 #include <sys/cdefs.h>
+#include <sys/stdint.h>
+
+#ifndef _SIZE_T_DECLARED
+#define _SIZE_T_DECLARED
+typedef __size_t       size_t;
+#endif
 
 __BEGIN_DECLS
+char **                buildargv(const char *);
+void           freeargv(char **);
+char **                dupargv(char * const *);
+
+void           hex_init(void);
+int            hex_p(int);
+unsigned int   hex_value(int);
+
+char *         concat(const char *, ...);
+char *         reconcat(char *, ...);
 const char *   lbasename(const char *);
 char *         lrealpath(const char *);
+int            xatexit(void (*)(void));
+void *         xcalloc(size_t, size_t);
+void           xexit(int);
+void *         xmalloc(size_t);
+void *         xmemdup(const void *, size_t, size_t);
+void *         xrealloc(void *, size_t);
+char *         xstrdup(const char *);
+char *         xstrerror(int error);
 __END_DECLS
 
 #endif
index 5b43c23..62129de 100644 (file)
@@ -1,8 +1,12 @@
-# $DragonFly: src/lib/libiberty/Makefile,v 1.2 2004/06/09 12:32:07 joerg Exp $
+# $DragonFly: src/lib/libiberty/Makefile,v 1.3 2004/10/23 12:15:21 joerg Exp $
 
 LIB=           iberty
 SHLIB_MAJOR=   1
-SRCS=          lbasename.c lrealpath.c
+SRCS=          argv.c concat.c hex.c
+SRCS+=         lbasename.c lrealpath.c
+SRCS+=         xatexit.c xexit.c
+SRCS+=         xcalloc.c xmalloc.c xmemdup.c xrealloc.c xstrdup.c xstrerror.c
+WARNS?=                6
 
 NOMAN=1
 
diff --git a/lib/libiberty/argv.c b/lib/libiberty/argv.c
new file mode 100644 (file)
index 0000000..708851c
--- /dev/null
@@ -0,0 +1,213 @@
+/*
+ * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $DragonFly: src/lib/libiberty/argv.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
+ */
+
+#include <assert.h>
+#include <libiberty.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define IS_SEP(x) ((x) == ' ' || (x) == '\t')
+
+/*
+ * Return the first character of the next word.
+ * len is the word len after quoting and escaping has been removed.
+ */
+static const char *
+find_next_word(const char *arg, size_t *len)
+{
+       enum {NOQUOTE, SQUOTE, DQUOTE} in_quote = NOQUOTE;
+
+       *len = 0;
+
+       while (*arg != '\0') {
+               if (IS_SEP(*arg) && in_quote == NOQUOTE) {
+                       break;
+               } else if (*arg == '\\') {
+                       arg++;
+                       if (*arg == '\0')
+                               break;
+                       (*len)++;
+               } else if (*arg == '"') {
+                       if (in_quote == NOQUOTE)
+                               in_quote = DQUOTE;
+                       else if (in_quote == DQUOTE)
+                               in_quote = NOQUOTE;                             
+                       else
+                               (*len)++;                               
+               } else if (*arg == '\'') {
+                       if (in_quote == NOQUOTE)
+                               in_quote = SQUOTE;
+                       else if (in_quote == SQUOTE)
+                               in_quote = NOQUOTE;                             
+                       else
+                               (*len)++;
+               } else {
+                       (*len)++;
+               }
+               arg++;
+       }
+       return(arg);
+}
+
+static char *
+copy_word(const char *arg, const char *end, size_t len)
+{
+       char *buf, *buf_begin;
+       enum {NOQUOTE, SQUOTE, DQUOTE} in_quote = NOQUOTE;
+
+       assert(arg < end);
+
+       buf_begin = buf = malloc(len + 1);
+
+       for (; arg < end; arg++) {
+               if (*arg == '\\') {
+                       arg++;
+                       if (arg >= end)
+                               break;
+                       *buf++ = *arg;
+               } else if (*arg == '"') {
+                       if (in_quote == NOQUOTE)
+                               in_quote = DQUOTE;
+                       else if (in_quote == DQUOTE)
+                               in_quote = NOQUOTE;                             
+                       else
+                               *buf++ = *arg;
+               } else if (*arg == '\'') {
+                       if (in_quote == NOQUOTE)
+                               in_quote = SQUOTE;
+                       else if (in_quote == SQUOTE)
+                               in_quote = NOQUOTE;                             
+                       else
+                               *buf++ = *arg;
+               } else {
+                       *buf++ = *arg;
+               }
+       }
+       *buf = '\0';
+       return(buf_begin);
+}
+
+char **
+buildargv(const char *arg)
+{
+       void *tmp;
+       const char *begin_arg;
+       char **argv;
+       int args;
+       size_t len;
+
+       if (arg == NULL)
+               return(NULL);
+
+       args = 0;
+       argv = malloc(sizeof(char *));
+       if (argv == NULL)
+               return(NULL);
+       argv[0] = NULL;
+
+       while (*arg != '\0') {
+               /* Skip leading white space. */
+               while (IS_SEP(*arg))
+                       arg++;
+               if (*arg == '\0')
+                       break;
+
+               begin_arg = arg;
+               arg = find_next_word(arg, &len);
+
+               tmp = realloc(argv, (args + 2) * sizeof(char *));
+               if (tmp == NULL)
+                       goto error;
+               argv = tmp;
+
+               argv[args] = copy_word(begin_arg, arg, len);
+               if (argv[args] == NULL)
+                       goto error;
+               args++;
+               argv[args] = NULL;
+       }
+
+       /*
+        * The argument might be only white space, in that case,
+        * an empty argument list should be returned.
+        */
+       if (args == 0) {
+               tmp = realloc(argv, 2 * sizeof(char *));
+               if (tmp == NULL)
+                       goto error;
+               argv = tmp;
+
+               argv[0] = strdup("");
+               if (argv[0] == NULL)
+                       goto error;
+               argv[1] = NULL;
+       }
+       return(argv);
+error:
+       freeargv(argv);
+       return(NULL);
+}
+
+void
+freeargv(char **argv)
+{
+       char **orig_argv;
+
+       if (argv == NULL)
+               return;
+
+       for (orig_argv = argv; *argv != NULL; argv++)
+               free(*argv);
+
+       free(orig_argv);        
+}
+
+char **
+dupargv(char * const *argv)
+{
+       char * const *orig_argv;
+       char **new_argv, **new_argv2;
+       size_t len;
+
+       orig_argv = argv;
+       for (len = 0; *argv != NULL; argv++)
+               len++;
+
+       new_argv = malloc((len+1) * sizeof(char *));
+
+       new_argv2 = new_argv;
+       for (; orig_argv != NULL; orig_argv++, new_argv++) {
+               *new_argv = strdup(*orig_argv);
+               if (*new_argv == NULL) {
+                       freeargv(new_argv2);
+                       return(NULL);
+               }
+       }
+       *new_argv = NULL;
+
+       return(new_argv2);
+}
similarity index 53%
copy from lib/libiberty/lrealpath.c
copy to lib/libiberty/concat.c
index 2a92f6d..67720ea 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lrealpath.c,v 1.1 2004/06/09 12:32:07 joerg Exp $
+ * $DragonFly: src/lib/libiberty/concat.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
-#include <sys/param.h>
-#include <stdlib.h>
+#include <err.h>
+#include <libiberty.h>
 #include <string.h>
+#include <stdarg.h>
+#include <stdlib.h>
+
+static size_t
+concat_strlen(const char *str1, va_list ap)
+{
+       size_t len, new_len;
+
+       len = strlen(str1);
+
+       while ((str1 = va_arg(ap, const char *)) != NULL) {
+               new_len = len + strlen(str1);
+               if (new_len < len)
+                       errx(EXIT_FAILURE, "overflow in %s", __func__);
+               len = new_len;
+       }
+       return(len);
+}
+
+static void
+concat_copy(char *buf, const char *str1, va_list ap)
+{
+       size_t len;
+
+       do {
+           len = strlen(str1);
+           memcpy(buf, str1, len);
+           buf += len;
+       } while ((str1 = va_arg(ap, const char *)) != NULL);
+
+       *buf = '\0';
+}
 
 char *
-lrealpath(const char *pathname)
+concat(const char *str1, ...)
 {
-       char resolved_path[MAXPATHLEN];
+       va_list ap;
+       size_t len;
+       char *str;
+
+       if (str1 == NULL)
+               return(xstrdup(""));
+
+       va_start(ap, str1);
+       len = concat_strlen(str1, ap);
+       va_end(ap);
+
+       str = xmalloc(len + 1);
+       va_start(ap, str1);
+       concat_copy(str, str1, ap);
+       va_end(ap);     
+
+       return(str);
+}
+
+char *
+reconcat(char *str1, ...)
+{
+       va_list ap;
+       size_t len;
+       char *str;
+
+       va_start(ap, str1);
+       len = concat_strlen("", ap);
+       va_end(ap);
+
+       str = xmalloc(len + 1);
+       va_start(ap, str1);
+       concat_copy(str, "", ap);
+       va_end(ap);     
+
+       free(str1);
 
-       if (realpath(pathname, resolved_path) == NULL)
-               return(strdup(resolved_path));
-       else
-               return(strdup(pathname));
+       return(str);
 }
diff --git a/lib/libiberty/hex.c b/lib/libiberty/hex.c
new file mode 100644 (file)
index 0000000..0398712
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $DragonFly: src/lib/libiberty/hex.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
+ */
+
+#include <libiberty.h>
+
+static const char hex_table[128] = {
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   1,    2,    3,    4,    5,    6,    7,    8, 
+   9,   10,    0,    0,    0,    0,    0,    0, 
+   0,   11,   12,   13,   14,   15,   16,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,   11,   12,   13,   14,   15,   16,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0, 
+   0,    0,    0,    0,    0,    0,    0,    0
+};
+#define HEX_TABLE_INITIALZED
+
+#ifndef HEX_TABLE_INITIALZED
+static char    hex_table[128];
+#endif
+
+void
+hex_init(void)
+{
+#ifndef HEX_TABLE_INITIALZED
+       hex_table['0'] = 1;
+       hex_table['1'] = 2;
+       hex_table['2'] = 3;
+       hex_table['3'] = 4;
+       hex_table['4'] = 5;
+       hex_table['5'] = 6;
+       hex_table['6'] = 7;
+       hex_table['7'] = 8;
+       hex_table['8'] = 9;
+       hex_table['9'] = 10;
+       hex_table['a'] = 11;
+       hex_table['b'] = 12;
+       hex_table['c'] = 13;
+       hex_table['d'] = 14;
+       hex_table['e'] = 15;
+       hex_table['f'] = 16;
+       hex_table['a'] = 11;
+       hex_table['b'] = 12;
+       hex_table['c'] = 13;
+       hex_table['d'] = 14;
+       hex_table['e'] = 15;
+       hex_table['f'] = 16;
+       hex_table['A'] = 11;
+       hex_table['B'] = 12;
+       hex_table['C'] = 13;
+       hex_table['D'] = 14;
+       hex_table['E'] = 15;
+       hex_table['F'] = 16;
+#endif
+}
+
+int
+hex_p(int c)
+{
+       if ((c & ~127) != 0)
+               return(0);
+       return(hex_table[c]);
+}
+
+unsigned int
+hex_value(int c)
+{
+       if ((c & ~127) != 0)
+               return(0);
+       return(hex_table[c] - 1);
+}
index c779e98..240dc05 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/lbasename.c,v 1.3 2004/10/23 12:15:21 joerg Exp $
  */
 
+#include <libiberty.h>
 #include <string.h>
 
 const char *
@@ -32,7 +33,7 @@ lbasename(const char *path)
 {
        const char *b = strrchr(path, '/');
 
-       if (b)
+       if (b != NULL)
                return(b);
        else
                return(path);
index 2a92f6d..4cee063 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lrealpath.c,v 1.1 2004/06/09 12:32:07 joerg Exp $
+ * $DragonFly: src/lib/libiberty/lrealpath.c,v 1.2 2004/10/23 12:15:21 joerg Exp $
  */
 
 #include <sys/param.h>
+#include <libiberty.h>
 #include <stdlib.h>
 #include <string.h>
 
similarity index 85%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xatexit.c
index c779e98..e8ed7ed 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xatexit.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
-#include <string.h>
+#include <libiberty.h>
+#include <stdlib.h>
 
-const char *
-lbasename(const char *path)
+int
+xatexit(void (*function)(void))
 {
-       const char *b = strrchr(path, '/');
-
-       if (b)
-               return(b);
-       else
-               return(path);
+       return(atexit(function));
 }
similarity index 82%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xcalloc.c
index c779e98..48c1b23 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xcalloc.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
-#include <string.h>
+#include <err.h>
+#include <libiberty.h>
+#include <stdlib.h>
 
-const char *
-lbasename(const char *path)
+void *
+xcalloc(size_t number, size_t size)
 {
-       const char *b = strrchr(path, '/');
+       void *tmp;
 
-       if (b)
-               return(b);
-       else
-               return(path);
+       if ((tmp = calloc(number, size)) == NULL)
+               err(EXIT_FAILURE, "%s failed", __func__);
+       return(tmp);
 }
similarity index 85%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xexit.c
index c779e98..d8b7942 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xexit.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
-#include <string.h>
+#include <libiberty.h>
+#include <stdlib.h>
 
-const char *
-lbasename(const char *path)
+void
+xexit(int code)
 {
-       const char *b = strrchr(path, '/');
-
-       if (b)
-               return(b);
-       else
-               return(path);
+       exit(code);
 }
similarity index 83%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xmalloc.c
index c779e98..1f41d1f 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xmalloc.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
-#include <string.h>
+#include <err.h>
+#include <libiberty.h>
+#include <stdlib.h>
 
-const char *
-lbasename(const char *path)
+void *
+xmalloc(size_t size)
 {
-       const char *b = strrchr(path, '/');
+       void *tmp;
 
-       if (b)
-               return(b);
-       else
-               return(path);
+       if ((tmp = malloc(size)) == NULL)
+               err(EXIT_FAILURE, "%s failed", __func__);
+       return(tmp);
 }
similarity index 83%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xmemdup.c
index c779e98..fcb8943 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xmemdup.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
 #include <string.h>
+#include <libiberty.h>
 
-const char *
-lbasename(const char *path)
+void *
+xmemdup(const void *input, size_t input_size, size_t buffer_size)
 {
-       const char *b = strrchr(path, '/');
+       char *tmp;
 
-       if (b)
-               return(b);
-       else
-               return(path);
+       tmp = xcalloc(1, buffer_size);
+       memcpy(tmp, input, input_size);
+       return(tmp);
 }
similarity index 82%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xrealloc.c
index c779e98..85d7fa3 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xrealloc.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
-#include <string.h>
+#include <err.h>
+#include <libiberty.h>
+#include <stdlib.h>
 
-const char *
-lbasename(const char *path)
+void *
+xrealloc(void *ptr, size_t size)
 {
-       const char *b = strrchr(path, '/');
+       void *tmp;
 
-       if (b)
-               return(b);
-       else
-               return(path);
+       if ((tmp = xrealloc(ptr, size)) == NULL)
+               err(EXIT_FAILURE, "%s failed", __func__);
+       return(tmp);
 }
similarity index 83%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xstrdup.c
index c779e98..c2009d9 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xstrdup.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
+#include <err.h>
+#include <libiberty.h>
+#include <stdlib.h>
 #include <string.h>
 
-const char *
-lbasename(const char *path)
+char *
+xstrdup(const char *str)
 {
-       const char *b = strrchr(path, '/');
+       char *tmp;
 
-       if (b)
-               return(b);
-       else
-               return(path);
+       if ((tmp = strdup(str)) == NULL)
+               err(EXIT_FAILURE, "%s failed", __func__);
+       return(tmp);
 }
similarity index 87%
copy from lib/libiberty/lbasename.c
copy to lib/libiberty/xstrerror.c
index c779e98..31c5d67 100644 (file)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $DragonFly: src/lib/libiberty/lbasename.c,v 1.2 2004/06/09 12:19:21 joerg Exp $
+ * $DragonFly: src/lib/libiberty/xstrerror.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
  */
 
+#include <libiberty.h>
 #include <string.h>
 
-const char *
-lbasename(const char *path)
+char *
+xstrerror(int error)
 {
-       const char *b = strrchr(path, '/');
-
-       if (b)
-               return(b);
-       else
-               return(path);
+       return(strerror(error));
 }