15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland * CDDL HEADER START
35c51f124SMoriah Waterland *
45c51f124SMoriah Waterland * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland *
85c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland * See the License for the specific language governing permissions
115c51f124SMoriah Waterland * and limitations under the License.
125c51f124SMoriah Waterland *
135c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland *
195c51f124SMoriah Waterland * CDDL HEADER END
205c51f124SMoriah Waterland */
215c51f124SMoriah Waterland
225c51f124SMoriah Waterland /*
235c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
245c51f124SMoriah Waterland * Use is subject to license terms.
255c51f124SMoriah Waterland */
265c51f124SMoriah Waterland
275c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
285c51f124SMoriah Waterland /* All Rights Reserved */
295c51f124SMoriah Waterland
305c51f124SMoriah Waterland
315c51f124SMoriah Waterland
325c51f124SMoriah Waterland #include <stdio.h>
335c51f124SMoriah Waterland #include <stdarg.h>
345c51f124SMoriah Waterland #include <unistd.h>
355c51f124SMoriah Waterland #include <string.h>
365c51f124SMoriah Waterland #include <stdlib.h>
375c51f124SMoriah Waterland #include <errno.h>
385c51f124SMoriah Waterland #include "pkglocale.h"
395c51f124SMoriah Waterland #include "pkgerr.h"
405c51f124SMoriah Waterland
415c51f124SMoriah Waterland static char *ProgName = NULL; /* Set via set_prog_name() */
425c51f124SMoriah Waterland
435c51f124SMoriah Waterland
445c51f124SMoriah Waterland static void
error_and_exit(int error_num)455c51f124SMoriah Waterland error_and_exit(int error_num)
465c51f124SMoriah Waterland {
475c51f124SMoriah Waterland (void) fprintf(stderr, "%d\n", error_num);
485c51f124SMoriah Waterland exit(99);
495c51f124SMoriah Waterland }
505c51f124SMoriah Waterland
515c51f124SMoriah Waterland static void (*fatal_err_func)() = &error_and_exit;
525c51f124SMoriah Waterland
535c51f124SMoriah Waterland char *
set_prog_name(char * name)545c51f124SMoriah Waterland set_prog_name(char *name)
555c51f124SMoriah Waterland {
565c51f124SMoriah Waterland if (name == NULL)
575c51f124SMoriah Waterland return (NULL);
585c51f124SMoriah Waterland if ((name = strdup(name)) == NULL) {
595c51f124SMoriah Waterland (void) fprintf(stderr,
605c51f124SMoriah Waterland "set_prog_name(): strdup(name) failed.\n");
615c51f124SMoriah Waterland exit(1);
625c51f124SMoriah Waterland }
635c51f124SMoriah Waterland ProgName = strrchr(name, '/');
645c51f124SMoriah Waterland if (!ProgName++)
655c51f124SMoriah Waterland ProgName = name;
665c51f124SMoriah Waterland
675c51f124SMoriah Waterland return (ProgName);
685c51f124SMoriah Waterland }
695c51f124SMoriah Waterland
705c51f124SMoriah Waterland char *
get_prog_name(void)715c51f124SMoriah Waterland get_prog_name(void)
725c51f124SMoriah Waterland {
735c51f124SMoriah Waterland return (ProgName);
745c51f124SMoriah Waterland }
755c51f124SMoriah Waterland
765c51f124SMoriah Waterland
77*4656d474SGarrett D'Amore /*PRINTFLIKE1*/
785c51f124SMoriah Waterland void
progerr(char * fmt,...)795c51f124SMoriah Waterland progerr(char *fmt, ...)
805c51f124SMoriah Waterland {
815c51f124SMoriah Waterland va_list ap;
825c51f124SMoriah Waterland
835c51f124SMoriah Waterland va_start(ap, fmt);
845c51f124SMoriah Waterland
855c51f124SMoriah Waterland if (ProgName && *ProgName)
865c51f124SMoriah Waterland (void) fprintf(stderr, pkg_gt("%s: ERROR: "), ProgName);
875c51f124SMoriah Waterland else
885c51f124SMoriah Waterland (void) fprintf(stderr, pkg_gt(" ERROR: "));
895c51f124SMoriah Waterland
905c51f124SMoriah Waterland (void) vfprintf(stderr, fmt, ap);
915c51f124SMoriah Waterland
925c51f124SMoriah Waterland va_end(ap);
935c51f124SMoriah Waterland
945c51f124SMoriah Waterland (void) fprintf(stderr, "\n");
955c51f124SMoriah Waterland }
965c51f124SMoriah Waterland
975c51f124SMoriah Waterland void
pkgerr(PKG_ERR * err)985c51f124SMoriah Waterland pkgerr(PKG_ERR *err)
995c51f124SMoriah Waterland {
1005c51f124SMoriah Waterland int i;
1015c51f124SMoriah Waterland
1025c51f124SMoriah Waterland for (i = 0; i < pkgerr_num(err); i++) {
1035c51f124SMoriah Waterland progerr("%s", pkgerr_get(err, i));
1045c51f124SMoriah Waterland }
1055c51f124SMoriah Waterland }
1065c51f124SMoriah Waterland
1075c51f124SMoriah Waterland
1085c51f124SMoriah Waterland /*
1095c51f124SMoriah Waterland * set_memalloc_failure_func()
1105c51f124SMoriah Waterland * Allows an appliation to specify the function to be called when
1115c51f124SMoriah Waterland * a memory allocation function fails.
1125c51f124SMoriah Waterland * Parameters:
1135c51f124SMoriah Waterland * (*alloc_proc)(int) - specifies the function to call if fatal error
1145c51f124SMoriah Waterland * (such as being unable to allocate memory) occurs.
1155c51f124SMoriah Waterland * Return:
1165c51f124SMoriah Waterland * none
1175c51f124SMoriah Waterland * Status:
1185c51f124SMoriah Waterland * Public
1195c51f124SMoriah Waterland */
1205c51f124SMoriah Waterland void
set_memalloc_failure_func(void (* alloc_proc)(int))1215c51f124SMoriah Waterland set_memalloc_failure_func(void (*alloc_proc)(int))
1225c51f124SMoriah Waterland {
1235c51f124SMoriah Waterland if (alloc_proc != (void (*)())NULL)
1245c51f124SMoriah Waterland fatal_err_func = alloc_proc;
1255c51f124SMoriah Waterland }
1265c51f124SMoriah Waterland
1275c51f124SMoriah Waterland /*
1285c51f124SMoriah Waterland * xmalloc()
1295c51f124SMoriah Waterland * Alloc 'size' bytes from heap using malloc()
1305c51f124SMoriah Waterland * Parameters:
1315c51f124SMoriah Waterland * size - number of bytes to malloc
1325c51f124SMoriah Waterland * Return:
1335c51f124SMoriah Waterland * NULL - malloc() failure
1345c51f124SMoriah Waterland * void * - pointer to allocated structure
1355c51f124SMoriah Waterland * Status:
1365c51f124SMoriah Waterland * public
1375c51f124SMoriah Waterland */
1385c51f124SMoriah Waterland void *
xmalloc(size_t size)1395c51f124SMoriah Waterland xmalloc(size_t size)
1405c51f124SMoriah Waterland {
1415c51f124SMoriah Waterland void *tmp;
1425c51f124SMoriah Waterland
1435c51f124SMoriah Waterland if ((tmp = (void *) malloc(size)) == NULL) {
1445c51f124SMoriah Waterland fatal_err_func(errno);
1455c51f124SMoriah Waterland return (NULL);
1465c51f124SMoriah Waterland } else
1475c51f124SMoriah Waterland return (tmp);
1485c51f124SMoriah Waterland }
1495c51f124SMoriah Waterland
1505c51f124SMoriah Waterland /*
1515c51f124SMoriah Waterland * xrealloc()
1525c51f124SMoriah Waterland * Calls realloc() with the specfied parameters. xrealloc()
1535c51f124SMoriah Waterland * checks for realloc failures and adjusts the return value
1545c51f124SMoriah Waterland * automatically.
1555c51f124SMoriah Waterland * Parameters:
1565c51f124SMoriah Waterland * ptr - pointer to existing data block
1575c51f124SMoriah Waterland * size - number of bytes additional
1585c51f124SMoriah Waterland * Return:
1595c51f124SMoriah Waterland * NULL - realloc() failed
1605c51f124SMoriah Waterland * void * - pointer to realloc'd structured
1615c51f124SMoriah Waterland * Status:
1625c51f124SMoriah Waterland * public
1635c51f124SMoriah Waterland */
1645c51f124SMoriah Waterland void *
xrealloc(void * ptr,size_t size)1655c51f124SMoriah Waterland xrealloc(void *ptr, size_t size)
1665c51f124SMoriah Waterland {
1675c51f124SMoriah Waterland void *tmp;
1685c51f124SMoriah Waterland
1695c51f124SMoriah Waterland if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
1705c51f124SMoriah Waterland fatal_err_func(errno);
1715c51f124SMoriah Waterland return ((void *)NULL);
1725c51f124SMoriah Waterland } else
1735c51f124SMoriah Waterland return (tmp);
1745c51f124SMoriah Waterland }
1755c51f124SMoriah Waterland
1765c51f124SMoriah Waterland /*
1775c51f124SMoriah Waterland * xstrdup()
1785c51f124SMoriah Waterland * Allocate space for the string from the heap, copy 'str' into it,
1795c51f124SMoriah Waterland * and return a pointer to it.
1805c51f124SMoriah Waterland * Parameters:
1815c51f124SMoriah Waterland * str - string to duplicate
1825c51f124SMoriah Waterland * Return:
1835c51f124SMoriah Waterland * NULL - duplication failed or 'str' was NULL
1845c51f124SMoriah Waterland * char * - pointer to newly allocated/initialized structure
1855c51f124SMoriah Waterland * Status:
1865c51f124SMoriah Waterland * public
1875c51f124SMoriah Waterland */
1885c51f124SMoriah Waterland char *
xstrdup(char * str)1895c51f124SMoriah Waterland xstrdup(char *str)
1905c51f124SMoriah Waterland {
1915c51f124SMoriah Waterland char *tmp;
1925c51f124SMoriah Waterland
1935c51f124SMoriah Waterland if (str == NULL)
1945c51f124SMoriah Waterland return ((char *)NULL);
1955c51f124SMoriah Waterland
1965c51f124SMoriah Waterland if ((tmp = strdup(str)) == NULL) {
1975c51f124SMoriah Waterland fatal_err_func(errno);
1985c51f124SMoriah Waterland return ((char *)NULL);
1995c51f124SMoriah Waterland } else
2005c51f124SMoriah Waterland return (tmp);
2015c51f124SMoriah Waterland }
202