diff options
Diffstat (limited to 'lib/libuutil')
-rw-r--r-- | lib/libuutil/include/libuutil.h | 13 | ||||
-rw-r--r-- | lib/libuutil/uu_alloc.c | 41 | ||||
-rw-r--r-- | lib/libuutil/uu_misc.c | 33 | ||||
-rw-r--r-- | lib/libuutil/uu_string.c | 56 |
4 files changed, 135 insertions, 8 deletions
diff --git a/lib/libuutil/include/libuutil.h b/lib/libuutil/include/libuutil.h index ccd46b977..667542446 100644 --- a/lib/libuutil/include/libuutil.h +++ b/lib/libuutil/include/libuutil.h @@ -19,8 +19,7 @@ * CDDL HEADER END */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. */ #ifndef _LIBUUTIL_H @@ -28,6 +27,7 @@ #include <sys/types.h> #include <stdarg.h> +#include <stdio.h> #ifdef __cplusplus extern "C" { @@ -142,12 +142,21 @@ extern int uu_open_tmp(const char *dir, uint_t uflags); /* * Convenience functions. */ +#define UU_NELEM(a) (sizeof (a) / sizeof ((a)[0])) + /*PRINTFLIKE1*/ extern char *uu_msprintf(const char *format, ...); extern void *uu_zalloc(size_t); extern char *uu_strdup(const char *); extern void uu_free(void *); +extern boolean_t uu_strcaseeq(const char *a, const char *b); +extern boolean_t uu_streq(const char *a, const char *b); +extern char *uu_strndup(const char *s, size_t n); +extern boolean_t uu_strbw(const char *a, const char *b); +extern void *uu_memdup(const void *buf, size_t sz); +extern void uu_dump(FILE *out, const char *prefix, const void *buf, size_t len); + /* * Comparison function type definition. * Developers should be careful in their use of the _private argument. If you diff --git a/lib/libuutil/uu_alloc.c b/lib/libuutil/uu_alloc.c index 05d862287..2bef759d5 100644 --- a/lib/libuutil/uu_alloc.c +++ b/lib/libuutil/uu_alloc.c @@ -19,8 +19,7 @@ * CDDL HEADER END */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. */ #include "libuutil_common.h" @@ -67,6 +66,44 @@ uu_strdup(const char *str) return (buf); } +/* + * Duplicate up to n bytes of a string. Kind of sort of like + * strdup(strlcpy(s, n)). + */ +char * +uu_strndup(const char *s, size_t n) +{ + size_t len; + char *p; + + len = strnlen(s, n); + p = uu_zalloc(len + 1); + if (p == NULL) + return (NULL); + + if (len > 0) + (void) memcpy(p, s, len); + p[len] = '\0'; + + return (p); +} + +/* + * Duplicate a block of memory. Combines malloc with memcpy, much as + * strdup combines malloc, strlen, and strcpy. + */ +void * +uu_memdup(const void *buf, size_t sz) +{ + void *p; + + p = uu_zalloc(sz); + if (p == NULL) + return (NULL); + (void) memcpy(p, buf, sz); + return (p); +} + char * uu_msprintf(const char *format, ...) { diff --git a/lib/libuutil/uu_misc.c b/lib/libuutil/uu_misc.c index 74ec177c1..3d5b40ca8 100644 --- a/lib/libuutil/uu_misc.c +++ b/lib/libuutil/uu_misc.c @@ -20,12 +20,9 @@ */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "libuutil_common.h" #include <assert.h> @@ -39,6 +36,7 @@ #include <sys/debug.h> #include <thread.h> #include <unistd.h> +#include <ctype.h> #if !defined(TEXT_DOMAIN) #define TEXT_DOMAIN "SYS_TEST" @@ -253,3 +251,30 @@ uu_init(void) { (void) pthread_atfork(uu_lockup, uu_release, uu_release_child); } + +/* + * Dump a block of memory in hex+ascii, for debugging + */ +void +uu_dump(FILE *out, const char *prefix, const void *buf, size_t len) +{ + const unsigned char *p = buf; + int i; + + for (i = 0; i < len; i += 16) { + int j; + + (void) fprintf(out, "%s", prefix); + for (j = 0; j < 16 && i + j < len; j++) { + (void) fprintf(out, "%2.2x ", p[i + j]); + } + for (; j < 16; j++) { + (void) fprintf(out, " "); + } + for (j = 0; j < 16 && i + j < len; j++) { + (void) fprintf(out, "%c", + isprint(p[i + j]) ? p[i + j] : '.'); + } + (void) fprintf(out, "\n"); + } +} diff --git a/lib/libuutil/uu_string.c b/lib/libuutil/uu_string.c new file mode 100644 index 000000000..66afba05e --- /dev/null +++ b/lib/libuutil/uu_string.c @@ -0,0 +1,56 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. + */ + +/* + * String helper functions + */ + +#include <string.h> +#include <sys/types.h> +#include <stdio.h> +#include <malloc.h> +#include <ctype.h> +#include "libuutil.h" + +/* Return true if strings are equal */ +boolean_t +uu_streq(const char *a, const char *b) +{ + return (strcmp(a, b) == 0); +} + +/* Return true if strings are equal, case-insensitively */ +boolean_t +uu_strcaseeq(const char *a, const char *b) +{ + return (strcasecmp(a, b) == 0); +} + +/* Return true if string a Begins With string b */ +boolean_t +uu_strbw(const char *a, const char *b) +{ + return (strncmp(a, b, strlen(b)) == 0); +} |