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 (c) 1998-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27#pragma ident	"@(#)utils.c	1.2	05/06/08 SMI"
28
29#include <string.h>
30#include <stdlib.h>
31#include <stdarg.h>
32#include <stdio.h>
33#include <errno.h>
34
35#include "utils.h"
36
37/*LINTLIBRARY*/
38
39static const char *pname;
40
41#if !defined(__APPLE__)
42#pragma init(getpname)
43const char *
44getpname(void)
45{
46	const char *p, *q;
47
48	if (pname != NULL)
49		return (pname);
50
51	if ((p = getexecname()) != NULL)
52		q = strrchr(p, '/');
53	else
54		q = NULL;
55
56	if (q == NULL)
57		pname = p;
58	else
59		pname = q + 1;
60
61	return (pname);
62}
63#endif /* __APPLE__ */
64
65void
66vwarn(const char *format, va_list alist)
67{
68	int err = errno;
69
70	if (pname != NULL)
71		(void) fprintf(stderr, "%s: ", pname);
72
73	(void) vfprintf(stderr, format, alist);
74
75	if (strchr(format, '\n') == NULL)
76		(void) fprintf(stderr, ": %s\n", strerror(err));
77}
78
79/*PRINTFLIKE1*/
80void
81warn(const char *format, ...)
82{
83	va_list alist;
84
85	va_start(alist, format);
86	vwarn(format, alist);
87	va_end(alist);
88}
89
90void
91vdie(const char *format, va_list alist)
92{
93	vwarn(format, alist);
94	exit(E_ERROR);
95}
96
97/*PRINTFLIKE1*/
98void
99die(const char *format, ...)
100{
101	va_list alist;
102
103	va_start(alist, format);
104	vdie(format, alist);
105	va_end(alist);
106}
107