xref: /illumos-gate/usr/src/cmd/msgfmt/util.c (revision a28480febf31f0e61debac062a55216a98a05a92)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "common.h"
30 
31 /*PRINTFLIKE1*/
32 void
33 error(char *err, ...)
34 {
35 	va_list	ap;
36 	va_start(ap, err);
37 
38 	(void) fprintf(stderr, gettext(ERR_ERROR));
39 	(void) vfprintf(stderr, err, ap);
40 	va_end(ap);
41 	exit(2);
42 }
43 
44 /*PRINTFLIKE1*/
45 void
46 warning(char *err, ...)
47 {
48 	va_list	ap;
49 	va_start(ap, err);
50 
51 	(void) fprintf(stderr, gettext(WARN_WARNING));
52 	(void) vfprintf(stderr, err, ap);
53 	va_end(ap);
54 }
55 
56 /*PRINTFLIKE1*/
57 void
58 diag(char *err, ...)
59 {
60 	va_list	ap;
61 	va_start(ap, err);
62 
63 	(void) vfprintf(stderr, err, ap);
64 	va_end(ap);
65 }
66 
67 void	*
68 Xmalloc(size_t size)
69 {
70 	void	*t;
71 
72 	t = malloc(size);
73 	if (!t) {
74 		error(gettext(ERR_MALLOC));
75 		/* NOTREACHED */
76 	}
77 	return (t);
78 }
79 
80 void	*
81 Xcalloc(size_t nelem, size_t elsize)
82 {
83 	void	*t;
84 
85 	t = calloc(nelem, elsize);
86 	if (!t) {
87 		error(gettext(ERR_MALLOC));
88 		/* NOTREACHED */
89 	}
90 	return (t);
91 }
92 
93 void	*
94 Xrealloc(void *ptr, size_t size)
95 {
96 	void	*t;
97 
98 	t = realloc(ptr, size);
99 	if (!t) {
100 		free(ptr);
101 		error(gettext(ERR_MALLOC));
102 		/* NOTREACHED */
103 	}
104 	return (t);
105 }
106 
107 char	*
108 Xstrdup(const char *str)
109 {
110 	char	*t;
111 
112 	t = strdup(str);
113 	if (!t) {
114 		error(gettext(ERR_MALLOC));
115 		/* NOTREACHED */
116 	}
117 	return (t);
118 }
119