xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/gen/eprintf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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) 1996, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * MKS library
31  * Copyright 1985, 1992 by Mortice Kern Systems Inc.  All rights reserved.
32  *
33  */
34 #ifdef M_RCSID
35 #ifndef lint
36 static char rcsID[] = "$Header: /rd/src/libc/gen/rcs/eprintf.c 1.17 1994/06/17 19:42:34 hilary Exp $";
37 #endif
38 #endif
39 
40 #include <mks.h>
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <string.h>
45 
46 char *_cmdname;
47 
48 /*f
49  * print error message followed by errno value.
50  * The value of errno is guaranteed to be restored on exit.
51  */
52 /* VARARGS0 */
53 LDEFN int
54 eprintf VARARG1(const char *, fmt)
55 {
56 	va_list args;
57 	register int saveerrno = errno;
58 	register int nprf = 0;
59 	char *str;
60 
61 	if (_cmdname != NULL)
62 		nprf += fprintf(stderr, "%s: ", _cmdname);
63 	va_start(args, fmt);
64 	nprf += vfprintf(stderr, fmt, args);
65 	va_end(args);
66 	str = strerror(saveerrno);
67 	if (*str == '\0')
68 		nprf += fprintf(stderr, ": error %d\n", saveerrno);
69 	else
70 		nprf += fprintf(stderr,": %s\n", str);
71 	errno = saveerrno;
72 	return (nprf);
73 }
74