xref: /freebsd/lib/libc/stdio/xprintf_errno.c (revision e73151eb827122b6da662018d051ba8f58ef7e82)
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>
37*e73151ebSJilles Tjoelker #include "errlst.h"
386dbacee2SPoul-Henning Kamp #include "printf.h"
396dbacee2SPoul-Henning Kamp 
406dbacee2SPoul-Henning Kamp int
416dbacee2SPoul-Henning Kamp __printf_arginfo_errno(const struct printf_info *pi __unused, size_t n, int *argt)
426dbacee2SPoul-Henning Kamp {
436dbacee2SPoul-Henning Kamp 
446dbacee2SPoul-Henning Kamp 	assert(n >= 1);
456dbacee2SPoul-Henning Kamp 	argt[0] = PA_INT;
466dbacee2SPoul-Henning Kamp 	return (1);
476dbacee2SPoul-Henning Kamp }
486dbacee2SPoul-Henning Kamp 
496dbacee2SPoul-Henning Kamp int
506dbacee2SPoul-Henning Kamp __printf_render_errno(struct __printf_io *io, const struct printf_info *pi __unused, const void *const *arg)
516dbacee2SPoul-Henning Kamp {
526dbacee2SPoul-Henning Kamp 	int ret, error;
536dbacee2SPoul-Henning Kamp 	char buf[64];
546dbacee2SPoul-Henning Kamp 	const char *p;
556dbacee2SPoul-Henning Kamp 
566dbacee2SPoul-Henning Kamp 	ret = 0;
576dbacee2SPoul-Henning Kamp 	error = *((const int *)arg[0]);
58*e73151ebSJilles Tjoelker 	if (error >= 0 && error < __hidden_sys_nerr) {
596dbacee2SPoul-Henning Kamp 		p = strerror(error);
606dbacee2SPoul-Henning Kamp 		return (__printf_out(io, pi, p, strlen(p)));
616dbacee2SPoul-Henning Kamp 	}
626dbacee2SPoul-Henning Kamp 	sprintf(buf, "errno=%d/0x%x", error, error);
636dbacee2SPoul-Henning Kamp 	ret += __printf_out(io, pi, buf, strlen(buf));
646dbacee2SPoul-Henning Kamp 	__printf_flush(io);
656dbacee2SPoul-Henning Kamp 	return(ret);
666dbacee2SPoul-Henning Kamp }
67