Mon Sep 6 14:52:55 2010 UTC ()
add dprintf and vdprintf


(christos)
diff -r1.36 -r1.37 src/lib/libc/stdio/Makefile.inc
diff -r0 -r1.1 src/lib/libc/stdio/dprintf.c
diff -r0 -r1.1 src/lib/libc/stdio/vdprintf.c
diff -r1.24 -r1.25 src/lib/libc/stdio/findfp.c
diff -r1.24 -r1.25 src/lib/libc/stdio/local.h
diff -r1.52 -r1.53 src/lib/libc/stdio/printf.3

cvs diff -r1.36 -r1.37 src/lib/libc/stdio/Makefile.inc (expand / switch to unified diff)

--- src/lib/libc/stdio/Makefile.inc 2009/07/13 22:19:25 1.36
+++ src/lib/libc/stdio/Makefile.inc 2010/09/06 14:52:55 1.37
@@ -1,35 +1,35 @@ @@ -1,35 +1,35 @@
1# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91 1# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
2# $NetBSD: Makefile.inc,v 1.36 2009/07/13 22:19:25 roy Exp $ 2# $NetBSD: Makefile.inc,v 1.37 2010/09/06 14:52:55 christos Exp $
3 3
4# stdio sources 4# stdio sources
5.PATH: ${.CURDIR}/stdio 5.PATH: ${.CURDIR}/stdio
6 6
7CPPFLAGS+=-DWIDE_DOUBLE 7CPPFLAGS+=-DWIDE_DOUBLE
8 8
9SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \ 9SRCS+= asprintf.c clrerr.c dprintf.c fclose.c fdopen.c feof.c ferror.c \
10 fgetc.c fgetln.c fgetpos.c fgets.c fgetstr.c fgetwc.c fgetwln.c \ 10 fflush.c fgetc.c fgetln.c fgetpos.c fgets.c fgetstr.c fgetwc.c \
11 fgetws.c fileno.c findfp.c flags.c flockfile.c fopen.c fparseln.c \ 11 fgetwln.c fgetws.c fileno.c findfp.c flags.c flockfile.c fopen.c \
12 fprintf.c fpurge.c fputc.c fputs.c fputwc.c fputws.c fread.c \ 12 fparseln.c fprintf.c fpurge.c fputc.c fputs.c fputwc.c fputws.c \
13 freopen.c fscanf.c fseek.c fseeko.c fsetpos.c ftell.c ftello.c \ 13 fread.c freopen.c fscanf.c fseek.c fseeko.c fsetpos.c ftell.c ftello.c \
14 funopen.c fvwrite.c fwalk.c fwide.c fwprintf.c fwrite.c fwscanf.c \ 14 funopen.c fvwrite.c fwalk.c fwide.c fwprintf.c fwrite.c fwscanf.c \
15 getc.c getchar.c getdelim.c getline.c gettemp.c getw.c getwc.c \ 15 getc.c getchar.c getdelim.c getline.c gettemp.c getw.c getwc.c \
16 getwchar.c makebuf.c mkdtemp.c mkstemp.c perror.c printf.c putc.c \ 16 getwchar.c makebuf.c mkdtemp.c mkstemp.c perror.c printf.c putc.c \
17 putchar.c puts.c putw.c putwc.c putwchar.c refill.c remove.c rewind.c \ 17 putchar.c puts.c putw.c putwc.c putwchar.c refill.c remove.c rewind.c \
18 rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \ 18 rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \
19 sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \ 19 sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \
20 vasprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c vprintf.c \ 20 vasprintf.c vdprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \
21 vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c vswscanf.c \ 21 vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c \
22 vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c 22 vswscanf.c vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c
23 23
24.if !defined(AUDIT) 24.if !defined(AUDIT)
25SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c 25SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c
26.endif 26.endif
27 27
28# namespace purity wrappers 28# namespace purity wrappers
29SRCS+= _fileno.c _fseeko.c _ftello.c 29SRCS+= _fileno.c _fseeko.c _ftello.c
30 30
31MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fgetws.3 \ 31MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fgetws.3 \
32 flockfile.3 fopen.3 fparseln.3 fputs.3 fputws.3 fread.3 fseek.3 \ 32 flockfile.3 fopen.3 fparseln.3 fputs.3 fputws.3 fread.3 fseek.3 \
33 funopen.3 fwide.3 getc.3 getdelim.3 getwc.3 mktemp.3 printf.3 \ 33 funopen.3 fwide.3 getc.3 getdelim.3 getwc.3 mktemp.3 printf.3 \
34 putc.3 putwc.3 remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 \ 34 putc.3 putwc.3 remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 \
35 ungetc.3 ungetwc.3 wprintf.3 wscanf.3 35 ungetc.3 ungetwc.3 wprintf.3 wscanf.3
@@ -40,26 +40,27 @@ MLINKS+=flockfile.3 ftrylockfile.3 flock @@ -40,26 +40,27 @@ MLINKS+=flockfile.3 ftrylockfile.3 flock
40MLINKS+=fgets.3 gets.3 40MLINKS+=fgets.3 gets.3
41MLINKS+=fopen.3 fdopen.3 fopen.3 freopen.3 41MLINKS+=fopen.3 fdopen.3 fopen.3 freopen.3
42MLINKS+=fputs.3 puts.3 42MLINKS+=fputs.3 puts.3
43MLINKS+=fread.3 fwrite.3 43MLINKS+=fread.3 fwrite.3
44MLINKS+=fseek.3 fgetpos.3 fseek.3 fseeko.3 fseek.3 fsetpos.3 fseek.3 ftell.3 \ 44MLINKS+=fseek.3 fgetpos.3 fseek.3 fseeko.3 fseek.3 fsetpos.3 fseek.3 ftell.3 \
45 fseek.3 ftello.3 fseek.3 rewind.3 45 fseek.3 ftello.3 fseek.3 rewind.3
46MLINKS+=funopen.3 fropen.3 funopen.3 fwopen.3 46MLINKS+=funopen.3 fropen.3 funopen.3 fwopen.3
47MLINKS+=getc.3 fgetc.3 getc.3 getc_unlocked.3 getc.3 getchar.3 \ 47MLINKS+=getc.3 fgetc.3 getc.3 getc_unlocked.3 getc.3 getchar.3 \
48 getc.3 getchar_unlocked.3 getc.3 getw.3 48 getc.3 getchar_unlocked.3 getc.3 getw.3
49MLINKS+=getdelim.3 getline.3 49MLINKS+=getdelim.3 getline.3
50MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3 50MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3
51MLINKS+=printf.3 asprintf.3 printf.3 fprintf.3 printf.3 snprintf.3 \ 51MLINKS+=printf.3 asprintf.3 printf.3 fprintf.3 printf.3 snprintf.3 \
52 printf.3 sprintf.3 printf.3 vasprintf.3 printf.3 vfprintf.3 \ 52 printf.3 sprintf.3 printf.3 vasprintf.3 printf.3 vfprintf.3 \
53 printf.3 vprintf.3 printf.3 vsnprintf.3 printf.3 vsprintf.3  53 printf.3 vprintf.3 printf.3 vsnprintf.3 printf.3 vsprintf.3 \
 54 printf.3 dprintf.3 printf.3 vdprintf.3
