Add support for dprintf(3) / vdprintf(3)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Add support for dprintf(3) / vdprintf(3)

Brad Smith-14
Here is a diff to add support for the POSIX functions dprintf(3)
and vdprintf(3).


Index: lib/libc/shlib_version
===================================================================
RCS file: /cvs/src/lib/libc/shlib_version,v
retrieving revision 1.146
diff -u -p -r1.146 shlib_version
--- lib/libc/shlib_version 3 Dec 2012 20:08:33 -0000 1.146
+++ lib/libc/shlib_version 23 Jan 2013 06:15:32 -0000
@@ -1,4 +1,4 @@
 major=66
-minor=1
+minor=2
 # note: If changes were made to include/thread_private.h or if system
 # calls were added/changed then librthread/shlib_version also be updated.
Index: include/stdio.h
===================================================================
RCS file: /cvs/src/include/stdio.h,v
retrieving revision 1.42
diff -u -p -r1.42 stdio.h
--- include/stdio.h 21 Mar 2012 23:44:35 -0000 1.42
+++ include/stdio.h 23 Jan 2013 06:15:32 -0000
@@ -212,6 +212,9 @@ __END_DECLS
  */
 __BEGIN_DECLS
 void clearerr(FILE *);
+#if __POSIX_VISIBLE >= 200809
+int dprintf(int, const char * __restrict, ...);
+#endif
 int fclose(FILE *);
 int feof(FILE *);
 int ferror(FILE *);
@@ -272,6 +275,9 @@ int ungetc(int, FILE *);
 int vfprintf(FILE *, const char *, __va_list);
 int vprintf(const char *, __va_list);
 int vsprintf(char *, const char *, __va_list);
+#if __POSIX_VISIBLE >= 200809
+int vdprintf(int, const char * __restrict, __va_list);
+#endif
 
 #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
 int snprintf(char *, size_t, const char *, ...)
Index: lib/libc/stdio/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/stdio/Makefile.inc,v
retrieving revision 1.22
diff -u -p -r1.22 Makefile.inc
--- lib/libc/stdio/Makefile.inc 21 Mar 2012 23:44:35 -0000 1.22
+++ lib/libc/stdio/Makefile.inc 23 Jan 2013 06:15:32 -0000
@@ -19,7 +19,7 @@ SRCS+= asprintf.c clrerr.c fclose.c fdop
  putwc.c putwchar.c ungetwc.c \
  fwprintf.c swprintf.c vfwprintf.c vswprintf.c vwprintf.c wprintf.c \
  fwscanf.c swscanf.c vfwscanf.c vswscanf.c vwscanf.c wscanf.c \
- getdelim.c getline.c
+ getdelim.c getline.c dprintf.c vdprintf.c
 
 MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fopen.3 fputs.3 \
  fread.3 fseek.3 funopen.3 getc.3 mktemp.3 perror.3 printf.3 putc.3 \
@@ -43,7 +43,8 @@ MLINKS+=mktemp.3 mkdtemp.3
 MLINKS+=mktemp.3 mkstemps.3
 MLINKS+=printf.3 fprintf.3 printf.3 snprintf.3 printf.3 sprintf.3 \
  printf.3 vfprintf.3 printf.3 vprintf.3 printf.3 vsnprintf.3 \
- printf.3 vsprintf.3 printf.3 asprintf.3 printf.3 vasprintf.3
+ printf.3 vsprintf.3 printf.3 asprintf.3 printf.3 vasprintf.3 \
+ printf.3 dprintf.3 printf.3 vdprintf.3
 MLINKS+=putc.3 fputc.3 putc.3 putchar.3 putc.3 putw.3
 MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 \
  scanf.3 vsscanf.3
