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/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A
2N/A
2N/A#include <stdio.h>
2N/A#include <stdarg.h>
2N/A#include <unistd.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <errno.h>
2N/A#include "pkglocale.h"
2N/A#include "pkgerr.h"
2N/A
2N/Astatic char *ProgName = NULL; /* Set via set_prog_name() */
2N/A
2N/A
2N/Astatic void
2N/Aerror_and_exit(int error_num)
2N/A{
2N/A (void) fprintf(stderr, "%d\n", error_num);
2N/A exit(99);
2N/A}
2N/A
2N/Astatic void (*fatal_err_func)() = &error_and_exit;
2N/A
2N/Achar *
2N/Aset_prog_name(char *name)
2N/A{
2N/A if (name == NULL)
2N/A return (NULL);
2N/A if ((name = strdup(name)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A "set_prog_name(): strdup(name) failed.\n");
2N/A exit(1);
2N/A }
2N/A ProgName = strrchr(name, '/');
2N/A if (!ProgName++)
2N/A ProgName = name;
2N/A
2N/A return (ProgName);
2N/A}
2N/A
2N/Achar *
2N/Aget_prog_name(void)
2N/A{
2N/A return (ProgName);
2N/A}
2N/A
2N/A
2N/A/*VARARGS*/
2N/Avoid
2N/Aprogerr(char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A va_start(ap, fmt);
2N/A
2N/A if (ProgName && *ProgName)
2N/A (void) fprintf(stderr, pkg_gt("%s: ERROR: "), ProgName);
2N/A else
2N/A (void) fprintf(stderr, pkg_gt(" ERROR: "));
2N/A
2N/A (void) vfprintf(stderr, fmt, ap);
2N/A
2N/A va_end(ap);
2N/A
2N/A (void) fprintf(stderr, "\n");
2N/A}
2N/A
2N/Avoid
2N/Apkgerr(PKG_ERR *err)
2N/A{
2N/A int i;
2N/A
2N/A for (i = 0; i < pkgerr_num(err); i++) {
2N/A progerr("%s", pkgerr_get(err, i));
2N/A }
2N/A}
2N/A
2N/A
2N/A/*
2N/A * set_memalloc_failure_func()
2N/A * Allows an appliation to specify the function to be called when
2N/A * a memory allocation function fails.
2N/A * Parameters:
2N/A * (*alloc_proc)(int) - specifies the function to call if fatal error
2N/A * (such as being unable to allocate memory) occurs.
2N/A * Return:
2N/A * none
2N/A * Status:
2N/A * Public
2N/A */
2N/Avoid
2N/Aset_memalloc_failure_func(void (*alloc_proc)(int))
2N/A{
2N/A if (alloc_proc != (void (*)())NULL)
2N/A fatal_err_func = alloc_proc;
2N/A}
2N/A
2N/A/*
2N/A * xmalloc()
2N/A * Alloc 'size' bytes from heap using malloc()
2N/A * Parameters:
2N/A * size - number of bytes to malloc
2N/A * Return:
2N/A * NULL - malloc() failure
2N/A * void * - pointer to allocated structure
2N/A * Status:
2N/A * public
2N/A */
2N/Avoid *
2N/Axmalloc(size_t size)
2N/A{
2N/A void *tmp;
2N/A
2N/A if ((tmp = (void *) malloc(size)) == NULL) {
2N/A fatal_err_func(errno);
2N/A return (NULL);
2N/A } else
2N/A return (tmp);
2N/A}
2N/A
2N/A/*
2N/A * xrealloc()
2N/A * Calls realloc() with the specfied parameters. xrealloc()
2N/A * checks for realloc failures and adjusts the return value
2N/A * automatically.
2N/A * Parameters:
2N/A * ptr - pointer to existing data block
2N/A * size - number of bytes additional
2N/A * Return:
2N/A * NULL - realloc() failed
2N/A * void * - pointer to realloc'd structured
2N/A * Status:
2N/A * public
2N/A */
2N/Avoid *
2N/Axrealloc(void *ptr, size_t size)
2N/A{
2N/A void *tmp;
2N/A
2N/A if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
2N/A fatal_err_func(errno);
2N/A return ((void *)NULL);
2N/A } else
2N/A return (tmp);
2N/A}
2N/A
2N/A/*
2N/A * xstrdup()
2N/A * Allocate space for the string from the heap, copy 'str' into it,
2N/A * and return a pointer to it.
2N/A * Parameters:
2N/A * str - string to duplicate
2N/A * Return:
2N/A * NULL - duplication failed or 'str' was NULL
2N/A * char * - pointer to newly allocated/initialized structure
2N/A * Status:
2N/A * public
2N/A */
2N/Achar *
2N/Axstrdup(char *str)
2N/A{
2N/A char *tmp;
2N/A
2N/A if (str == NULL)
2N/A return ((char *)NULL);
2N/A
2N/A if ((tmp = strdup(str)) == NULL) {
2N/A fatal_err_func(errno);
2N/A return ((char *)NULL);
2N/A } else
2N/A return (tmp);
2N/A}