54MLINKS+=putc.3 fputc.3 putc.3 putc_unlocked.3 putc.3 putchar.3 \ 55MLINKS+=putc.3 fputc.3 putc.3 putc_unlocked.3 putc.3 putchar.3 \
55 putc.3 putchar_unlocked.3 putc.3 putw.3 56 putc.3 putchar_unlocked.3 putc.3 putw.3
56MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 \ 57MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 \
57 scanf.3 vsscanf.3 58 scanf.3 vsscanf.3
58MLINKS+=setbuf.3 setbuffer.3 setbuf.3 setlinebuf.3 setbuf.3 setvbuf.3 59MLINKS+=setbuf.3 setbuffer.3 setbuf.3 setlinebuf.3 setbuf.3 setvbuf.3
59MLINKS+=tmpnam.3 tempnam.3 tmpnam.3 tmpfile.3 60MLINKS+=tmpnam.3 tempnam.3 tmpnam.3 tmpfile.3
60MLINKS+=putwc.3 fputwc.3 putwc.3 putwchar.3 61MLINKS+=putwc.3 fputwc.3 putwc.3 putwchar.3
61MLINKS+=getwc.3 fgetwc.3 getwc.3 getwchar.3 62MLINKS+=getwc.3 fgetwc.3 getwc.3 getwchar.3
62MLINKS+=wprintf.3 fwprintf.3 wprintf.3 swprintf.3 63MLINKS+=wprintf.3 fwprintf.3 wprintf.3 swprintf.3
63MLINKS+=wprintf.3 vwprintf.3 wprintf.3 vfwprintf.3 wprintf.3 vswprintf.3 64MLINKS+=wprintf.3 vwprintf.3 wprintf.3 vfwprintf.3 wprintf.3 vswprintf.3
64MLINKS+=wscanf.3 fwscanf.3 wscanf.3 swscanf.3 wscanf.3 vwscanf.3 65MLINKS+=wscanf.3 fwscanf.3 wscanf.3 swscanf.3 wscanf.3 vwscanf.3
65MLINKS+=wscanf.3 vswscanf.3 wscanf.3 vfwscanf.3 66MLINKS+=wscanf.3 vswscanf.3 wscanf.3 vfwscanf.3