Index: lib/libc/stdio/dprintf.c
===================================================================
RCS file: lib/libc/stdio/dprintf.c
diff -N lib/libc/stdio/dprintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/stdio/dprintf.c 23 Jan 2013 06:15:32 -0000
@@ -0,0 +1,48 @@
+/* $OpenBSD$ */
+/* $FreeBSD: src/lib/libc/stdio/dprintf.c,v 1.2 2012/11/17 01:49:39 svnexp Exp $ */
+
+/*-
+ * Copyright (c) 2009 David Schultz <[hidden email]>
+ * All rights reserved.
+ *
+ * Copyright (c) 2011 The FreeBSD Foundation
+ * All rights reserved.
+ * Portions of this software were developed by David Chisnall
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <stdio.h>
+#include <stdarg.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: lib/libc/stdio/printf.3
===================================================================
RCS file: /cvs/src/lib/libc/stdio/printf.3,v
retrieving revision 1.59
diff -u -p -r1.59 printf.3
--- lib/libc/stdio/printf.3 3 Sep 2011 22:59:07 -0000 1.59
+++ lib/libc/stdio/printf.3 23 Jan 2013 06:15:33 -0000
@@ -42,11 +42,13 @@
 .Nm sprintf ,
 .Nm snprintf ,
 .Nm asprintf ,
+.Nm dprintf ,
 .Nm vprintf ,
 .Nm vfprintf ,
 .Nm vsprintf ,
 .Nm vsnprintf ,
-.Nm vasprintf
+.Nm vasprintf ,
+.Nm vdprintf
 .Nd formatted output conversion
 .Sh SYNOPSIS
 .Fd #include <stdio.h>
@@ -60,6 +62,8 @@
 .Fn snprintf "char *str" "size_t size" "const char *format" ...
 .Ft int
 .Fn asprintf "char **ret" "const char *format" ...
+.Ft int
+.Fn dprintf "int fd" "const char * restrict format" ...
 .Fd #include <stdarg.h>
 .Ft int
 .Fn vprintf "const char *format" "va_list ap"
@@ -71,6 +75,8 @@
 .Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
 .Ft int
 .Fn vasprintf "char **ret" "const char *format" "va_list ap"
+.Ft int
+.Fn vdprintf "int fd" "const char * restrict format" "va_list ap"
 .Sh DESCRIPTION
 The
 .Fn printf
@@ -95,6 +101,10 @@ and
 .Fn vfprintf
 write output to the supplied stream pointer
 .Fa stream ;
+.Fn dprintf
+and
+.Fn vdprintf
+write output to the given file descriptor;
 .Fn sprintf ,
 .Fn snprintf ,
 .Fn vsprintf ,
@@ -754,6 +764,12 @@ and
 .Fn vsprintf
 functions conform to
 .St -ansiC .
+The
+.Fn dprintf
+and
+.Fn vdprintf
+functions conform to
+.St -p1003.1-2008 .
 .Sh HISTORY
 The functions
 .Fn snprintf
@@ -769,6 +785,13 @@ and
 first appeared in the GNU C library.
 This implementation first appeared in
 .Ox 2.3 .
+.Pp
+The functions
+.Fn dprintf
+and
+.Fn vdprintf
+first appeared in
+.Ox 5.3 .
 .Sh CAVEATS
 The conversion formats
 .Cm \&%D ,
Index: lib/libc/stdio/vdprintf.c
===================================================================
RCS file: lib/libc/stdio/vdprintf.c
diff -N lib/libc/stdio/vdprintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/stdio/vdprintf.c 23 Jan 2013 06:15:33 -0000
@@ -0,0 +1,84 @@
+/* $OpenBSD$ */
+/* $FreeBSD: src/lib/libc/stdio/vdprintf.c,v 1.4 2012/11/17 01:49:40 svnexp Exp $ */
+
+/*-
+ * Copyright (c) 2009 David Schultz <[hidden email]>
+ * All rights reserved.
+ *
+ * Copyright (c) 2011 The FreeBSD Foundation
+ * All rights reserved.
+ * Portions of this software were developed by David Chisnall
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "local.h"
+
+int
+vdprintf(int fd, const char * __restrict fmt, va_list ap)
+{
+ FILE f;
+ struct __sfileext fext;
+ unsigned char buf[BUFSIZ];
+ int ret, fdflags, tmp;
+
+ /* _file is only a short */
+ if (fd > SHRT_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;
+ }
+
+ _FILEEXT_SETUP(&f, &fext);
+
+ 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._write = __swrite;
+
+ if ((ret = __vfprintf(&f, fmt, ap)) < 0)
+ return ret;
+
+ return fflush(&f) ? EOF : ret;
+}

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Reply | Threaded
Open this post in threaded view
|

Re: Add support for dprintf(3) / vdprintf(3)

Philip Guenther-2
On Mon, 28 Jan 2013, Brad Smith wrote:
> Here is a diff to add support for the POSIX functions dprintf(3)
> and vdprintf(3).

