vdprintf.c revision 1.1
11.1Schristos/*	$NetBSD: vdprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 1990, 1993
51.1Schristos *	The Regents of the University of California.  All rights reserved.
61.1Schristos *
71.1Schristos * This code is derived from software contributed to Berkeley by
81.1Schristos * Chris Torek.
91.1Schristos *
101.1Schristos * Redistribution and use in source and binary forms, with or without
111.1Schristos * modification, are permitted provided that the following conditions
121.1Schristos * are met:
131.1Schristos * 1. Redistributions of source code must retain the above copyright
141.1Schristos *    notice, this list of conditions and the following disclaimer.
151.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
161.1Schristos *    notice, this list of conditions and the following disclaimer in the
171.1Schristos *    documentation and/or other materials provided with the distribution.
181.1Schristos * 3. Neither the name of the University nor the names of its contributors
191.1Schristos *    may be used to endorse or promote products derived from this software
201.1Schristos *    without specific prior written permission.
211.1Schristos *
221.1Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.1Schristos * SUCH DAMAGE.
331.1Schristos */
341.1Schristos
351.1Schristos#include <sys/cdefs.h>
361.1Schristos#if defined(LIBC_SCCS) && !defined(lint)
371.1Schristos__RCSID("$NetBSD: vdprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $");
381.1Schristos#endif /* LIBC_SCCS and not lint */
391.1Schristos
401.1Schristos#include "namespace.h"
411.1Schristos#include <sys/types.h>
421.1Schristos
431.1Schristos#include <assert.h>
441.1Schristos#include <errno.h>
451.1Schristos#include <fcntl.h>
461.1Schristos#include <unistd.h>
471.1Schristos#include <stdio.h>
481.1Schristos#include <limits.h>
491.1Schristos
501.1Schristos#include "reentrant.h"
511.1Schristos#include "local.h"
521.1Schristos
531.1Schristos#ifdef __weak_alias
541.1Schristos__weak_alias(vdprintf,_vdprintf)
551.1Schristos#endif
561.1Schristos
571.1Schristosint
581.1Schristosvdprintf(int fd, const char * __restrict fmt, _BSD_VA_LIST_ ap)
591.1Schristos{
601.1Schristos	FILE f;
611.1Schristos	struct __sfileext fext;
621.1Schristos	unsigned char buf[BUFSIZ];
631.1Schristos	int ret, fdflags, tmp;
641.1Schristos
651.1Schristos	_DIAGASSERT(fd != -1);
661.1Schristos
671.1Schristos	/*
681.1Schristos	 * File descriptors are a full int, but _file is only a short.
691.1Schristos	 * If we get a valid file descriptor that is greater or equal to
701.1Schristos	 * USHRT_MAX, then the fd will get sign-extended into an
711.1Schristos	 * invalid file descriptor.  Handle this case by failing the
721.1Schristos	 * open. (We treat the short as unsigned, and special-case -1).
731.1Schristos	 */
741.1Schristos	if (fd >= USHRT_MAX) {
751.1Schristos		errno = EMFILE;
761.1Schristos		return EOF;
771.1Schristos	}
781.1Schristos
791.1Schristos	if ((fdflags = fcntl(fd, F_GETFL, 0)) == -1)
801.1Schristos		return EOF;
811.1Schristos
821.1Schristos	tmp = fdflags & O_ACCMODE;
831.1Schristos	if (tmp != O_RDWR && tmp != O_WRONLY) {
841.1Schristos		errno = EINVAL;
851.1Schristos		return EOF;
861.1Schristos	}
871.1Schristos
881.1Schristos	if (fdflags & O_NONBLOCK) {
891.1Schristos		struct stat st;
901.1Schristos		if (fstat(fd, &st) == -1)
911.1Schristos			return -1;
921.1Schristos		if (!S_ISREG(st.st_mode)) {
931.1Schristos			errno = EFTYPE;
941.1Schristos			return EOF;
951.1Schristos		}
961.1Schristos	}
971.1Schristos
981.1Schristos	_FILEEXT_SETUP(&f, &fext);
991.1Schristos	__sfpinit(&f);
1001.1Schristos	f._p = buf;
1011.1Schristos	f._w = sizeof(buf);
1021.1Schristos	f._flags = __SWR;
1031.1Schristos	f._file = fd;
1041.1Schristos	f._bf._base = buf;
1051.1Schristos	f._bf._size = sizeof(buf);
1061.1Schristos	f._cookie = &f;
1071.1Schristos	f._read = NULL;
1081.1Schristos	f._write = __swrite;
1091.1Schristos	f._seek = NULL;
1101.1Schristos	f._close = NULL;
1111.1Schristos
1121.1Schristos	if ((ret = vfprintf(&f, fmt, ap)) < 0)
1131.1Schristos		return ret;
1141.1Schristos
1151.1Schristos	return fflush(&f) ? EOF : ret;
1161.1Schristos}
117