xref: /illumos-gate/usr/src/cmd/eeprom/common/error.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright 1989-2003 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <errno.h>
32 
33 #define	NO_PERROR	0
34 #define	PERROR		1
35 
36 char *progname;
37 
38 void
39 setprogname(char *name)
40 {
41 	register char *p = name, c;
42 
43 	if (p)
44 		while (c = *p++)
45 			if (c == '/')
46 				name = p;
47 
48 	progname = name;
49 }
50 
51 /* _error([no_perror, ] fmt [, arg ...]) */
52 /*VARARGS*/
53 int
54 _error(int do_perror, char *fmt, ...)
55 {
56 	int saved_errno;
57 	va_list ap;
58 
59 	saved_errno = errno;
60 
61 	/*
62 	 * flush all buffers
63 	 */
64 	(void) fflush(NULL);
65 	if (progname)
66 		(void) fprintf(stderr, "%s: ", progname);
67 
68 	va_start(ap, fmt);
69 	(void) vfprintf(stderr, fmt, ap);
70 	va_end(ap);
71 
72 	if (do_perror == NO_PERROR)
73 		(void) fprintf(stderr, "\n");
74 	else {
75 		(void) fprintf(stderr, ": ");
76 		errno = saved_errno;
77 		perror("");
78 	}
79 
80 	return (1);
81 }
82