1 /* @(#)serrmsg.c	1.5 09/07/10 Copyright 1985, 2000-2003 J. Schilling */
2 /*
3  *	Routines for printing command errors
4  *
5  *	Copyright (c) 1985, 2000-2003 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #include <schily/mconfig.h>
22 #include <schily/unistd.h>	/* include <sys/types.h> try to get size_t */
23 #include <schily/stdio.h>	/* Try again for size_t	*/
24 #include <schily/stdlib.h>	/* Try again for size_t	*/
25 #include <schily/standard.h>
26 #include <schily/stdlib.h>
27 #include <schily/varargs.h>
28 #include <schily/string.h>
29 #include <schily/schily.h>
30 #ifndef	HAVE_STRERROR
31 extern	char	*sys_errlist[];
32 extern	int	sys_nerr;
33 #endif
34 
35 EXPORT	int	serrmsg		__PR((char *buf, size_t maxcnt, const char *, ...));
36 EXPORT	int	serrmsgno	__PR((int, char *buf, size_t maxcnt, const char *, ...));
37 LOCAL	int	_serrmsg	__PR((int, char *buf, size_t maxcnt, const char *, va_list));
38 
39 /* VARARGS1 */
40 EXPORT int
41 #ifdef	PROTOTYPES
serrmsg(char * buf,size_t maxcnt,const char * msg,...)42 serrmsg(char *buf, size_t maxcnt, const char *msg, ...)
43 #else
44 serrmsg(buf, maxcnt, msg, va_alist)
45 	char	*buf;
46 	size_t	maxcnt;
47 	char	*msg;
48 	va_dcl
49 #endif
50 {
51 	va_list	args;
52 	int	ret;
53 
54 #ifdef	PROTOTYPES
55 	va_start(args, msg);
56 #else
57 	va_start(args);
58 #endif
59 	ret = _serrmsg(geterrno(), buf, maxcnt, msg, args);
60 	va_end(args);
61 	return (ret);
62 }
63 
64 /* VARARGS2 */
65 #ifdef	PROTOTYPES
66 EXPORT int
serrmsgno(int err,char * buf,size_t maxcnt,const char * msg,...)67 serrmsgno(int err, char *buf, size_t maxcnt, const char *msg, ...)
68 #else
69 serrmsgno(err, buf, maxcnt, msg, va_alist)
70 	int	err;
71 	char	*buf;
72 	size_t	maxcnt;
73 	char	*msg;
74 	va_dcl
75 #endif
76 {
77 	va_list	args;
78 	int	ret;
79 
80 #ifdef	PROTOTYPES
81 	va_start(args, msg);
82 #else
83 	va_start(args);
84 #endif
85 	ret = _serrmsg(err, buf, maxcnt, msg, args);
86 	va_end(args);
87 	return (ret);
88 }
89 
90 LOCAL int
_serrmsg(err,buf,maxcnt,msg,args)91 _serrmsg(err, buf, maxcnt, msg, args)
92 	int		err;
93 	char		*buf;
94 	size_t		maxcnt;
95 	const char	*msg;
96 	va_list		args;
97 {
98 	int	ret;
99 	char	errbuf[20];
100 	char	*errnam;
101 	char	*prognam = get_progname();
102 
103 	if (err < 0) {
104 		ret = js_snprintf(buf, maxcnt, "%s: %r", prognam, msg, args);
105 	} else {
106 		errnam = errmsgstr(err);
107 		if (errnam == NULL) {
108 			(void) js_snprintf(errbuf, sizeof (errbuf),
109 						"Error %d", err);
110 			errnam = errbuf;
111 		}
112 		ret = js_snprintf(buf, maxcnt,
113 				"%s: %s. %r", prognam, errnam, msg, args);
114 	}
115 	return (ret);
116 }
117