Module Name: src Committed By: christos Date: Mon Sep 6 14:52:55 UTC 2010
Modified Files: src/lib/libc/stdio: Makefile.inc findfp.c local.h printf.3 Added Files: src/lib/libc/stdio: dprintf.c vdprintf.c Log Message: add dprintf and vdprintf To generate a diff of this commit: cvs rdiff -u -r1.36 -r1.37 src/lib/libc/stdio/Makefile.inc cvs rdiff -u -r0 -r1.1 src/lib/libc/stdio/dprintf.c \ src/lib/libc/stdio/vdprintf.c cvs rdiff -u -r1.24 -r1.25 src/lib/libc/stdio/findfp.c \ src/lib/libc/stdio/local.h cvs rdiff -u -r1.52 -r1.53 src/lib/libc/stdio/printf.3 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libc/stdio/Makefile.inc diff -u src/lib/libc/stdio/Makefile.inc:1.36 src/lib/libc/stdio/Makefile.inc:1.37 --- src/lib/libc/stdio/Makefile.inc:1.36 Mon Jul 13 18:19:25 2009 +++ src/lib/libc/stdio/Makefile.inc Mon Sep 6 10:52:55 2010 @@ -1,25 +1,25 @@ # from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91 -# $NetBSD: Makefile.inc,v 1.36 2009/07/13 22:19:25 roy Exp $ +# $NetBSD: Makefile.inc,v 1.37 2010/09/06 14:52:55 christos Exp $ # stdio sources .PATH: ${.CURDIR}/stdio CPPFLAGS+=-DWIDE_DOUBLE -SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \ - fgetc.c fgetln.c fgetpos.c fgets.c fgetstr.c fgetwc.c fgetwln.c \ - fgetws.c fileno.c findfp.c flags.c flockfile.c fopen.c fparseln.c \ - fprintf.c fpurge.c fputc.c fputs.c fputwc.c fputws.c fread.c \ - freopen.c fscanf.c fseek.c fseeko.c fsetpos.c ftell.c ftello.c \ +SRCS+= asprintf.c clrerr.c dprintf.c fclose.c fdopen.c feof.c ferror.c \ + fflush.c fgetc.c fgetln.c fgetpos.c fgets.c fgetstr.c fgetwc.c \ + fgetwln.c fgetws.c fileno.c findfp.c flags.c flockfile.c fopen.c \ + fparseln.c fprintf.c fpurge.c fputc.c fputs.c fputwc.c fputws.c \ + fread.c freopen.c fscanf.c fseek.c fseeko.c fsetpos.c ftell.c ftello.c \ funopen.c fvwrite.c fwalk.c fwide.c fwprintf.c fwrite.c fwscanf.c \ getc.c getchar.c getdelim.c getline.c gettemp.c getw.c getwc.c \ getwchar.c makebuf.c mkdtemp.c mkstemp.c perror.c printf.c putc.c \ putchar.c puts.c putw.c putwc.c putwchar.c refill.c remove.c rewind.c \ rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \ sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \ - vasprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c vprintf.c \ - vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c vswscanf.c \ - vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c + vasprintf.c vdprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \ + vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c \ + vswscanf.c vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c .if !defined(AUDIT) SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c @@ -50,7 +50,8 @@ MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3 MLINKS+=printf.3 asprintf.3 printf.3 fprintf.3 printf.3 snprintf.3 \ printf.3 sprintf.3 printf.3 vasprintf.3 printf.3 vfprintf.3 \ - printf.3 vprintf.3 printf.3 vsnprintf.3 printf.3 vsprintf.3 + printf.3 vprintf.3 printf.3 vsnprintf.3 printf.3 vsprintf.3 \ + printf.3 dprintf.3 printf.3 vdprintf.3 MLINKS+=putc.3 fputc.3 putc.3 putc_unlocked.3 putc.3 putchar.3 \ putc.3 putchar_unlocked.3 putc.3 putw.3 MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 \ Index: src/lib/libc/stdio/findfp.c diff -u src/lib/libc/stdio/findfp.c:1.24 src/lib/libc/stdio/findfp.c:1.25 --- src/lib/libc/stdio/findfp.c:1.24 Mon Jan 11 15:39:29 2010 +++ src/lib/libc/stdio/findfp.c Mon Sep 6 10:52:55 2010 @@ -1,4 +1,4 @@ -/* $NetBSD: findfp.c,v 1.24 2010/01/11 20:39:29 joerg Exp $ */ +/* $NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94"; #else -__RCSID("$NetBSD: findfp.c,v 1.24 2010/01/11 20:39:29 joerg Exp $"); +__RCSID("$NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -119,6 +119,23 @@ return (g); } +void +__sfpinit(FILE *fp) +{ + fp->_flags = 1; /* reserve this slot; caller sets real flags */ + fp->_p = NULL; /* no current pointer */ + fp->_w = 0; /* nothing to read or write */ + fp->_r = 0; + fp->_bf._base = NULL; /* no buffer */ + fp->_bf._size = 0; + fp->_lbfsize = 0; /* not line buffered */ + fp->_file = -1; /* no file */ +/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ + _UB(fp)._base = NULL; /* no ungetc buffer */ + _UB(fp)._size = 0; + memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data)); +} + /* * Find a free FILE for fopen et al. */ @@ -143,18 +160,7 @@ rwlock_unlock(&__sfp_lock); return (NULL); found: - fp->_flags = 1; /* reserve this slot; caller sets real flags */ - fp->_p = NULL; /* no current pointer */ - fp->_w = 0; /* nothing to read or write */ - fp->_r = 0; - fp->_bf._base = NULL; /* no buffer */ - fp->_bf._size = 0; - fp->_lbfsize = 0; /* not line buffered */ - fp->_file = -1; /* no file */ -/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ - _UB(fp)._base = NULL; /* no ungetc buffer */ - _UB(fp)._size = 0; - memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data)); + __sfpinit(fp); rwlock_unlock(&__sfp_lock); return (fp); } Index: src/lib/libc/stdio/local.h diff -u src/lib/libc/stdio/local.h:1.24 src/lib/libc/stdio/local.h:1.25 --- src/lib/libc/stdio/local.h:1.24 Mon Jan 11 15:39:29 2010 +++ src/lib/libc/stdio/local.h Mon Sep 6 10:52:55 2010 @@ -1,4 +1,4 @@ -/* $NetBSD: local.h,v 1.24 2010/01/11 20:39:29 joerg Exp $ */ +/* $NetBSD: local.h,v 1.25 2010/09/06 14:52:55 christos Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -44,6 +44,7 @@ extern int __sflush __P((FILE *)); extern FILE *__sfp __P((void)); +extern void __sfpinit __P((FILE *)); extern int __srefill __P((FILE *)); extern int __sread __P((void *, char *, int)); extern int __swrite __P((void *, char const *, int)); Index: src/lib/libc/stdio/printf.3 diff -u src/lib/libc/stdio/printf.3:1.52 src/lib/libc/stdio/printf.3:1.53 --- src/lib/libc/stdio/printf.3:1.52 Thu May 13 17:52:43 2010 +++ src/lib/libc/stdio/printf.3 Mon Sep 6 10:52:55 2010 @@ -1,4 +1,4 @@ -.\" $NetBSD: printf.3,v 1.52 2010/05/13 21:52:43 joerg Exp $ +.\" $NetBSD: printf.3,v 1.53 2010/09/06 14:52:55 christos Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -33,18 +33,20 @@ .\" .\" @(#)printf.3 8.1 (Berkeley) 6/4/93 .\" -.Dd March 21, 2010 +.Dd August 28, 2010 .Dt PRINTF 3 .Os .Sh NAME .Nm printf , .Nm fprintf , +.Nm dprintf .Nm sprintf , .Nm snprintf , .Nm asprintf , .Nm vprintf , .Nm vfprintf , .Nm vsprintf , +.Nm vdprintf , .Nm vsnprintf , .Nm vasprintf .Nd formatted output conversion @@ -57,6 +59,8 @@ .Ft int .Fn fprintf "FILE * restrict stream" "const char * restrict format" ... .Ft int +.Fn dprintf "int fd" "const char * restrict format" ... +.Ft int .Fn sprintf "char * restrict str" "const char * restrict format" ... .Ft int .Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ... @@ -70,6 +74,8 @@ .Ft int .Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap" .Ft int +.Fn vdprintf "int fd" "const char * restrict format" "va_list ap" +.Ft int .Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap" .Ft int .Fn vasprintf "char ** restrict ret" "const char * restrict format" "va_list ap" @@ -92,6 +98,11 @@ .Fn vfprintf write output to the given output .Fa stream ; +.Fn dprintf +and +.Fn vdprintf +write output to the give file descriptor +.Fa fd ; .Fn sprintf , .Fn snprintf , .Fn vsprintf , @@ -814,6 +825,12 @@ and .Fn vasprintf are modeled on the ones that first appeared in the GNU C library. +The functions +.Fn dprintf +and +.Fn vdprintf +are parts of POSIX 200805 and appeared in +.Nx 6.0 . .Sh CAVEATS Because .Fn sprintf Added files: Index: src/lib/libc/stdio/dprintf.c diff -u /dev/null src/lib/libc/stdio/dprintf.c:1.1 --- /dev/null Mon Sep 6 10:52:55 2010 +++ src/lib/libc/stdio/dprintf.c Mon Sep 6 10:52:55 2010 @@ -0,0 +1,59 @@ +/* $NetBSD: dprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: dprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include <sys/types.h> + +#include <stdio.h> +#include <stdarg.h> + +#include "reentrant.h" +#include "local.h" + +int +dprintf(int fd, const char * __restrict fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = vdprintf(fd, fmt, ap); + va_end(ap); + return ret; +} Index: src/lib/libc/stdio/vdprintf.c diff -u /dev/null src/lib/libc/stdio/vdprintf.c:1.1 --- /dev/null Mon Sep 6 10:52:55 2010 +++ src/lib/libc/stdio/vdprintf.c Mon Sep 6 10:52:55 2010 @@ -0,0 +1,116 @@ +/* $NetBSD: vdprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: vdprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include <sys/types.h> + +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> +#include <limits.h> + +#include "reentrant.h" +#include "local.h" + +#ifdef __weak_alias +__weak_alias(vdprintf,_vdprintf) +#endif + +int +vdprintf(int fd, const char * __restrict fmt, _BSD_VA_LIST_ ap) +{ + FILE f; + struct __sfileext fext; + unsigned char buf[BUFSIZ]; + int ret, fdflags, tmp; + + _DIAGASSERT(fd != -1); + + /* + * File descriptors are a full int, but _file is only a short. + * If we get a valid file descriptor that is greater or equal to + * USHRT_MAX, then the fd will get sign-extended into an + * invalid file descriptor. Handle this case by failing the + * open. (We treat the short as unsigned, and special-case -1). + */ + if (fd >= USHRT_MAX) { + errno = EMFILE; + return EOF; + } + + if ((fdflags = fcntl(fd, F_GETFL, 0)) == -1) + return EOF; + + tmp = fdflags & O_ACCMODE; + if (tmp != O_RDWR && tmp != O_WRONLY) { + errno = EINVAL; + return EOF; + } + + if (fdflags & O_NONBLOCK) { + struct stat st; + if (fstat(fd, &st) == -1) + return -1; + if (!S_ISREG(st.st_mode)) { + errno = EFTYPE; + return EOF; + } + } + + _FILEEXT_SETUP(&f, &fext); + __sfpinit(&f); + f._p = buf; + f._w = sizeof(buf); + f._flags = __SWR; + f._file = fd; + f._bf._base = buf; + f._bf._size = sizeof(buf); + f._cookie = &f; + f._read = NULL; + f._write = __swrite; + f._seek = NULL; + f._close = NULL; + + if ((ret = vfprintf(&f, fmt, ap)) < 0) + return ret; + + return fflush(&f) ? EOF : ret; +}