2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include "libuutil_common.h"
2N/A
2N/A#include <stdarg.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/Avoid *
2N/Auu_zalloc(size_t n)
2N/A{
2N/A void *p = malloc(n);
2N/A
2N/A if (p == NULL) {
2N/A uu_set_error(UU_ERROR_SYSTEM);
2N/A return (NULL);
2N/A }
2N/A
2N/A (void) memset(p, 0, n);
2N/A
2N/A return (p);
2N/A}
2N/A
2N/Avoid
2N/Auu_free(void *p)
2N/A{
2N/A free(p);
2N/A}
2N/A
2N/Achar *
2N/Auu_strdup(const char *str)
2N/A{
2N/A char *buf = NULL;
2N/A
2N/A if (str != NULL) {
2N/A size_t sz;
2N/A
2N/A sz = strlen(str) + 1;
2N/A buf = uu_zalloc(sz);
2N/A if (buf != NULL)
2N/A (void) memcpy(buf, str, sz);
2N/A }
2N/A return (buf);
2N/A}
2N/A
2N/A/*
2N/A * Duplicate up to n bytes of a string. Kind of sort of like
2N/A * strdup(strlcpy(s, n)).
2N/A */
2N/Achar *
2N/Auu_strndup(const char *s, size_t n)
2N/A{
2N/A size_t len;
2N/A char *p;
2N/A
2N/A len = strnlen(s, n);
2N/A p = uu_zalloc(len + 1);
2N/A if (p == NULL)
2N/A return (NULL);
2N/A
2N/A if (len > 0)
2N/A (void) memcpy(p, s, len);
2N/A p[len] = '\0';
2N/A
2N/A return (p);
2N/A}
2N/A
2N/A/*
2N/A * Duplicate a block of memory. Combines malloc with memcpy, much as
2N/A * strdup combines malloc, strlen, and strcpy.
2N/A */
2N/Avoid *
2N/Auu_memdup(const void *buf, size_t sz)
2N/A{
2N/A void *p;
2N/A
2N/A p = uu_zalloc(sz);
2N/A if (p == NULL)
2N/A return (NULL);
2N/A (void) memcpy(p, buf, sz);
2N/A return (p);
2N/A}
2N/A
2N/Achar *
2N/Auu_msprintf(const char *format, ...)
2N/A{
2N/A va_list args;
2N/A char attic[1];
2N/A uint_t M, m;
2N/A char *b;
2N/A
2N/A va_start(args, format);
2N/A M = vsnprintf(attic, 1, format, args);
2N/A va_end(args);
2N/A
2N/A for (;;) {
2N/A m = M;
2N/A if ((b = uu_zalloc(m + 1)) == NULL)
2N/A return (NULL);
2N/A
2N/A va_start(args, format);
2N/A M = vsnprintf(b, m + 1, format, args);
2N/A va_end(args);
2N/A
2N/A if (M == m)
2N/A break; /* sizes match */
2N/A
2N/A uu_free(b);
2N/A }
2N/A
2N/A return (b);
2N/A}