xref: /illumos-gate/usr/src/lib/libpkg/common/progerr.c (revision 31358b0d3ea5b07f4167f88ac393a5eb2a2249d6)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2017 Peter Tribble.
24  */
25 
26 /*
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
33 
34 
35 
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include "pkglocale.h"
43 
44 static char	*ProgName = NULL; 	/* Set via set_prog_name() */
45 
46 
47 static void
48 error_and_exit(int error_num)
49 {
50 	(void) fprintf(stderr, "%d\n", error_num);
51 	exit(99);
52 }
53 
54 static void	(*fatal_err_func)() = &error_and_exit;
55 
56 char *
57 set_prog_name(char *name)
58 {
59 	if (name == NULL)
60 		return (NULL);
61 	if ((name = strdup(name)) == NULL) {
62 		(void) fprintf(stderr,
63 		    "set_prog_name(): strdup(name) failed.\n");
64 		exit(1);
65 	}
66 	ProgName = strrchr(name, '/');
67 	if (!ProgName++)
68 		ProgName = name;
69 
70 	return (ProgName);
71 }
72 
73 char *
74 get_prog_name(void)
75 {
76 	return (ProgName);
77 }
78 
79 
80 /*PRINTFLIKE1*/
81 void
82 progerr(char *fmt, ...)
83 {
84 	va_list ap;
85 
86 	va_start(ap, fmt);
87 
88 	if (ProgName && *ProgName)
89 		(void) fprintf(stderr, pkg_gt("%s: ERROR: "), ProgName);
90 	else
91 		(void) fprintf(stderr, pkg_gt(" ERROR: "));
92 
93 	(void) vfprintf(stderr, fmt, ap);
94 
95 	va_end(ap);
96 
97 	(void) fprintf(stderr, "\n");
98 }
99 
100 /*
101  * set_memalloc_failure_func()
102  *	Allows an appliation to specify the function to be called when
103  *	a memory allocation function fails.
104  * Parameters:
105  *	(*alloc_proc)(int)	- specifies the function to call if fatal error
106  *			  (such as being unable to allocate memory) occurs.
107  * Return:
108  *	none
109  * Status:
110  *	Public
111  */
112 void
113 set_memalloc_failure_func(void (*alloc_proc)(int))
114 {
115 	if (alloc_proc != (void (*)())NULL)
116 		fatal_err_func = alloc_proc;
117 }
118 
119 /*
120  * xmalloc()
121  * 	Alloc 'size' bytes from heap using malloc()
122  * Parameters:
123  *	size	- number of bytes to malloc
124  * Return:
125  *	NULL	- malloc() failure
126  *	void *	- pointer to allocated structure
127  * Status:
128  *	public
129  */
130 void *
131 xmalloc(size_t size)
132 {
133 	void *tmp;
134 
135 	if ((tmp = (void *) malloc(size)) == NULL) {
136 		fatal_err_func(errno);
137 		return (NULL);
138 	} else
139 		return (tmp);
140 }
141 
142 /*
143  * xrealloc()
144  *	Calls realloc() with the specfied parameters. xrealloc()
145  *	checks for realloc failures and adjusts the return value
146  *	automatically.
147  * Parameters:
148  *	ptr	- pointer to existing data block
149  * 	size	- number of bytes additional
150  * Return:
151  *	NULL	- realloc() failed
152  *	void *	- pointer to realloc'd structured
153  * Status:
154  *	public
155  */
156 void *
157 xrealloc(void *ptr, size_t size)
158 {
159 	void *tmp;
160 
161 	if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
162 		fatal_err_func(errno);
163 		return ((void *)NULL);
164 	} else
165 		return (tmp);
166 }
167 
168 /*
169  * xstrdup()
170  *	Allocate space for the string from the heap, copy 'str' into it,
171  *	and return a pointer to it.
172  * Parameters:
173  *	str	- string to duplicate
174  * Return:
175  *	NULL	- duplication failed or 'str' was NULL
176  * 	char *	- pointer to newly allocated/initialized structure
177  * Status:
178  *	public
179  */
180 char *
181 xstrdup(char *str)
182 {
183 	char *tmp;
184 
185 	if (str == NULL)
186 		return ((char *)NULL);
187 
188 	if ((tmp = strdup(str)) == NULL) {
189 		fatal_err_func(errno);
190 		return ((char *)NULL);
191 	} else
192 		return (tmp);
193 }
194