File Added: src/lib/libc/stdio/dprintf.c
/*	$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;
}

File Added: src/lib/libc/stdio/vdprintf.c
/*	$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;
}

cvs diff -r1.24 -r1.25 src/lib/libc/stdio/findfp.c (expand / switch to unified diff)

--- src/lib/libc/stdio/findfp.c 2010/01/11 20:39:29 1.24
+++ src/lib/libc/stdio/findfp.c 2010/09/06 14:52:55 1.25
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: findfp.c,v 1.24 2010/01/11 20:39:29 joerg Exp $ */ 1/* $NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1990, 1993 4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek. 8 * Chris Torek.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36#if defined(LIBC_SCCS) && !defined(lint) 36#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 37#if 0
38static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94"; 38static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
39#else 39#else
40__RCSID("$NetBSD: findfp.c,v 1.24 2010/01/11 20:39:29 joerg Exp $"); 40__RCSID("$NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $");
41#endif 41#endif
42#endif /* LIBC_SCCS and not lint */ 42#endif /* LIBC_SCCS and not lint */
43 43
44#include "namespace.h" 44#include "namespace.h"
45#include <sys/param.h> 45#include <sys/param.h>
46#include <unistd.h> 46#include <unistd.h>
47#include <stdio.h> 47#include <stdio.h>
48#include <errno.h> 48#include <errno.h>
49#include <stdlib.h> 49#include <stdlib.h>
50#include <string.h> 50#include <string.h>
51#include "reentrant.h" 51#include "reentrant.h"
52#include "local.h" 52#include "local.h"
53#include "glue.h" 53#include "glue.h"
@@ -109,62 +109,68 @@ moreglue(n) @@ -109,62 +109,68 @@ moreglue(n)
109 g->next = NULL; 109 g->next = NULL;
110 g->niobs = n; 110 g->niobs = n;
111 g->iobs = p; 111 g->iobs = p;
112 pext = (void *)(p + n); 112 pext = (void *)(p + n);
113 while (--n >= 0) { 113 while (--n >= 0) {
114 *p = empty; 114 *p = empty;
115 _FILEEXT_SETUP(p, pext); 115 _FILEEXT_SETUP(p, pext);
116 p++; 116 p++;
117 pext++; 117 pext++;
118 } 118 }
119 return (g); 119 return (g);
120} 120}
121 121
 122void
 123__sfpinit(FILE *fp)
 124{
 125 fp->_flags = 1; /* reserve this slot; caller sets real flags */
 126 fp->_p = NULL; /* no current pointer */
 127 fp->_w = 0; /* nothing to read or write */
 128 fp->_r = 0;
 129 fp->_bf._base = NULL; /* no buffer */
 130 fp->_bf._size = 0;
 131 fp->_lbfsize = 0; /* not line buffered */
 132 fp->_file = -1; /* no file */
 133/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
 134 _UB(fp)._base = NULL; /* no ungetc buffer */
 135 _UB(fp)._size = 0;
 136 memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
 137}
 138