Having poked at it a bit, I think the vdprintf.c below may be better.  By
using its own write callback, it avoids the SHRT_MAX limit on the fd, and
it removes the fcntl() dance to check the mode on the fd as it is
unnecessary: the write callback will diagnose that correctly (by failing
with EBADF) when the data is written.

Philip


-----------------------
/* $OpenBSD$ */
/* $FreeBSD: src/lib/libc/stdio/vdprintf.c,v 1.4 2012/11/17 01:49:40 svnexp Exp $ */

/*-
 * Copyright (c) 2009 David Schultz <[hidden email]>
 * All rights reserved.
 *
 * Copyright (c) 2011 The FreeBSD Foundation
 * All rights reserved.
 * Portions of this software were developed by David Chisnall
 * under sponsorship from the FreeBSD Foundation.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "local.h"

static int
__dwrite(void *cookie, const char *buf, int n)
{
        int *fdp = cookie;
        return (write(*fdp, buf, n));
}

int
vdprintf(int fd, const char * __restrict fmt, va_list ap)
{
        FILE f;
        struct __sfileext fext;
        unsigned char buf[BUFSIZ];
        int ret;

        _FILEEXT_SETUP(&f, &fext);

        f._p = buf;
        f._w = sizeof(buf);
        f._flags = __SWR;
        f._file = -1;
        f._bf._base = buf;
        f._bf._size = sizeof(buf);
        f._cookie = &fd;
        f._write = __dwrite;

        if ((ret = __vfprintf(&f, fmt, ap)) < 0)
                return ret;

        return fflush(&f) ? EOF : ret;
}

Reply | Threaded
Open this post in threaded view
|

Re: Add support for dprintf(3) / vdprintf(3)

Brad Smith-14
In reply to this post by Brad Smith-14
On Mon, Jan 28, 2013 at 07:15:17PM -0500, Brad Smith wrote:
> Here is a diff to add support for the POSIX functions dprintf(3)
> and vdprintf(3).

Here is an updated diff with the feedback from guenther@ and
also updating the stdio(3) man page.


Index: include/stdio.h
===================================================================
RCS file: /home/cvs/src/include/stdio.h,v
retrieving revision 1.42
diff -u -p -r1.42 stdio.h
--- include/stdio.h 21 Mar 2012 23:44:35 -0000 1.42
+++ include/stdio.h 24 Jan 2013 21:36:36 -0000
@@ -212,6 +212,9 @@ __END_DECLS
  */
 __BEGIN_DECLS
 void clearerr(FILE *);
+#if __POSIX_VISIBLE >= 200809
+int dprintf(int, const char * __restrict, ...);
+#endif
 int fclose(FILE *);
 int feof(FILE *);
 int ferror(FILE *);
@@ -272,6 +275,9 @@ int ungetc(int, FILE *);
 int vfprintf(FILE *, const char *, __va_list);
 int vprintf(const char *, __va_list);
 int vsprintf(char *, const char *, __va_list);
+#if __POSIX_VISIBLE >= 200809
+int vdprintf(int, const char * __restrict, __va_list);
+#endif
 
 #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
 int snprintf(char *, size_t, const char *, ...)
Index: lib/libc/shlib_version
===================================================================
RCS file: /home/cvs/src/lib/libc/shlib_version,v
retrieving revision 1.146
diff -u -p -r1.146 shlib_version
--- lib/libc/shlib_version 3 Dec 2012 20:08:33 -0000 1.146
+++ lib/libc/shlib_version 24 Jan 2013 21:36:35 -0000
@@ -1,4 +1,4 @@
 major=66
-minor=1
+minor=2
 # note: If changes were made to include/thread_private.h or if system
 # calls were added/changed then librthread/shlib_version also be updated.
Index: lib/libc/stdio/Makefile.inc
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/Makefile.inc,v
retrieving revision 1.22
diff -u -p -r1.22 Makefile.inc
--- lib/libc/stdio/Makefile.inc 21 Mar 2012 23:44:35 -0000 1.22
+++ lib/libc/stdio/Makefile.inc 24 Jan 2013 21:36:36 -0000
@@ -19,7 +19,7 @@ SRCS+= asprintf.c clrerr.c fclose.c fdop
  putwc.c putwchar.c ungetwc.c \
  fwprintf.c swprintf.c vfwprintf.c vswprintf.c vwprintf.c wprintf.c \
  fwscanf.c swscanf.c vfwscanf.c vswscanf.c vwscanf.c wscanf.c \
