xref: /illumos-gate/usr/src/cmd/zpool/zpool_util.c (revision f00e6aa6)
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 2006 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 <errno.h>
30 #include <libgen.h>
31 #include <libintl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 
36 #include "zpool_util.h"
37 
38 /*
39  * Utility function to guarantee malloc() success.
40  */
41 void *
42 safe_malloc(size_t size)
43 {
44 	void *data;
45 
46 	if ((data = calloc(1, size)) == NULL) {
47 		(void) fprintf(stderr, "internal error: out of memory\n");
48 		exit(1);
49 	}
50 
51 	return (data);
52 }
53 
54 /*
55  * Same as above, but for strdup()
56  */
57 char *
58 safe_strdup(const char *str)
59 {
60 	char *ret;
61 
62 	if ((ret = strdup(str)) == NULL) {
63 		(void) fprintf(stderr, "internal error: out of memory\n");
64 		exit(1);
65 	}
66 
67 	return (ret);
68 }
69 
70 /*
71  * Display an out of memory error message and abort the current program.
72  */
73 void
74 no_memory(void)
75 {
76 	assert(errno == ENOMEM);
77 	(void) fprintf(stderr,
78 	    gettext("internal error: out of memory\n"));
79 	exit(1);
80 }
81