forked from opnsense/src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dprintf() and vdprintf() from POSIX.1-2008. Like getline(),
dprintf() is a simple wrapper around another function, so we may as well implement it. But also like getline(), we can't prototype it by default right now because it would break too many ports.
- Loading branch information
David Schultz
authored and
David Schultz
committed
Mar 4, 2009
1 parent
3895337
commit ad760e6
Showing
7 changed files
with
218 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,8 +111,10 @@ FBSD_1.0 { | |
}; | ||
|
||
FBSD_1.1 { | ||
dprintf; | ||
getdelim; | ||
getline; | ||
vdprintf; | ||
}; | ||
|
||
FBSDprivate_1.0 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*- | ||
* Copyright (c) 2009 David Schultz <[email protected]> | ||
* All rights reserved. | ||
* | ||
* 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/cdefs.h> | ||
__FBSDID("$FreeBSD$"); | ||
|
||
#define _WITH_DPRINTF | ||
#include "namespace.h" | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include "un-namespace.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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,16 +32,17 @@ | |
.\" @(#)printf.3 8.1 (Berkeley) 6/4/93 | ||
.\" $FreeBSD$ | ||
.\" | ||
.Dd October 16, 2004 | ||
.Dd March 3, 2009 | ||
.Dt PRINTF 3 | ||
.Os | ||
.Sh NAME | ||
.Nm printf , fprintf , sprintf , snprintf , asprintf , | ||
.Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf | ||
.Nm printf , fprintf , sprintf , snprintf , asprintf , dprintf , | ||
.Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf, vdprintf | ||
.Nd formatted output conversion | ||
.Sh LIBRARY | ||
.Lb libc | ||
.Sh SYNOPSIS | ||
.Fd "#define _WITH_DPRINTF" | ||
.In stdio.h | ||
.Ft int | ||
.Fn printf "const char * restrict format" ... | ||
|
@@ -53,6 +54,8 @@ | |
.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ... | ||
.Ft int | ||
.Fn asprintf "char **ret" "const char *format" ... | ||
.Ft int | ||
.Fn dprintf "int" "const char * restrict format" ... | ||
.In stdarg.h | ||
.Ft int | ||
.Fn vprintf "const char * restrict format" "va_list ap" | ||
|
@@ -64,6 +67,8 @@ | |
.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict 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 | ||
|
@@ -83,6 +88,10 @@ and | |
.Fn vfprintf | ||
write output to the given output | ||
.Fa stream ; | ||
.Fn dprintf | ||
and | ||
.Fn vdprintf | ||
write output to the given file descriptor; | ||
.Fn sprintf , | ||
.Fn snprintf , | ||
.Fn vsprintf , | ||
|
@@ -771,6 +780,57 @@ for later interpolation by | |
Always use the proper secure idiom: | ||
.Pp | ||
.Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);" | ||
.Sh COMPATIBILITY | ||
Many application writers used the name | ||
.Va dprintf | ||
before the | ||
.Fn dprintf | ||
function was introduced in | ||
.St -p1003.1 , | ||
so a prototype is not provided by default in order to avoid | ||
compatibility problems. | ||
Applications that wish to use the | ||
.Fn dprintf | ||
function described herein should either request a strict | ||
.St -p1003.1-2008 | ||
environment by defining the macro | ||
.Dv _POSIX_C_SOURCE | ||
to the value 200809 or greater, or by defining the macro | ||
.Dv _WITH_DPRINTF , | ||
prior to the inclusion of | ||
.In stdio.h . | ||
For compatibility with GNU libc, defining either | ||
.Dv _BSD_SOURCE | ||
or | ||
.Dv _GNU_SOURCE | ||
prior to the inclusion of | ||
.In stdio.h | ||
will also make | ||
.Fn dprintf | ||
available. | ||
.Pp | ||
The conversion formats | ||
.Cm \&%D , \&%O , | ||
and | ||
.Cm %U | ||
are not standard and | ||
are provided only for backward compatibility. | ||
The effect of padding the | ||
.Cm %p | ||
format with zeros (either by the | ||
.Cm 0 | ||
flag or by specifying a precision), and the benign effect (i.e., none) | ||
of the | ||
.Cm # | ||
flag on | ||
.Cm %n | ||
and | ||
.Cm %p | ||
conversions, as well as other | ||
nonsensical combinations such as | ||
.Cm %Ld , | ||
are not standard; such combinations | ||
should be avoided. | ||
.Sh ERRORS | ||
In addition to the errors documented for the | ||
.Xr write 2 | ||
|
@@ -810,7 +870,13 @@ With the same reservation, the | |
and | ||
.Fn vsnprintf | ||
functions conform to | ||
.St -isoC-99 . | ||
.St -isoC-99 , | ||
while | ||
.Fn dprintf | ||
and | ||
.Fn vdprintf | ||
conform to | ||
.St -p1003.1-2008 . | ||
.Sh HISTORY | ||
The functions | ||
.Fn asprintf | ||
|
@@ -828,30 +894,13 @@ from | |
.An Todd C. Miller Aq [email protected] | ||
for | ||
.Ox 2.3 . | ||
.Sh BUGS | ||
The conversion formats | ||
.Cm \&%D , \&%O , | ||
and | ||
.Cm %U | ||
are not standard and | ||
are provided only for backward compatibility. | ||
The effect of padding the | ||
.Cm %p | ||
format with zeros (either by the | ||
.Cm 0 | ||
flag or by specifying a precision), and the benign effect (i.e., none) | ||
of the | ||
.Cm # | ||
flag on | ||
.Cm %n | ||
The | ||
.Fn dprintf | ||
and | ||
.Cm %p | ||
conversions, as well as other | ||
nonsensical combinations such as | ||
.Cm %Ld , | ||
are not standard; such combinations | ||
should be avoided. | ||
.Pp | ||
.Fn vdprintf | ||
functions were added in | ||
.Fx 8.0 . | ||
.Sh BUGS | ||
The | ||
.Nm | ||
family of functions do not correctly handle multibyte characters in the | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*- | ||
* Copyright (c) 2009 David Schultz <[email protected]> | ||
* All rights reserved. | ||
* | ||
* 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/cdefs.h> | ||
__FBSDID("$FreeBSD$"); | ||
|
||
#include "namespace.h" | ||
#include <errno.h> | ||
#include <limits.h> | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include "un-namespace.h" | ||
|
||
#include "local.h" | ||
|
||
int | ||
vdprintf(int fd, const char * __restrict fmt, va_list ap) | ||
{ | ||
FILE f; | ||
unsigned char buf[BUFSIZ]; | ||
int ret; | ||
|
||
if (fd > SHRT_MAX) { | ||
errno = EMFILE; | ||
return (EOF); | ||
} | ||
|
||
f._p = buf; | ||
f._w = sizeof(buf); | ||
f._flags = __SWR; | ||
f._file = fd; | ||
f._cookie = &f; | ||
f._write = __swrite; | ||
f._bf._base = buf; | ||
f._bf._size = sizeof(buf); | ||
f._orientation = 0; | ||
bzero(&f._mbstate, sizeof(f._mbstate)); | ||
|
||
if ((ret = __vfprintf(&f, fmt, ap)) < 0) | ||
return (ret); | ||
|
||
return (__fflush(&f) ? EOF : ret); | ||
} |