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
22*32991bedSPeter Tribble /*
23*32991bedSPeter Tribble * Copyright (c) 2017 Peter Tribble.
24*32991bedSPeter Tribble */
25*32991bedSPeter Tribble
265c51f124SMoriah Waterland /*
275c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
285c51f124SMoriah Waterland * Use is subject to license terms.
295c51f124SMoriah Waterland */
305c51f124SMoriah Waterland
315c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
325c51f124SMoriah Waterland /* All Rights Reserved */
335c51f124SMoriah Waterland
345c51f124SMoriah Waterland
355c51f124SMoriah Waterland
365c51f124SMoriah Waterland #include <stdio.h>
375c51f124SMoriah Waterland #include <stdarg.h>
385c51f124SMoriah Waterland #include <unistd.h>
395c51f124SMoriah Waterland #include <string.h>
405c51f124SMoriah Waterland #include <stdlib.h>
415c51f124SMoriah Waterland #include <errno.h>
425c51f124SMoriah Waterland #include "pkglocale.h"
435c51f124SMoriah Waterland
445c51f124SMoriah Waterland static char *ProgName = NULL; /* Set via set_prog_name() */
455c51f124SMoriah Waterland
465c51f124SMoriah Waterland
475c51f124SMoriah Waterland static void
error_and_exit(int error_num)485c51f124SMoriah Waterland error_and_exit(int error_num)
495c51f124SMoriah Waterland {
505c51f124SMoriah Waterland (void) fprintf(stderr, "%d\n", error_num);
515c51f124SMoriah Waterland exit(99);
525c51f124SMoriah Waterland }
535c51f124SMoriah Waterland
545c51f124SMoriah Waterland static void (*fatal_err_func)() = &error_and_exit;
555c51f124SMoriah Waterland
565c51f124SMoriah Waterland char *
set_prog_name(char * name)575c51f124SMoriah Waterland set_prog_name(char *name)
585c51f124SMoriah Waterland {
595c51f124SMoriah Waterland if (name == NULL)
605c51f124SMoriah Waterland return (NULL);
615c51f124SMoriah Waterland if ((name = strdup(name)) == NULL) {
625c51f124SMoriah Waterland (void) fprintf(stderr,
635c51f124SMoriah Waterland "set_prog_name(): strdup(name) failed.\n");
645c51f124SMoriah Waterland exit(1);
655c51f124SMoriah Waterland }
665c51f124SMoriah Waterland ProgName = strrchr(name, '/');
675c51f124SMoriah Waterland if (!ProgName++)
685c51f124SMoriah Waterland ProgName = name;
695c51f124SMoriah Waterland
705c51f124SMoriah Waterland return (ProgName);
715c51f124SMoriah Waterland }
725c51f124SMoriah Waterland
735c51f124SMoriah Waterland char *
get_prog_name(void)745c51f124SMoriah Waterland get_prog_name(void)
755c51f124SMoriah Waterland {
765c51f124SMoriah Waterland return (ProgName);
775c51f124SMoriah Waterland }
785c51f124SMoriah Waterland
795c51f124SMoriah Waterland
804656d474SGarrett D'Amore /*PRINTFLIKE1*/
815c51f124SMoriah Waterland void
progerr(char * fmt,...)825c51f124SMoriah Waterland progerr(char *fmt, ...)
835c51f124SMoriah Waterland {
845c51f124SMoriah Waterland va_list ap;
855c51f124SMoriah Waterland
865c51f124SMoriah Waterland va_start(ap, fmt);
875c51f124SMoriah Waterland
885c51f124SMoriah Waterland if (ProgName && *ProgName)
895c51f124SMoriah Waterland (void) fprintf(stderr, pkg_gt("%s: ERROR: "), ProgName);
905c51f124SMoriah Waterland else
915c51f124SMoriah Waterland (void) fprintf(stderr, pkg_gt(" ERROR: "));
925c51f124SMoriah Waterland
935c51f124SMoriah Waterland (void) vfprintf(stderr, fmt, ap);
945c51f124SMoriah Waterland
955c51f124SMoriah Waterland va_end(ap);
965c51f124SMoriah Waterland
975c51f124SMoriah Waterland (void) fprintf(stderr, "\n");
985c51f124SMoriah Waterland }
995c51f124SMoriah Waterland
1005c51f124SMoriah Waterland /*
1015c51f124SMoriah Waterland * set_memalloc_failure_func()
1025c51f124SMoriah Waterland * Allows an appliation to specify the function to be called when
1035c51f124SMoriah Waterland * a memory allocation function fails.
1045c51f124SMoriah Waterland * Parameters:
1055c51f124SMoriah Waterland * (*alloc_proc)(int) - specifies the function to call if fatal error
1065c51f124SMoriah Waterland * (such as being unable to allocate memory) occurs.
1075c51f124SMoriah Waterland * Return:
1085c51f124SMoriah Waterland * none
1095c51f124SMoriah Waterland * Status:
1105c51f124SMoriah Waterland * Public
1115c51f124SMoriah Waterland */
1125c51f124SMoriah Waterland void
set_memalloc_failure_func(void (* alloc_proc)(int))1135c51f124SMoriah Waterland set_memalloc_failure_func(void (*alloc_proc)(int))
1145c51f124SMoriah Waterland {
1155c51f124SMoriah Waterland if (alloc_proc != (void (*)())NULL)
1165c51f124SMoriah Waterland fatal_err_func = alloc_proc;
1175c51f124SMoriah Waterland }
1185c51f124SMoriah Waterland
1195c51f124SMoriah Waterland /*
1205c51f124SMoriah Waterland * xmalloc()
1215c51f124SMoriah Waterland * Alloc 'size' bytes from heap using malloc()
1225c51f124SMoriah Waterland * Parameters:
1235c51f124SMoriah Waterland * size - number of bytes to malloc
1245c51f124SMoriah Waterland * Return:
1255c51f124SMoriah Waterland * NULL - malloc() failure
1265c51f124SMoriah Waterland * void * - pointer to allocated structure
1275c51f124SMoriah Waterland * Status:
1285c51f124SMoriah Waterland * public
1295c51f124SMoriah Waterland */
1305c51f124SMoriah Waterland void *
xmalloc(size_t size)1315c51f124SMoriah Waterland xmalloc(size_t size)
1325c51f124SMoriah Waterland {
1335c51f124SMoriah Waterland void *tmp;
1345c51f124SMoriah Waterland
1355c51f124SMoriah Waterland if ((tmp = (void *) malloc(size)) == NULL) {
1365c51f124SMoriah Waterland fatal_err_func(errno);
1375c51f124SMoriah Waterland return (NULL);
1385c51f124SMoriah Waterland } else
1395c51f124SMoriah Waterland return (tmp);
1405c51f124SMoriah Waterland }
1415c51f124SMoriah Waterland
1425c51f124SMoriah Waterland /*
1435c51f124SMoriah Waterland * xrealloc()
1445c51f124SMoriah Waterland * Calls realloc() with the specfied parameters. xrealloc()
1455c51f124SMoriah Waterland * checks for realloc failures and adjusts the return value
1465c51f124SMoriah Waterland * automatically.
1475c51f124SMoriah Waterland * Parameters:
1485c51f124SMoriah Waterland * ptr - pointer to existing data block
1495c51f124SMoriah Waterland * size - number of bytes additional
1505c51f124SMoriah Waterland * Return:
1515c51f124SMoriah Waterland * NULL - realloc() failed
1525c51f124SMoriah Waterland * void * - pointer to realloc'd structured
1535c51f124SMoriah Waterland * Status:
1545c51f124SMoriah Waterland * public
1555c51f124SMoriah Waterland */
1565c51f124SMoriah Waterland void *
xrealloc(void * ptr,size_t size)1575c51f124SMoriah Waterland xrealloc(void *ptr, size_t size)
1585c51f124SMoriah Waterland {
1595c51f124SMoriah Waterland void *tmp;
1605c51f124SMoriah Waterland
1615c51f124SMoriah Waterland if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
1625c51f124SMoriah Waterland fatal_err_func(errno);
1635c51f124SMoriah Waterland return ((void *)NULL);
1645c51f124SMoriah Waterland } else
1655c51f124SMoriah Waterland return (tmp);
1665c51f124SMoriah Waterland }
1675c51f124SMoriah Waterland
1685c51f124SMoriah Waterland /*
1695c51f124SMoriah Waterland * xstrdup()
1705c51f124SMoriah Waterland * Allocate space for the string from the heap, copy 'str' into it,
1715c51f124SMoriah Waterland * and return a pointer to it.
1725c51f124SMoriah Waterland * Parameters:
1735c51f124SMoriah Waterland * str - string to duplicate
1745c51f124SMoriah Waterland * Return:
1755c51f124SMoriah Waterland * NULL - duplication failed or 'str' was NULL
1765c51f124SMoriah Waterland * char * - pointer to newly allocated/initialized structure
1775c51f124SMoriah Waterland * Status:
1785c51f124SMoriah Waterland * public
1795c51f124SMoriah Waterland */
1805c51f124SMoriah Waterland char *
xstrdup(char * str)1815c51f124SMoriah Waterland xstrdup(char *str)
1825c51f124SMoriah Waterland {
1835c51f124SMoriah Waterland char *tmp;
1845c51f124SMoriah Waterland
1855c51f124SMoriah Waterland if (str == NULL)
1865c51f124SMoriah Waterland return ((char *)NULL);
1875c51f124SMoriah Waterland
1885c51f124SMoriah Waterland if ((tmp = strdup(str)) == NULL) {
1895c51f124SMoriah Waterland fatal_err_func(errno);
1905c51f124SMoriah Waterland return ((char *)NULL);
1915c51f124SMoriah Waterland } else
1925c51f124SMoriah Waterland return (tmp);
1935c51f124SMoriah Waterland }
194