- getdelim.c getline.c
+ getdelim.c getline.c dprintf.c vdprintf.c
 
 MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fopen.3 fputs.3 \
  fread.3 fseek.3 funopen.3 getc.3 mktemp.3 perror.3 printf.3 putc.3 \
@@ -43,7 +43,8 @@ MLINKS+=mktemp.3 mkdtemp.3
 MLINKS+=mktemp.3 mkstemps.3
 MLINKS+=printf.3 fprintf.3 printf.3 snprintf.3 printf.3 sprintf.3 \
  printf.3 vfprintf.3 printf.3 vprintf.3 printf.3 vsnprintf.3 \
- printf.3 vsprintf.3 printf.3 asprintf.3 printf.3 vasprintf.3
+ printf.3 vsprintf.3 printf.3 asprintf.3 printf.3 vasprintf.3 \
+ printf.3 dprintf.3 printf.3 vdprintf.3
 MLINKS+=putc.3 fputc.3 putc.3 putchar.3 putc.3 putw.3
 MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 \
  scanf.3 vsscanf.3
Index: lib/libc/stdio/dprintf.c
===================================================================
RCS file: lib/libc/stdio/dprintf.c
diff -N lib/libc/stdio/dprintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/stdio/dprintf.c 24 Jan 2013 21:36:36 -0000
@@ -0,0 +1,48 @@
+/* $OpenBSD$ */
+/* $FreeBSD: src/lib/libc/stdio/dprintf.c,v 1.2 2012/11/17 01:49:39 svnexp Exp $ */
+
+/*-
+ * Copyright (c) 2009 David Schultz <[hidden email]>
+ * All rights reserved.
+ *
+ * Copyright (c) 2011 The FreeBSD Foundation
+ * All rights reserved.
+ * Portions of this software were developed by David Chisnall
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <stdio.h>
+#include <stdarg.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: lib/libc/stdio/printf.3
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/printf.3,v
retrieving revision 1.59
diff -u -p -r1.59 printf.3
--- lib/libc/stdio/printf.3 3 Sep 2011 22:59:07 -0000 1.59
+++ lib/libc/stdio/printf.3 24 Jan 2013 21:36:36 -0000
@@ -42,11 +42,13 @@
 .Nm sprintf ,
 .Nm snprintf ,
 .Nm asprintf ,
+.Nm dprintf ,
 .Nm vprintf ,
 .Nm vfprintf ,
 .Nm vsprintf ,
 .Nm vsnprintf ,
-.Nm vasprintf
+.Nm vasprintf ,
+.Nm vdprintf
 .Nd formatted output conversion
 .Sh SYNOPSIS
 .Fd #include <stdio.h>
@@ -60,6 +62,8 @@
 .Fn snprintf "char *str" "size_t size" "const char *format" ...
 .Ft int
 .Fn asprintf "char **ret" "const char *format" ...
+.Ft int
+.Fn dprintf "int fd" "const char * restrict format" ...
 .Fd #include <stdarg.h>
 .Ft int
 .Fn vprintf "const char *format" "va_list ap"
@@ -71,6 +75,8 @@
 .Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
 .Ft int
 .Fn vasprintf "char **ret" "const char *format" "va_list ap"
+.Ft int
+.Fn vdprintf "int fd" "const char * restrict format" "va_list ap"
 .Sh DESCRIPTION
 The
 .Fn printf
@@ -95,6 +101,10 @@ and
 .Fn vfprintf
 write output to the supplied stream pointer
 .Fa stream ;
+.Fn dprintf
+and
+.Fn vdprintf
+write output to the given file descriptor;
 .Fn sprintf ,
 .Fn snprintf ,
 .Fn vsprintf ,
@@ -754,6 +764,12 @@ and
 .Fn vsprintf
 functions conform to
 .St -ansiC .
+The
+.Fn dprintf
+and
+.Fn vdprintf
+functions conform to
+.St -p1003.1-2008 .
 .Sh HISTORY
 The functions
 .Fn snprintf
@@ -769,6 +785,13 @@ and
 first appeared in the GNU C library.
 This implementation first appeared in
 .Ox 2.3 .
+.Pp
+The functions
+.Fn dprintf
+and
+.Fn vdprintf
+first appeared in
+.Ox 5.3 .
 .Sh CAVEATS
 The conversion formats
 .Cm \&%D ,
Index: lib/libc/stdio/stdio.3
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/stdio.3,v
retrieving revision 1.23
diff -u -p -r1.23 stdio.3
--- lib/libc/stdio/stdio.3 12 Apr 2012 08:26:38 -0000 1.23
+++ lib/libc/stdio/stdio.3 29 Jan 2013 06:14:43 -0000
@@ -210,6 +210,7 @@ definitions are explicitly removed.
 .It Sy Function Ta Sy Description
 .It asprintf Ta "formatted output conversion with allocation"
 .It clearerr Ta "check and reset stream status"
+.It dprintf Ta "formatted output conversion"
 .It fclose Ta "close a stream"
 .It fdopen Ta "stream open functions"
 .It feof Ta "check and reset stream status"
@@ -267,6 +268,7 @@ definitions are explicitly removed.
 .It tmpnam Ta "temporary file routines"
 .It ungetc Ta "un-get character from input stream"
 .It vasprintf Ta "formatted output conversion with allocation"
+.It vdprintf Ta "formatte output conversion"
 .It vfprintf Ta "formatted output conversion"
 .It vfscanf Ta "input format conversion"
 .It vprintf Ta "formatted output conversion"
Index: lib/libc/stdio/vdprintf.c
===================================================================
RCS file: lib/libc/stdio/vdprintf.c
diff -N lib/libc/stdio/vdprintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/stdio/vdprintf.c 29 Jan 2013 08:10:26 -0000
@@ -0,0 +1,75 @@
+/* $OpenBSD$ */
+/* $FreeBSD: src/lib/libc/stdio/vdprintf.c,v 1.4 2012/11/17 01:49:40 svnexp Exp $ */
+
+/*-
+ * Copyright (c) 2009 David Schultz <[hidden email]>
+ * All rights reserved.
+ *
+ * Copyright (c) 2011 The FreeBSD Foundation
+ * All rights reserved.
+ * Portions of this software were developed by David Chisnall
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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/stat.h>
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "local.h"
+
+static int
+__dwrite(void *cookie, const char *buf, int n)
+{
+ int *fdp = cookie;
+ return (write(*fdp, buf, n));
+}
+
+int
+vdprintf(int fd, const char * __restrict fmt, va_list ap)
+{
+ FILE f;
+ struct __sfileext fext;
+ unsigned char buf[BUFSIZ];
+ int ret, fdflags, tmp;
+
+ _FILEEXT_SETUP(&f, &fext);
+
+ f._p = buf;
+ f._w = sizeof(buf);
+ f._flags = __SWR;
+ f._file = -1;
+ f._bf._base = buf;
+ f._bf._size = sizeof(buf);
+ f._cookie = &fd;
+ f._write = __dwrite;
+
+ if ((ret = __vfprintf(&f, fmt, ap)) < 0)
+ return ret;
+
+ return fflush(&f) ? EOF : ret;
+}

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Reply | Threaded
Open this post in threaded view
|

Re: Add support for dprintf(3) / vdprintf(3)

Brad Smith-14
On Tue, Jan 29, 2013 at 03:29:30AM -0500, Brad Smith wrote:
> On Mon, Jan 28, 2013 at 07:15:17PM -0500, Brad Smith wrote:
> > Here is a diff to add support for the POSIX functions dprintf(3)
> > and vdprintf(3).
>
> Here is an updated diff with the feedback from guenther@ and
> also updating the stdio(3) man page.

Fixed a typo in the stdio(3) man page.


Index: include/stdio.h
===================================================================
RCS file: /home/cvs/src/include/stdio.h,v
retrieving revision 1.42
diff -u -p -r1.42 stdio.h
--- include/stdio.h 21 Mar 2012 23:44:35 -0000 1.42
+++ include/stdio.h 24 Jan 2013 21:36:36 -0000
@@ -212,6 +212,9 @@ __END_DECLS
  */
 __BEGIN_DECLS
 void clearerr(FILE *);
