xref: /illumos-gate/usr/src/cmd/rctladm/utils.c (revision d3d50737e566cade9a08d73d2af95105ac7cd960)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/param.h>
29 #include <libintl.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <errno.h>
35 
36 #include "utils.h"
37 
38 static char PNAME_FMT[] = "%s: ";
39 static char ERRNO_FMT[] = ": %s\n";
40 
41 static char *pname;
42 
43 /*PRINTFLIKE1*/
44 void
45 warn(const char *format, ...)
46 {
47 	int err = errno;
48 	va_list alist;
49 	if (pname != NULL)
50 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
51 	va_start(alist, format);
52 	(void) vfprintf(stderr, format, alist);
53 	va_end(alist);
54 	if (strchr(format, '\n') == NULL)
55 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
56 }
57 
58 /*PRINTFLIKE1*/
59 void
60 die(char *format, ...)
61 {
62 	int err = errno;
63 	va_list alist;
64 
65 	if (pname != NULL)
66 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
67 	va_start(alist, format);
68 	(void) vfprintf(stderr, format, alist);
69 	va_end(alist);
70 	if (strchr(format, '\n') == NULL)
71 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
72 	exit(E_ERROR);
73 }
74 
75 char *
76 setprogname(char *arg0)
77 {
78 	char *p = strrchr(arg0, '/');
79 
80 	if (p == NULL)
81 		p = arg0;
82 	else
83 		p++;
84 	pname = p;
85 	return (pname);
86 }
87