4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2017 Peter Tribble.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
42 #include "pkglocale.h"
44 static char *ProgName
= NULL
; /* Set via set_prog_name() */
48 error_and_exit(int error_num
)
50 (void) fprintf(stderr
, "%d\n", error_num
);
54 static void (*fatal_err_func
)() = &error_and_exit
;
57 set_prog_name(char *name
)
61 if ((name
= strdup(name
)) == NULL
) {
62 (void) fprintf(stderr
,
63 "set_prog_name(): strdup(name) failed.\n");
66 ProgName
= strrchr(name
, '/');
82 progerr(char *fmt
, ...)
88 if (ProgName
&& *ProgName
)
89 (void) fprintf(stderr
, pkg_gt("%s: ERROR: "), ProgName
);
91 (void) fprintf(stderr
, pkg_gt(" ERROR: "));
93 (void) vfprintf(stderr
, fmt
, ap
);
97 (void) fprintf(stderr
, "\n");
101 * set_memalloc_failure_func()
102 * Allows an appliation to specify the function to be called when
103 * a memory allocation function fails.
105 * (*alloc_proc)(int) - specifies the function to call if fatal error
106 * (such as being unable to allocate memory) occurs.
113 set_memalloc_failure_func(void (*alloc_proc
)(int))
115 if (alloc_proc
!= (void (*)())NULL
)
116 fatal_err_func
= alloc_proc
;
121 * Alloc 'size' bytes from heap using malloc()
123 * size - number of bytes to malloc
125 * NULL - malloc() failure
126 * void * - pointer to allocated structure
135 if ((tmp
= (void *) malloc(size
)) == NULL
) {
136 fatal_err_func(errno
);
144 * Calls realloc() with the specfied parameters. xrealloc()
145 * checks for realloc failures and adjusts the return value
148 * ptr - pointer to existing data block
149 * size - number of bytes additional
151 * NULL - realloc() failed
152 * void * - pointer to realloc'd structured
157 xrealloc(void *ptr
, size_t size
)
161 if ((tmp
= (void *)realloc(ptr
, size
)) == (void *)NULL
) {
162 fatal_err_func(errno
);
163 return ((void *)NULL
);
170 * Allocate space for the string from the heap, copy 'str' into it,
171 * and return a pointer to it.
173 * str - string to duplicate
175 * NULL - duplication failed or 'str' was NULL
176 * char * - pointer to newly allocated/initialized structure
186 return ((char *)NULL
);
188 if ((tmp
= strdup(str
)) == NULL
) {
189 fatal_err_func(errno
);
190 return ((char *)NULL
);