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