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