+#if __POSIX_VISIBLE >= 200809
+int dprintf(int, const char * __restrict, ...);
+#endif
 int fclose(FILE *);
 int feof(FILE *);
 int ferror(FILE *);
@@ -272,6 +275,9 @@ int ungetc(int, FILE *);
 int vfprintf(FILE *, const char *, __va_list);
 int vprintf(const char *, __va_list);
 int vsprintf(char *, const char *, __va_list);
+#if __POSIX_VISIBLE >= 200809
+int vdprintf(int, const char * __restrict, __va_list);
+#endif
 
 #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
 int snprintf(char *, size_t, const char *, ...)
Index: lib/libc/shlib_version
===================================================================
RCS file: /home/cvs/src/lib/libc/shlib_version,v
retrieving revision 1.146
diff -u -p -r1.146 shlib_version
--- lib/libc/shlib_version 3 Dec 2012 20:08:33 -0000 1.146
+++ lib/libc/shlib_version 24 Jan 2013 21:36:35 -0000
@@ -1,4 +1,4 @@
 major=66
-minor=1
+minor=2
 # note: If changes were made to include/thread_private.h or if system
 # calls were added/changed then librthread/shlib_version also be updated.
Index: lib/libc/stdio/Makefile.inc
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/Makefile.inc,v
retrieving revision 1.22
diff -u -p -r1.22 Makefile.inc
--- lib/libc/stdio/Makefile.inc 21 Mar 2012 23:44:35 -0000 1.22
+++ lib/libc/stdio/Makefile.inc 24 Jan 2013 21:36:36 -0000
@@ -19,7 +19,7 @@ SRCS+= asprintf.c clrerr.c fclose.c fdop
  putwc.c putwchar.c ungetwc.c \
  fwprintf.c swprintf.c vfwprintf.c vswprintf.c vwprintf.c wprintf.c \
  fwscanf.c swscanf.c vfwscanf.c vswscanf.c vwscanf.c wscanf.c \
