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