xref: /netbsd-src/lib/libc/stdio/vdprintf.c (revision 93b012cb25794577ed8e68ee2350c1273c7ba68f)
1*93b012cbSchristos /*	$NetBSD: vdprintf.c,v 1.6 2020/08/28 22:02:24 christos Exp $	*/
24ddc2fb3Schristos 
34ddc2fb3Schristos /*-
44ddc2fb3Schristos  * Copyright (c) 1990, 1993
54ddc2fb3Schristos  *	The Regents of the University of California.  All rights reserved.
64ddc2fb3Schristos  *
74ddc2fb3Schristos  * This code is derived from software contributed to Berkeley by
84ddc2fb3Schristos  * Chris Torek.
94ddc2fb3Schristos  *
104ddc2fb3Schristos  * Redistribution and use in source and binary forms, with or without
114ddc2fb3Schristos  * modification, are permitted provided that the following conditions
124ddc2fb3Schristos  * are met:
134ddc2fb3Schristos  * 1. Redistributions of source code must retain the above copyright
144ddc2fb3Schristos  *    notice, this list of conditions and the following disclaimer.
154ddc2fb3Schristos  * 2. Redistributions in binary form must reproduce the above copyright
164ddc2fb3Schristos  *    notice, this list of conditions and the following disclaimer in the
174ddc2fb3Schristos  *    documentation and/or other materials provided with the distribution.
184ddc2fb3Schristos  * 3. Neither the name of the University nor the names of its contributors
194ddc2fb3Schristos  *    may be used to endorse or promote products derived from this software
204ddc2fb3Schristos  *    without specific prior written permission.
214ddc2fb3Schristos  *
224ddc2fb3Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234ddc2fb3Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244ddc2fb3Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254ddc2fb3Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264ddc2fb3Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274ddc2fb3Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284ddc2fb3Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294ddc2fb3Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304ddc2fb3Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314ddc2fb3Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324ddc2fb3Schristos  * SUCH DAMAGE.
334ddc2fb3Schristos  */
344ddc2fb3Schristos 
354ddc2fb3Schristos #include <sys/cdefs.h>
364ddc2fb3Schristos #if defined(LIBC_SCCS) && !defined(lint)
37*93b012cbSchristos __RCSID("$NetBSD: vdprintf.c,v 1.6 2020/08/28 22:02:24 christos Exp $");
384ddc2fb3Schristos #endif /* LIBC_SCCS and not lint */
394ddc2fb3Schristos 
404ddc2fb3Schristos #include "namespace.h"
414ddc2fb3Schristos #include <sys/types.h>
424ddc2fb3Schristos 
434ddc2fb3Schristos #include <assert.h>
444ddc2fb3Schristos #include <errno.h>
454ddc2fb3Schristos #include <fcntl.h>
462561b634Sjoerg #include <locale.h>
474ddc2fb3Schristos #include <unistd.h>
484ddc2fb3Schristos #include <stdio.h>
494ddc2fb3Schristos #include <limits.h>
504ddc2fb3Schristos 
514ddc2fb3Schristos #include "reentrant.h"
522561b634Sjoerg #include "setlocale_local.h"
534ddc2fb3Schristos #include "local.h"
544ddc2fb3Schristos 
554ddc2fb3Schristos #ifdef __weak_alias
__weak_alias(vdprintf,_vdprintf)564ddc2fb3Schristos __weak_alias(vdprintf,_vdprintf)
572561b634Sjoerg __weak_alias(vdprintf_l,_vdprintf_l)
584ddc2fb3Schristos #endif
594ddc2fb3Schristos 
604ddc2fb3Schristos int
612561b634Sjoerg vdprintf_l(int fd, locale_t loc, const char * __restrict fmt, va_list ap)
624ddc2fb3Schristos {
634ddc2fb3Schristos 	FILE f;
644ddc2fb3Schristos 	struct __sfileext fext;
654ddc2fb3Schristos 	unsigned char buf[BUFSIZ];
664ddc2fb3Schristos 	int ret, fdflags, tmp;
674ddc2fb3Schristos 
684ddc2fb3Schristos 	_DIAGASSERT(fd != -1);
694ddc2fb3Schristos 
704ddc2fb3Schristos 	/*
714ddc2fb3Schristos 	 * File descriptors are a full int, but _file is only a short.
724ddc2fb3Schristos 	 * If we get a valid file descriptor that is greater or equal to
734ddc2fb3Schristos 	 * USHRT_MAX, then the fd will get sign-extended into an
744ddc2fb3Schristos 	 * invalid file descriptor.  Handle this case by failing the
754ddc2fb3Schristos 	 * open. (We treat the short as unsigned, and special-case -1).
764ddc2fb3Schristos 	 */
774ddc2fb3Schristos 	if (fd >= USHRT_MAX) {
784ddc2fb3Schristos 		errno = EMFILE;
794ddc2fb3Schristos 		return EOF;
804ddc2fb3Schristos 	}
814ddc2fb3Schristos 
824ddc2fb3Schristos 	if ((fdflags = fcntl(fd, F_GETFL, 0)) == -1)
834ddc2fb3Schristos 		return EOF;
844ddc2fb3Schristos 
854ddc2fb3Schristos 	tmp = fdflags & O_ACCMODE;
864ddc2fb3Schristos 	if (tmp != O_RDWR && tmp != O_WRONLY) {
874ddc2fb3Schristos 		errno = EINVAL;
884ddc2fb3Schristos 		return EOF;
894ddc2fb3Schristos 	}
904ddc2fb3Schristos 
914ddc2fb3Schristos 	_FILEEXT_SETUP(&f, &fext);
924ddc2fb3Schristos 	__sfpinit(&f);
934ddc2fb3Schristos 	f._p = buf;
944ddc2fb3Schristos 	f._w = sizeof(buf);
954ddc2fb3Schristos 	f._flags = __SWR;
964ddc2fb3Schristos 	f._file = fd;
974ddc2fb3Schristos 	f._bf._base = buf;
984ddc2fb3Schristos 	f._bf._size = sizeof(buf);
994ddc2fb3Schristos 	f._cookie = &f;
1004ddc2fb3Schristos 	f._read = NULL;
1014ddc2fb3Schristos 	f._write = __swrite;
1024ddc2fb3Schristos 	f._seek = NULL;
1034ddc2fb3Schristos 	f._close = NULL;
1044ddc2fb3Schristos 
1052561b634Sjoerg 	if ((ret = vfprintf_l(&f, loc, fmt, ap)) < 0)
1064ddc2fb3Schristos 		return ret;
1074ddc2fb3Schristos 
1084ddc2fb3Schristos 	return fflush(&f) ? EOF : ret;
1094ddc2fb3Schristos }
1102561b634Sjoerg 
1112561b634Sjoerg int
vdprintf(int fd,const char * __restrict fmt,va_list ap)1122561b634Sjoerg vdprintf(int fd, const char * __restrict fmt, va_list ap)
1132561b634Sjoerg {
114e0ac190eSjoerg 	return vdprintf_l(fd, _current_locale(), fmt, ap);
1152561b634Sjoerg }
116