- getdelim.c getline.c
+ getdelim.c getline.c dprintf.c vdprintf.c
 
 MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fopen.3 fputs.3 \
  fread.3 fseek.3 funopen.3 getc.3 mktemp.3 perror.3 printf.3 putc.3 \
@@ -43,7 +43,8 @@ MLINKS+=mktemp.3 mkdtemp.3
 MLINKS+=mktemp.3 mkstemps.3
 MLINKS+=printf.3 fprintf.3 printf.3 snprintf.3 printf.3 sprintf.3 \
  printf.3 vfprintf.3 printf.3 vprintf.3 printf.3 vsnprintf.3 \
- printf.3 vsprintf.3 printf.3 asprintf.3 printf.3 vasprintf.3
+ printf.3 vsprintf.3 printf.3 asprintf.3 printf.3 vasprintf.3 \
+ printf.3 dprintf.3 printf.3 vdprintf.3
 MLINKS+=putc.3 fputc.3 putc.3 putchar.3 putc.3 putw.3
 MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 \
  scanf.3 vsscanf.3
Index: lib/libc/stdio/dprintf.c
===================================================================
RCS file: lib/libc/stdio/dprintf.c
diff -N lib/libc/stdio/dprintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/stdio/dprintf.c 24 Jan 2013 21:36:36 -0000
@@ -0,0 +1,48 @@
+/* $OpenBSD$ */
+/* $FreeBSD: src/lib/libc/stdio/dprintf.c,v 1.2 2012/11/17 01:49:39 svnexp Exp $ */
+
+/*-
+ * Copyright (c) 2009 David Schultz <[hidden email]>
+ * All rights reserved.
+ *
+ * Copyright (c) 2011 The FreeBSD Foundation
+ * All rights reserved.
+ * Portions of this software were developed by David Chisnall
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <stdio.h>
+#include <stdarg.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: lib/libc/stdio/printf.3
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/printf.3,v
retrieving revision 1.59
diff -u -p -r1.59 printf.3
--- lib/libc/stdio/printf.3 3 Sep 2011 22:59:07 -0000 1.59
+++ lib/libc/stdio/printf.3 24 Jan 2013 21:36:36 -0000
@@ -42,11 +42,13 @@
 .Nm sprintf ,
 .Nm snprintf ,
 .Nm asprintf ,
+.Nm dprintf ,
 .Nm vprintf ,
 .Nm vfprintf ,
 .Nm vsprintf ,
 .Nm vsnprintf ,
-.Nm vasprintf
+.Nm vasprintf ,
+.Nm vdprintf
 .Nd formatted output conversion
 .Sh SYNOPSIS
 .Fd #include <stdio.h>
@@ -60,6 +62,8 @@
 .Fn snprintf "char *str" "size_t size" "const char *format" ...
 .Ft int
 .Fn asprintf "char **ret" "const char *format" ...
+.Ft int
+.Fn dprintf "int fd" "const char * restrict format" ...
 .Fd #include <stdarg.h>
 .Ft int
 .Fn vprintf "const char *format" "va_list ap"
@@ -71,6 +75,8 @@
 .Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
 .Ft int
 .Fn vasprintf "char **ret" "const char *format" "va_list ap"
+.Ft int
+.Fn vdprintf "int fd" "const char * restrict format" "va_list ap"
 .Sh DESCRIPTION
 The
 .Fn printf
@@ -95,6 +101,10 @@ and
 .Fn vfprintf
 write output to the supplied stream pointer
 .Fa stream ;
+.Fn dprintf
+and
+.Fn vdprintf
+write output to the given file descriptor;
 .Fn sprintf ,
 .Fn snprintf ,
 .Fn vsprintf ,
@@ -754,6 +764,12 @@ and
 .Fn vsprintf
 functions conform to
 .St -ansiC .
+The
+.Fn dprintf
+and
+.Fn vdprintf
+functions conform to
+.St -p1003.1-2008 .
 .Sh HISTORY
 The functions
 .Fn snprintf
@@ -769,6 +785,13 @@ and
 first appeared in the GNU C library.
 This implementation first appeared in
 .Ox 2.3 .
+.Pp
+The functions
+.Fn dprintf
+and
+.Fn vdprintf
+first appeared in
+.Ox 5.3 .
 .Sh CAVEATS
 The conversion formats
 .Cm \&%D ,
Index: lib/libc/stdio/stdio.3
===================================================================
RCS file: /home/cvs/src/lib/libc/stdio/stdio.3,v
retrieving revision 1.23
diff -u -p -r1.23 stdio.3
--- lib/libc/stdio/stdio.3 12 Apr 2012 08:26:38 -0000 1.23
+++ lib/libc/stdio/stdio.3 29 Jan 2013 08:32:56 -0000
@@ -210,6 +210,7 @@ definitions are explicitly removed.
 .It Sy Function Ta Sy Description
 .It asprintf Ta "formatted output conversion with allocation"
 .It clearerr Ta "check and reset stream status"
+.It dprintf Ta "formatted output conversion"
 .It fclose Ta "close a stream"
 .It fdopen Ta "stream open functions"
 .It feof Ta "check and reset stream status"
@@ -267,6 +268,7 @@ definitions are explicitly removed.
 .It tmpnam Ta "temporary file routines"
 .It ungetc Ta "un-get character from input stream"
 .It vasprintf Ta "formatted output conversion with allocation"
+.It vdprintf Ta "formatted output conversion"
 .It vfprintf Ta "formatted output conversion"
 .It vfscanf Ta "input format conversion"
 .It vprintf Ta "formatted output conversion"
Index: lib/libc/stdio/vdprintf.c
===================================================================
RCS file: lib/libc/stdio/vdprintf.c
diff -N lib/libc/stdio/vdprintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/stdio/vdprintf.c 29 Jan 2013 08:10:26 -0000
@@ -0,0 +1,75 @@
+/* $OpenBSD$ */
+/* $FreeBSD: src/lib/libc/stdio/vdprintf.c,v 1.4 2012/11/17 01:49:40 svnexp Exp $ */
+
+/*-
+ * Copyright (c) 2009 David Schultz <[hidden email]>
+ * All rights reserved.
+ *
+ * Copyright (c) 2011 The FreeBSD Foundation
+ * All rights reserved.
+ * Portions of this software were developed by David Chisnall
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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/stat.h>
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "local.h"
+
+static int
+__dwrite(void *cookie, const char *buf, int n)
+{
+ int *fdp = cookie;
+ return (write(*fdp, buf, n));
+}
+
+int
+vdprintf(int fd, const char * __restrict fmt, va_list ap)
+{
+ FILE f;
+ struct __sfileext fext;
+ unsigned char buf[BUFSIZ];
+ int ret, fdflags, tmp;
+
+ _FILEEXT_SETUP(&f, &fext);
+
+ f._p = buf;
+ f._w = sizeof(buf);
+ f._flags = __SWR;
+ f._file = -1;
+ f._bf._base = buf;
+ f._bf._size = sizeof(buf);
+ f._cookie = &fd;
+ f._write = __dwrite;
+
+ if ((ret = __vfprintf(&f, fmt, ap)) < 0)
+ return ret;
+
+ return fflush(&f) ? EOF : ret;
+}

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.