3db86aab554edbb4244c8d1a1c90f152eee768afstevel/*
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * CDDL HEADER START
3db86aab554edbb4244c8d1a1c90f152eee768afstevel *
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * The contents of this file are subject to the terms of the
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * Common Development and Distribution License, Version 1.0 only
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * (the "License"). You may not use this file except in compliance
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * with the License.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel *
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * or http://www.opensolaris.org/os/licensing.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * See the License for the specific language governing permissions
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * and limitations under the License.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel *
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * When distributing Covered Code, include this CDDL HEADER in each
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * If applicable, add the following below this CDDL HEADER, with the
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * fields enclosed by brackets "[]" replaced with your own identifying
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * information: Portions Copyright [yyyy] [name of copyright owner]
3db86aab554edbb4244c8d1a1c90f152eee768afstevel *
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * CDDL HEADER END
3db86aab554edbb4244c8d1a1c90f152eee768afstevel */
3db86aab554edbb4244c8d1a1c90f152eee768afstevel/*
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * Use is subject to license terms.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel */
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#pragma ident "%Z%%M% %I% %E% SMI"
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <stddef.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <stdlib.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <stdio.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <stdarg.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <string.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <locale.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <sys/param.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include <config_admin.h>
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#include "mema_util.h"
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel/*
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * The libmemadm routines can return arbitrary error strings. As the
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * calling program does not know how long these errors might be,
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * the library routines must allocate the required space and the
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * calling program must deallocate it.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel *
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * This routine povides a printf-like interface for creating the
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * error strings.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel */
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel#define FMT_STR_SLOP (16)
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevelvoid
3db86aab554edbb4244c8d1a1c90f152eee768afstevel__fmt_errstring(
3db86aab554edbb4244c8d1a1c90f152eee768afstevel char **errstring,
3db86aab554edbb4244c8d1a1c90f152eee768afstevel size_t extra_length_hint,
3db86aab554edbb4244c8d1a1c90f152eee768afstevel const char *fmt,
3db86aab554edbb4244c8d1a1c90f152eee768afstevel ...)
3db86aab554edbb4244c8d1a1c90f152eee768afstevel{
3db86aab554edbb4244c8d1a1c90f152eee768afstevel char *ebuf;
3db86aab554edbb4244c8d1a1c90f152eee768afstevel size_t elen;
3db86aab554edbb4244c8d1a1c90f152eee768afstevel va_list ap;
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel /*
3db86aab554edbb4244c8d1a1c90f152eee768afstevel * If no errors required or error already set, return.
3db86aab554edbb4244c8d1a1c90f152eee768afstevel */
3db86aab554edbb4244c8d1a1c90f152eee768afstevel if ((errstring == NULL) || (*errstring != NULL))
3db86aab554edbb4244c8d1a1c90f152eee768afstevel return;
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel elen = strlen(fmt) + extra_length_hint + FMT_STR_SLOP;
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel if ((ebuf = (char *)malloc(elen + 1)) == NULL)
3db86aab554edbb4244c8d1a1c90f152eee768afstevel return;
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel va_start(ap, fmt);
3db86aab554edbb4244c8d1a1c90f152eee768afstevel (void) vsprintf(ebuf, fmt, ap);
3db86aab554edbb4244c8d1a1c90f152eee768afstevel va_end(ap);
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel if (strlen(ebuf) > elen)
3db86aab554edbb4244c8d1a1c90f152eee768afstevel abort();
3db86aab554edbb4244c8d1a1c90f152eee768afstevel
3db86aab554edbb4244c8d1a1c90f152eee768afstevel *errstring = ebuf;
3db86aab554edbb4244c8d1a1c90f152eee768afstevel}