122/* 139/*
123 * Find a free FILE for fopen et al. 140 * Find a free FILE for fopen et al.
124 */ 141 */
125FILE * 142FILE *
126__sfp() 143__sfp()
127{ 144{
128 FILE *fp; 145 FILE *fp;
129 int n; 146 int n;
130 struct glue *g; 147 struct glue *g;
131 148
132 if (!__sdidinit) 149 if (!__sdidinit)
133 __sinit(); 150 __sinit();
134 151
135 rwlock_wrlock(&__sfp_lock); 152 rwlock_wrlock(&__sfp_lock);
136 for (g = &__sglue;; g = g->next) { 153 for (g = &__sglue;; g = g->next) {
137 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) 154 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
138 if (fp->_flags == 0) 155 if (fp->_flags == 0)
139 goto found; 156 goto found;
140 if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL) 157 if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
141 break; 158 break;
142 } 159 }
143 rwlock_unlock(&__sfp_lock); 160 rwlock_unlock(&__sfp_lock);
144 return (NULL); 161 return (NULL);
145found: 162found:
146 fp->_flags = 1; /* reserve this slot; caller sets real flags */ 163 __sfpinit(fp);
147 fp->_p = NULL; /* no current pointer */ 
148 fp->_w = 0; /* nothing to read or write */ 
149 fp->_r = 0; 
150 fp->_bf._base = NULL; /* no buffer */ 
151 fp->_bf._size = 0; 
152 fp->_lbfsize = 0; /* not line buffered */ 
153 fp->_file = -1; /* no file */ 
154/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ 
155 _UB(fp)._base = NULL; /* no ungetc buffer */ 
156 _UB(fp)._size = 0; 
157 memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data)); 
158 rwlock_unlock(&__sfp_lock); 164 rwlock_unlock(&__sfp_lock);
159 return (fp); 165 return (fp);
160} 166}
161 167
162/* 168/*
163 * XXX. Force immediate allocation of internal memory. Not used by stdio, 169 * XXX. Force immediate allocation of internal memory. Not used by stdio,
164 * but documented historically for certain applications. Bad applications. 170 * but documented historically for certain applications. Bad applications.
165 */ 171 */
166void 172void
167f_prealloc() 173f_prealloc()
168{ 174{
169 struct glue *g; 175 struct glue *g;
170 int n; 176 int n;

cvs diff -r1.24 -r1.25 src/lib/libc/stdio/local.h (expand / switch to unified diff)

--- src/lib/libc/stdio/local.h 2010/01/11 20:39:29 1.24
+++ src/lib/libc/stdio/local.h 2010/09/06 14:52:55 1.25
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: local.h,v 1.24 2010/01/11 20:39:29 joerg Exp $ */ 1/* $NetBSD: local.h,v 1.25 2010/09/06 14:52:55 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1990, 1993 4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek. 8 * Chris Torek.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -34,26 +34,27 @@ @@ -34,26 +34,27 @@
34 * @(#)local.h 8.3 (Berkeley) 7/3/94 34 * @(#)local.h 8.3 (Berkeley) 7/3/94
35 */ 35 */
36 36
37#include "wcio.h" 37#include "wcio.h"
38#include "fileext.h" 38#include "fileext.h"
39 39
40/* 40/*
41 * Information local to this implementation of stdio, 41 * Information local to this implementation of stdio,
42 * in particular, macros and private variables. 42 * in particular, macros and private variables.
43 */ 43 */
44 44
45extern int __sflush __P((FILE *)); 45extern int __sflush __P((FILE *));
46extern FILE *__sfp __P((void)); 46extern FILE *__sfp __P((void));
 47extern void __sfpinit __P((FILE *));
47extern int __srefill __P((FILE *)); 48extern int __srefill __P((FILE *));
48extern int __sread __P((void *, char *, int)); 49extern int __sread __P((void *, char *, int));
49extern int __swrite __P((void *, char const *, int)); 50extern int __swrite __P((void *, char const *, int));
50extern fpos_t __sseek __P((void *, fpos_t, int)); 51extern fpos_t __sseek __P((void *, fpos_t, int));
51extern int __sclose __P((void *)); 52extern int __sclose __P((void *));
52extern void __sinit __P((void)); 53extern void __sinit __P((void));
53extern void _cleanup __P((void)); 54extern void _cleanup __P((void));
54extern void (*__cleanup) __P((void)); 55extern void (*__cleanup) __P((void));
55extern void __smakebuf __P((FILE *)); 56extern void __smakebuf __P((FILE *));
56extern int __swhatbuf __P((FILE *, size_t *, int *)); 57extern int __swhatbuf __P((FILE *, size_t *, int *));
57extern int _fwalk __P((int (*)(FILE *))); 58extern int _fwalk __P((int (*)(FILE *)));
58extern char *_mktemp __P((char *)); 59extern char *_mktemp __P((char *));
59extern int __swsetup __P((FILE *)); 60extern int __swsetup __P((FILE *));

cvs diff -r1.52 -r1.53 src/lib/libc/stdio/printf.3 (expand / switch to unified diff)

--- src/lib/libc/stdio/printf.3 2010/05/13 21:52:43 1.52
+++ src/lib/libc/stdio/printf.3 2010/09/06 14:52:55 1.53
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1.\" $NetBSD: printf.3,v 1.52 2010/05/13 21:52:43 joerg Exp $ 1.\" $NetBSD: printf.3,v 1.53 2010/09/06 14:52:55 christos Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991, 1993 3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
5.\" 5.\"
6.\" This code is derived from software contributed to Berkeley by 6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3, 7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems. 8.\" on Information Processing Systems.
9.\" 9.\"
10.\" Redistribution and use in source and binary forms, with or without 10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions 11.\" modification, are permitted provided that the following conditions
12.\" are met: 12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright 13.\" 1. Redistributions of source code must retain the above copyright
14.\" notice, this list of conditions and the following disclaimer. 14.\" notice, this list of conditions and the following disclaimer.
@@ -23,85 +23,96 @@ @@ -23,85 +23,96 @@
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)printf.3 8.1 (Berkeley) 6/4/93 34.\" @(#)printf.3 8.1 (Berkeley) 6/4/93
35.\" 35.\"
36.Dd March 21, 2010 36.Dd August 28, 2010
37.Dt PRINTF 3 37.Dt PRINTF 3
38.Os 38.Os
39.Sh NAME 39.Sh NAME
40.Nm printf , 40.Nm printf ,
41.Nm fprintf , 41.Nm fprintf ,
 42.Nm dprintf
42.Nm sprintf , 43.Nm sprintf ,
43.Nm snprintf , 44.Nm snprintf ,
44.Nm asprintf , 45.Nm asprintf ,
45.Nm vprintf , 46.Nm vprintf ,
46.Nm vfprintf , 47.Nm vfprintf ,
47.Nm vsprintf , 48.Nm vsprintf ,
 49.Nm vdprintf ,
48.Nm vsnprintf , 50.Nm vsnprintf ,
49.Nm vasprintf 51.Nm vasprintf
50.Nd formatted output conversion 52.Nd formatted output conversion
51.Sh LIBRARY 53.Sh LIBRARY
52.Lb libc 54.Lb libc
53.Sh SYNOPSIS 55.Sh SYNOPSIS
54.In stdio.h 56.In stdio.h
55.Ft int 57.Ft int
56.Fn printf "const char * restrict format" ... 58.Fn printf "const char * restrict format" ...
57.Ft int 59.Ft int
58.Fn fprintf "FILE * restrict stream" "const char * restrict format" ... 60.Fn fprintf "FILE * restrict stream" "const char * restrict format" ...
59.Ft int 61.Ft int
 62.Fn dprintf "int fd" "const char * restrict format" ...
 63.Ft int
60.Fn sprintf "char * restrict str" "const char * restrict format" ... 64.Fn sprintf "char * restrict str" "const char * restrict format" ...
61.Ft int 65.Ft int
62.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ... 66.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ...
63.Ft int 67.Ft int
64.Fn asprintf "char ** restrict ret" "const char * restrict format" ... 68.Fn asprintf "char ** restrict ret" "const char * restrict format" ...
65.In stdarg.h 69.In stdarg.h
66.Ft int 70.Ft int
67.Fn vprintf "const char * restrict format" "va_list ap" 71.Fn vprintf "const char * restrict format" "va_list ap"
68.Ft int 72.Ft int
69.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap" 73.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap"
70.Ft int 74.Ft int
71.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap" 75.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap"
72.Ft int 76.Ft int
 77.Fn vdprintf "int fd" "const char * restrict format" "va_list ap"
 78.Ft int
73.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap" 79.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap"
74.Ft int 80.Ft int
75.Fn vasprintf "char ** restrict ret" "const char * restrict format" "va_list ap" 81.Fn vasprintf "char ** restrict ret" "const char * restrict format" "va_list ap"
76.Sh DESCRIPTION 82.Sh DESCRIPTION
77The 83The
78.Fn printf 84.Fn printf
79family of functions produces output according to a 85family of functions produces output according to a
80.Fa format 86.Fa format
81as described below. 87as described below.
82The 88The
83.Fn printf 89.Fn printf
84and 90and
85.Fn vprintf 91.Fn vprintf
86functions 92functions
87write output to 93write output to
88.Em stdout , 94.Em stdout ,
89the standard output stream; 95the standard output stream;
90.Fn fprintf 96.Fn fprintf
91and 97and
92.Fn vfprintf 98.Fn vfprintf
93write output to the given output 99write output to the given output
94.Fa stream ; 100.Fa stream ;
 101.Fn dprintf
 102and
 103.Fn vdprintf
 104write output to the give file descriptor
 105.Fa fd ;
95.Fn sprintf , 106.Fn sprintf ,
96.Fn snprintf , 107.Fn snprintf ,
97.Fn vsprintf , 108.Fn vsprintf ,
98and 109and
99.Fn vsnprintf 110.Fn vsnprintf
100write to the character string 111write to the character string
101.Fa str ; 112.Fa str ;
102and 113and
103.Fn asprintf 114.Fn asprintf
104and 115and
105.Fn vasprintf 116.Fn vasprintf
106write to a dynamically allocated string that is stored in 117write to a dynamically allocated string that is stored in
107.Fa ret . 118.Fa ret .
@@ -804,26 +815,32 @@ functions conform to @@ -804,26 +815,32 @@ functions conform to
804.St -isoC-99 . 815.St -isoC-99 .
805.Sh HISTORY 816.Sh HISTORY
806The functions 817The functions
807.Fn snprintf 818.Fn snprintf
808and 819and
809.Fn vsnprintf 820.Fn vsnprintf
810first appeared in 821first appeared in
811.Bx 4.4 . 822.Bx 4.4 .
812The functions 823The functions
813.Fn asprintf 824.Fn asprintf
814and 825and
815.Fn vasprintf 826.Fn vasprintf
816are modeled on the ones that first appeared in the GNU C library. 827are modeled on the ones that first appeared in the GNU C library.
 828The functions
 829.Fn dprintf
 830and
 831.Fn vdprintf
 832are parts of POSIX 200805 and appeared in
 833.Nx 6.0 .
817.Sh CAVEATS 834.Sh CAVEATS
818Because 835Because
819.Fn sprintf 836.Fn sprintf
820and 837and
821.Fn vsprintf 838.Fn vsprintf
822assume an infinitely long string, callers must be careful not to 839assume an infinitely long string, callers must be careful not to
823overflow the actual space; this is often impossible to assure. 840overflow the actual space; this is often impossible to assure.
824For safety, programmers should use the 841For safety, programmers should use the
825.Fn snprintf 842.Fn snprintf
826and 843and
827.Fn asprintf 844.Fn asprintf
828family of interfaces instead. 845family of interfaces instead.
829Unfortunately, the 846Unfortunately, the