Home | History | Annotate | Line # | Download | only in common
      1      1.1  haad /*
      2      1.1  haad  * CDDL HEADER START
      3      1.1  haad  *
      4      1.1  haad  * The contents of this file are subject to the terms of the
      5      1.1  haad  * Common Development and Distribution License (the "License").
      6      1.1  haad  * You may not use this file except in compliance with the License.
      7      1.1  haad  *
      8      1.1  haad  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9      1.1  haad  * or http://www.opensolaris.org/os/licensing.
     10      1.1  haad  * See the License for the specific language governing permissions
     11      1.1  haad  * and limitations under the License.
     12      1.1  haad  *
     13      1.1  haad  * When distributing Covered Code, include this CDDL HEADER in each
     14      1.1  haad  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15      1.1  haad  * If applicable, add the following below this CDDL HEADER, with the
     16      1.1  haad  * fields enclosed by brackets "[]" replaced with your own identifying
     17      1.1  haad  * information: Portions Copyright [yyyy] [name of copyright owner]
     18      1.1  haad  *
     19      1.1  haad  * CDDL HEADER END
     20      1.1  haad  */
     21      1.1  haad /*
     22  1.1.1.2   chs  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
     23      1.1  haad  */
     24      1.1  haad 
     25      1.1  haad #include "libuutil_common.h"
     26      1.1  haad 
     27      1.1  haad #include <stdarg.h>
     28      1.1  haad #include <stdio.h>
     29      1.1  haad #include <stdlib.h>
     30      1.1  haad #include <string.h>
     31      1.1  haad 
     32      1.1  haad void *
     33      1.1  haad uu_zalloc(size_t n)
     34      1.1  haad {
     35      1.1  haad 	void *p = malloc(n);
     36      1.1  haad 
     37      1.1  haad 	if (p == NULL) {
     38      1.1  haad 		uu_set_error(UU_ERROR_SYSTEM);
     39      1.1  haad 		return (NULL);
     40      1.1  haad 	}
     41      1.1  haad 
     42      1.1  haad 	(void) memset(p, 0, n);
     43      1.1  haad 
     44      1.1  haad 	return (p);
     45      1.1  haad }
     46      1.1  haad 
     47      1.1  haad void
     48      1.1  haad uu_free(void *p)
     49      1.1  haad {
     50      1.1  haad 	free(p);
     51      1.1  haad }
     52      1.1  haad 
     53      1.1  haad char *
     54      1.1  haad uu_strdup(const char *str)
     55      1.1  haad {
     56      1.1  haad 	char *buf = NULL;
     57      1.1  haad 
     58      1.1  haad 	if (str != NULL) {
     59      1.1  haad 		size_t sz;
     60      1.1  haad 
     61      1.1  haad 		sz = strlen(str) + 1;
     62      1.1  haad 		buf = uu_zalloc(sz);
     63      1.1  haad 		if (buf != NULL)
     64      1.1  haad 			(void) memcpy(buf, str, sz);
     65      1.1  haad 	}
     66      1.1  haad 	return (buf);
     67      1.1  haad }
     68      1.1  haad 
     69  1.1.1.2   chs /*
     70  1.1.1.2   chs  * Duplicate up to n bytes of a string.  Kind of sort of like
     71  1.1.1.2   chs  * strdup(strlcpy(s, n)).
     72  1.1.1.2   chs  */
     73  1.1.1.2   chs char *
     74  1.1.1.2   chs uu_strndup(const char *s, size_t n)
     75  1.1.1.2   chs {
     76  1.1.1.2   chs 	size_t len;
     77  1.1.1.2   chs 	char *p;
     78  1.1.1.2   chs 
     79  1.1.1.2   chs 	len = strnlen(s, n);
     80  1.1.1.2   chs 	p = uu_zalloc(len + 1);
     81  1.1.1.2   chs 	if (p == NULL)
     82  1.1.1.2   chs 		return (NULL);
     83  1.1.1.2   chs 
     84  1.1.1.2   chs 	if (len > 0)
     85  1.1.1.2   chs 		(void) memcpy(p, s, len);
     86  1.1.1.2   chs 	p[len] = '\0';
     87  1.1.1.2   chs 
     88  1.1.1.2   chs 	return (p);
     89  1.1.1.2   chs }
     90  1.1.1.2   chs 
     91  1.1.1.2   chs /*
     92  1.1.1.2   chs  * Duplicate a block of memory.  Combines malloc with memcpy, much as
     93  1.1.1.2   chs  * strdup combines malloc, strlen, and strcpy.
     94  1.1.1.2   chs  */
     95  1.1.1.2   chs void *
     96  1.1.1.2   chs uu_memdup(const void *buf, size_t sz)
     97  1.1.1.2   chs {
     98  1.1.1.2   chs 	void *p;
     99  1.1.1.2   chs 
    100  1.1.1.2   chs 	p = uu_zalloc(sz);
    101  1.1.1.2   chs 	if (p == NULL)
    102  1.1.1.2   chs 		return (NULL);
    103  1.1.1.2   chs 	(void) memcpy(p, buf, sz);
    104  1.1.1.2   chs 	return (p);
    105  1.1.1.2   chs }
    106  1.1.1.2   chs 
    107      1.1  haad char *
    108      1.1  haad uu_msprintf(const char *format, ...)
    109      1.1  haad {
    110      1.1  haad 	va_list args;
    111      1.1  haad 	char attic[1];
    112      1.1  haad 	uint_t M, m;
    113      1.1  haad 	char *b;
    114      1.1  haad 
    115      1.1  haad 	va_start(args, format);
    116      1.1  haad 	M = vsnprintf(attic, 1, format, args);
    117      1.1  haad 	va_end(args);
    118      1.1  haad 
    119      1.1  haad 	for (;;) {
    120      1.1  haad 		m = M;
    121      1.1  haad 		if ((b = uu_zalloc(m + 1)) == NULL)
    122      1.1  haad 			return (NULL);
    123      1.1  haad 
    124      1.1  haad 		va_start(args, format);
    125      1.1  haad 		M = vsnprintf(b, m + 1, format, args);
    126      1.1  haad 		va_end(args);
    127      1.1  haad 
    128      1.1  haad 		if (M == m)
    129      1.1  haad 			break;		/* sizes match */
    130      1.1  haad 
    131      1.1  haad 		uu_free(b);
    132      1.1  haad 	}
    133      1.1  haad 
    134      1.1  haad 	return (b);
    135      1.1  haad }
    136