xref: /freebsd/lib/libc/stdio/xprintf_errno.c (revision 6dbacee284ac789077c3f2a181437ff5a7ebb3b8)
16dbacee2SPoul-Henning Kamp /*-
26dbacee2SPoul-Henning Kamp  * Copyright (c) 2005 Poul-Henning Kamp
36dbacee2SPoul-Henning Kamp  * All rights reserved.
46dbacee2SPoul-Henning Kamp  *
56dbacee2SPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
66dbacee2SPoul-Henning Kamp  * modification, are permitted provided that the following conditions
76dbacee2SPoul-Henning Kamp  * are met:
86dbacee2SPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
96dbacee2SPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
106dbacee2SPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
116dbacee2SPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
126dbacee2SPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
136dbacee2SPoul-Henning Kamp  *
146dbacee2SPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156dbacee2SPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166dbacee2SPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176dbacee2SPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
186dbacee2SPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196dbacee2SPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206dbacee2SPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216dbacee2SPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226dbacee2SPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236dbacee2SPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246dbacee2SPoul-Henning Kamp  * SUCH DAMAGE.
256dbacee2SPoul-Henning Kamp  *
266dbacee2SPoul-Henning Kamp  * $FreeBSD$
276dbacee2SPoul-Henning Kamp  */
286dbacee2SPoul-Henning Kamp 
296dbacee2SPoul-Henning Kamp #include <namespace.h>
306dbacee2SPoul-Henning Kamp #include <stdio.h>
316dbacee2SPoul-Henning Kamp #include <string.h>
326dbacee2SPoul-Henning Kamp #include <stdlib.h>
336dbacee2SPoul-Henning Kamp #include <wchar.h>
346dbacee2SPoul-Henning Kamp #include <vis.h>
356dbacee2SPoul-Henning Kamp #include <assert.h>
366dbacee2SPoul-Henning Kamp #include <sys/time.h>
376dbacee2SPoul-Henning Kamp #include "printf.h"
386dbacee2SPoul-Henning Kamp 
396dbacee2SPoul-Henning Kamp int
406dbacee2SPoul-Henning Kamp __printf_arginfo_errno(const struct printf_info *pi __unused, size_t n, int *argt)
416dbacee2SPoul-Henning Kamp {
426dbacee2SPoul-Henning Kamp 
436dbacee2SPoul-Henning Kamp 	assert(n >= 1);
446dbacee2SPoul-Henning Kamp 	argt[0] = PA_INT;
456dbacee2SPoul-Henning Kamp 	return (1);
466dbacee2SPoul-Henning Kamp }
476dbacee2SPoul-Henning Kamp 
486dbacee2SPoul-Henning Kamp int
496dbacee2SPoul-Henning Kamp __printf_render_errno(struct __printf_io *io, const struct printf_info *pi __unused, const void *const *arg)
506dbacee2SPoul-Henning Kamp {
516dbacee2SPoul-Henning Kamp 	int ret, error;
526dbacee2SPoul-Henning Kamp 	char buf[64];
536dbacee2SPoul-Henning Kamp 	const char *p;
546dbacee2SPoul-Henning Kamp 
556dbacee2SPoul-Henning Kamp 	ret = 0;
566dbacee2SPoul-Henning Kamp 	error = *((const int *)arg[0]);
576dbacee2SPoul-Henning Kamp 	if (error >= 0 && error < sys_nerr) {
586dbacee2SPoul-Henning Kamp 		p = strerror(error);
596dbacee2SPoul-Henning Kamp 		return (__printf_out(io, pi, p, strlen(p)));
606dbacee2SPoul-Henning Kamp 	}
616dbacee2SPoul-Henning Kamp 	sprintf(buf, "errno=%d/0x%x", error, error);
626dbacee2SPoul-Henning Kamp 	ret += __printf_out(io, pi, buf, strlen(buf));
636dbacee2SPoul-Henning Kamp 	__printf_flush(io);
646dbacee2SPoul-Henning Kamp 	return(ret);
656dbacee2SPoul-Henning Kamp }
66