xref: /titanic_50/usr/src/lib/libpkg/common/progerr.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*5c51f124SMoriah Waterland /* All Rights Reserved */
29*5c51f124SMoriah Waterland 
30*5c51f124SMoriah Waterland 
31*5c51f124SMoriah Waterland 
32*5c51f124SMoriah Waterland #include <stdio.h>
33*5c51f124SMoriah Waterland #include <stdarg.h>
34*5c51f124SMoriah Waterland #include <unistd.h>
35*5c51f124SMoriah Waterland #include <string.h>
36*5c51f124SMoriah Waterland #include <stdlib.h>
37*5c51f124SMoriah Waterland #include <errno.h>
38*5c51f124SMoriah Waterland #include "pkglocale.h"
39*5c51f124SMoriah Waterland #include "pkgerr.h"
40*5c51f124SMoriah Waterland 
41*5c51f124SMoriah Waterland static char	*ProgName = NULL; 	/* Set via set_prog_name() */
42*5c51f124SMoriah Waterland 
43*5c51f124SMoriah Waterland 
44*5c51f124SMoriah Waterland static void
45*5c51f124SMoriah Waterland error_and_exit(int error_num)
46*5c51f124SMoriah Waterland {
47*5c51f124SMoriah Waterland 	(void) fprintf(stderr, "%d\n", error_num);
48*5c51f124SMoriah Waterland 	exit(99);
49*5c51f124SMoriah Waterland }
50*5c51f124SMoriah Waterland 
51*5c51f124SMoriah Waterland static void	(*fatal_err_func)() = &error_and_exit;
52*5c51f124SMoriah Waterland 
53*5c51f124SMoriah Waterland char *
54*5c51f124SMoriah Waterland set_prog_name(char *name)
55*5c51f124SMoriah Waterland {
56*5c51f124SMoriah Waterland 	if (name == NULL)
57*5c51f124SMoriah Waterland 		return (NULL);
58*5c51f124SMoriah Waterland 	if ((name = strdup(name)) == NULL) {
59*5c51f124SMoriah Waterland 		(void) fprintf(stderr,
60*5c51f124SMoriah Waterland 		    "set_prog_name(): strdup(name) failed.\n");
61*5c51f124SMoriah Waterland 		exit(1);
62*5c51f124SMoriah Waterland 	}
63*5c51f124SMoriah Waterland 	ProgName = strrchr(name, '/');
64*5c51f124SMoriah Waterland 	if (!ProgName++)
65*5c51f124SMoriah Waterland 		ProgName = name;
66*5c51f124SMoriah Waterland 
67*5c51f124SMoriah Waterland 	return (ProgName);
68*5c51f124SMoriah Waterland }
69*5c51f124SMoriah Waterland 
70*5c51f124SMoriah Waterland char *
71*5c51f124SMoriah Waterland get_prog_name(void)
72*5c51f124SMoriah Waterland {
73*5c51f124SMoriah Waterland 	return (ProgName);
74*5c51f124SMoriah Waterland }
75*5c51f124SMoriah Waterland 
76*5c51f124SMoriah Waterland 
77*5c51f124SMoriah Waterland /*VARARGS*/
78*5c51f124SMoriah Waterland void
79*5c51f124SMoriah Waterland progerr(char *fmt, ...)
80*5c51f124SMoriah Waterland {
81*5c51f124SMoriah Waterland 	va_list ap;
82*5c51f124SMoriah Waterland 
83*5c51f124SMoriah Waterland 	va_start(ap, fmt);
84*5c51f124SMoriah Waterland 
85*5c51f124SMoriah Waterland 	if (ProgName && *ProgName)
86*5c51f124SMoriah Waterland 		(void) fprintf(stderr, pkg_gt("%s: ERROR: "), ProgName);
87*5c51f124SMoriah Waterland 	else
88*5c51f124SMoriah Waterland 		(void) fprintf(stderr, pkg_gt(" ERROR: "));
89*5c51f124SMoriah Waterland 
90*5c51f124SMoriah Waterland 	(void) vfprintf(stderr, fmt, ap);
91*5c51f124SMoriah Waterland 
92*5c51f124SMoriah Waterland 	va_end(ap);
93*5c51f124SMoriah Waterland 
94*5c51f124SMoriah Waterland 	(void) fprintf(stderr, "\n");
95*5c51f124SMoriah Waterland }
96*5c51f124SMoriah Waterland 
97*5c51f124SMoriah Waterland void
98*5c51f124SMoriah Waterland pkgerr(PKG_ERR *err)
99*5c51f124SMoriah Waterland {
100*5c51f124SMoriah Waterland 	int i;
101*5c51f124SMoriah Waterland 
102*5c51f124SMoriah Waterland 	for (i = 0; i < pkgerr_num(err); i++) {
103*5c51f124SMoriah Waterland 		progerr("%s", pkgerr_get(err, i));
104*5c51f124SMoriah Waterland 	}
105*5c51f124SMoriah Waterland }
106*5c51f124SMoriah Waterland 
107*5c51f124SMoriah Waterland 
108*5c51f124SMoriah Waterland /*
109*5c51f124SMoriah Waterland  * set_memalloc_failure_func()
110*5c51f124SMoriah Waterland  *	Allows an appliation to specify the function to be called when
111*5c51f124SMoriah Waterland  *	a memory allocation function fails.
112*5c51f124SMoriah Waterland  * Parameters:
113*5c51f124SMoriah Waterland  *	(*alloc_proc)(int)	- specifies the function to call if fatal error
114*5c51f124SMoriah Waterland  *			  (such as being unable to allocate memory) occurs.
115*5c51f124SMoriah Waterland  * Return:
116*5c51f124SMoriah Waterland  *	none
117*5c51f124SMoriah Waterland  * Status:
118*5c51f124SMoriah Waterland  *	Public
119*5c51f124SMoriah Waterland  */
120*5c51f124SMoriah Waterland void
121*5c51f124SMoriah Waterland set_memalloc_failure_func(void (*alloc_proc)(int))
122*5c51f124SMoriah Waterland {
123*5c51f124SMoriah Waterland 	if (alloc_proc != (void (*)())NULL)
124*5c51f124SMoriah Waterland 		fatal_err_func = alloc_proc;
125*5c51f124SMoriah Waterland }
126*5c51f124SMoriah Waterland 
127*5c51f124SMoriah Waterland /*
128*5c51f124SMoriah Waterland  * xmalloc()
129*5c51f124SMoriah Waterland  * 	Alloc 'size' bytes from heap using malloc()
130*5c51f124SMoriah Waterland  * Parameters:
131*5c51f124SMoriah Waterland  *	size	- number of bytes to malloc
132*5c51f124SMoriah Waterland  * Return:
133*5c51f124SMoriah Waterland  *	NULL	- malloc() failure
134*5c51f124SMoriah Waterland  *	void *	- pointer to allocated structure
135*5c51f124SMoriah Waterland  * Status:
136*5c51f124SMoriah Waterland  *	public
137*5c51f124SMoriah Waterland  */
138*5c51f124SMoriah Waterland void *
139*5c51f124SMoriah Waterland xmalloc(size_t size)
140*5c51f124SMoriah Waterland {
141*5c51f124SMoriah Waterland 	void *tmp;
142*5c51f124SMoriah Waterland 
143*5c51f124SMoriah Waterland 	if ((tmp = (void *) malloc(size)) == NULL) {
144*5c51f124SMoriah Waterland 		fatal_err_func(errno);
145*5c51f124SMoriah Waterland 		return (NULL);
146*5c51f124SMoriah Waterland 	} else
147*5c51f124SMoriah Waterland 		return (tmp);
148*5c51f124SMoriah Waterland }
149*5c51f124SMoriah Waterland 
150*5c51f124SMoriah Waterland /*
151*5c51f124SMoriah Waterland  * xrealloc()
152*5c51f124SMoriah Waterland  *	Calls realloc() with the specfied parameters. xrealloc()
153*5c51f124SMoriah Waterland  *	checks for realloc failures and adjusts the return value
154*5c51f124SMoriah Waterland  *	automatically.
155*5c51f124SMoriah Waterland  * Parameters:
156*5c51f124SMoriah Waterland  *	ptr	- pointer to existing data block
157*5c51f124SMoriah Waterland  * 	size	- number of bytes additional
158*5c51f124SMoriah Waterland  * Return:
159*5c51f124SMoriah Waterland  *	NULL	- realloc() failed
160*5c51f124SMoriah Waterland  *	void *	- pointer to realloc'd structured
161*5c51f124SMoriah Waterland  * Status:
162*5c51f124SMoriah Waterland  *	public
163*5c51f124SMoriah Waterland  */
164*5c51f124SMoriah Waterland void *
165*5c51f124SMoriah Waterland xrealloc(void *ptr, size_t size)
166*5c51f124SMoriah Waterland {
167*5c51f124SMoriah Waterland 	void *tmp;
168*5c51f124SMoriah Waterland 
169*5c51f124SMoriah Waterland 	if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
170*5c51f124SMoriah Waterland 		fatal_err_func(errno);
171*5c51f124SMoriah Waterland 		return ((void *)NULL);
172*5c51f124SMoriah Waterland 	} else
173*5c51f124SMoriah Waterland 		return (tmp);
174*5c51f124SMoriah Waterland }
175*5c51f124SMoriah Waterland 
176*5c51f124SMoriah Waterland /*
177*5c51f124SMoriah Waterland  * xstrdup()
178*5c51f124SMoriah Waterland  *	Allocate space for the string from the heap, copy 'str' into it,
179*5c51f124SMoriah Waterland  *	and return a pointer to it.
180*5c51f124SMoriah Waterland  * Parameters:
181*5c51f124SMoriah Waterland  *	str	- string to duplicate
182*5c51f124SMoriah Waterland  * Return:
183*5c51f124SMoriah Waterland  *	NULL	- duplication failed or 'str' was NULL
184*5c51f124SMoriah Waterland  * 	char *	- pointer to newly allocated/initialized structure
185*5c51f124SMoriah Waterland  * Status:
186*5c51f124SMoriah Waterland  *	public
187*5c51f124SMoriah Waterland  */
188*5c51f124SMoriah Waterland char *
189*5c51f124SMoriah Waterland xstrdup(char *str)
190*5c51f124SMoriah Waterland {
191*5c51f124SMoriah Waterland 	char *tmp;
192*5c51f124SMoriah Waterland 
193*5c51f124SMoriah Waterland 	if (str == NULL)
194*5c51f124SMoriah Waterland 		return ((char *)NULL);
195*5c51f124SMoriah Waterland 
196*5c51f124SMoriah Waterland 	if ((tmp = strdup(str)) == NULL) {
197*5c51f124SMoriah Waterland 		fatal_err_func(errno);
198*5c51f124SMoriah Waterland 		return ((char *)NULL);
199*5c51f124SMoriah Waterland 	} else
200*5c51f124SMoriah Waterland 		return (tmp);
201*5c51f124SMoriah Waterland }
202