8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libpkg / common / progerr.c
blob567d77b5814bdf6b6e72e1bc103e2b451ecb5e1d
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2017 Peter Tribble.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
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"
44 static char *ProgName = NULL; /* Set via set_prog_name() */
47 static void
48 error_and_exit(int error_num)
50 (void) fprintf(stderr, "%d\n", error_num);
51 exit(99);
54 static void (*fatal_err_func)() = &error_and_exit;
56 char *
57 set_prog_name(char *name)
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);
66 ProgName = strrchr(name, '/');
67 if (!ProgName++)
68 ProgName = name;
70 return (ProgName);
73 char *
74 get_prog_name(void)
76 return (ProgName);
80 /*PRINTFLIKE1*/
81 void
82 progerr(char *fmt, ...)
84 va_list ap;
86 va_start(ap, fmt);
88 if (ProgName && *ProgName)
89 (void) fprintf(stderr, pkg_gt("%s: ERROR: "), ProgName);
90 else
91 (void) fprintf(stderr, pkg_gt(" ERROR: "));
93 (void) vfprintf(stderr, fmt, ap);
95 va_end(ap);
97 (void) fprintf(stderr, "\n");
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
112 void
113 set_memalloc_failure_func(void (*alloc_proc)(int))
115 if (alloc_proc != (void (*)())NULL)
116 fatal_err_func = alloc_proc;
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
130 void *
131 xmalloc(size_t size)
133 void *tmp;
135 if ((tmp = (void *) malloc(size)) == NULL) {
136 fatal_err_func(errno);
137 return (NULL);
138 } else
139 return (tmp);
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
156 void *
157 xrealloc(void *ptr, size_t size)
159 void *tmp;
161 if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
162 fatal_err_func(errno);
163 return ((void *)NULL);
164 } else
165 return (tmp);
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
180 char *
181 xstrdup(char *str)
183 char *tmp;
185 if (str == NULL)
186 return ((char *)NULL);
188 if ((tmp = strdup(str)) == NULL) {
189 fatal_err_func(errno);
190 return ((char *)NULL);
191 } else
192 return (tmp);