diff options
| author | Michael Kerrisk <mtk.manpages@gmail.com> | 2007-05-19 04:30:20 +0000 |
|---|---|---|
| committer | Michael Kerrisk <mtk.manpages@gmail.com> | 2007-05-19 04:30:20 +0000 |
| commit | 2b2581ee37f080c0a95d20aedecd5cb04e81ef4a (patch) | |
| tree | 4ce1a3bcc3cd8793e50f48abba8eab04c8e7ea07 /man3/printf.3 | |
| parent | 2dd578fd5b80cbfe5892b200633aa1442c4ac674 (diff) | |
| download | man-pages-2b2581ee37f080c0a95d20aedecd5cb04e81ef4a.tar.gz | |
Fix inconsistencies in order of .SH sections
Diffstat (limited to 'man3/printf.3')
| -rw-r--r-- | man3/printf.3 | 206 |
1 files changed, 103 insertions, 103 deletions
diff --git a/man3/printf.3 b/man3/printf.3 index 31b4c365d0..91762f9dc6 100644 --- a/man3/printf.3 +++ b/man3/printf.3 @@ -756,109 +756,6 @@ A `%' is written. No argument is converted. The complete conversion specification is `%%'. -.SH EXAMPLE -.br -.if \w'\*(Pi'=0 .ds Pi pi -To print \*(Pi to five decimal places: -.RS -.nf - -#include <math.h> -#include <stdio.h> -fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); -.fi -.RE -.PP -To print a date and time in the form `Sunday, July 3, 10:02', -where -.I weekday -and -.I month -are pointers to strings: -.RS -.nf - -#include <stdio.h> -fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", - weekday, month, day, hour, min); -.fi -.RE -.PP -Many countries use the day-month-year order. -Hence, an internationalized version must be able to print -the arguments in an order specified by the format: -.RS -.nf - -#include <stdio.h> -fprintf(stdout, format, - weekday, month, day, hour, min); - -.fi -.RE -where -.I format -depends on locale, and may permute the arguments. -With the value -.RS -.nf -"%1$s, %3$d. %2$s, %4$d:%5$.2d\en" -.fi -.RE -one might obtain `Sonntag, 3. Juli, 10:02'. -.PP -To allocate a sufficiently large string and print into it -(code correct for both glibc 2.0 and glibc 2.1): -.RS -.nf - -#include <stdio.h> -#include <stdlib.h> -#include <stdarg.h> - -char * -make_message(const char *fmt, ...) -{ - /* Guess we need no more than 100 bytes. */ - int n, size = 100; - char *p, *np; - va_list ap; - - if ((p = malloc(size)) == NULL) - return NULL; - - while (1) { - /* Try to print in the allocated space. */ - va_start(ap, fmt); - n = vsnprintf(p, size, fmt, ap); - va_end(ap); - /* If that worked, return the string. */ - if (n > \-1 && n < size) - return p; - /* Else try again with more space. */ - if (n > \-1) /* glibc 2.1 */ - size = n+1; /* precisely what is needed */ - else /* glibc 2.0 */ - size *= 2; /* twice the old size */ - if ((np = realloc (p, size)) == NULL) { - free(p); - return NULL; - } else { - p = np; - } - } -} -.fi -.RE -.SH NOTES -The glibc implementation of the functions -.BR snprintf () -and -.BR vsnprintf () -conforms to the C99 standard, i.e., behaves as described above, -since glibc version 2.1. -Until glibc 2.0.6 they would return \-1 -when the output was truncated. .SH "CONFORMING TO" The .BR fprintf (), @@ -912,6 +809,15 @@ glibc 2.1 adds length modifiers hh,j,t,z and conversion characters a,A. .PP glibc 2.2 adds the conversion character F with C99 semantics, and the flag character I. +.SH NOTES +The glibc implementation of the functions +.BR snprintf () +and +.BR vsnprintf () +conforms to the C99 standard, i.e., behaves as described above, +since glibc version 2.1. +Until glibc 2.0.6 they would return \-1 +when the output was truncated. .\" .SH HISTORY .\" Unix V7 defines the three routines .\" .BR printf (), @@ -989,6 +895,100 @@ call to write to memory and creating a security hole. .\" .PP .\" Some floating point conversions under early libc4 .\" caused memory leaks. +.SH EXAMPLE +.br +.if \w'\*(Pi'=0 .ds Pi pi +To print \*(Pi to five decimal places: +.RS +.nf + +#include <math.h> +#include <stdio.h> +fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); +.fi +.RE +.PP +To print a date and time in the form `Sunday, July 3, 10:02', +where +.I weekday +and +.I month +are pointers to strings: +.RS +.nf + +#include <stdio.h> +fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", + weekday, month, day, hour, min); +.fi +.RE +.PP +Many countries use the day-month-year order. +Hence, an internationalized version must be able to print +the arguments in an order specified by the format: +.RS +.nf + +#include <stdio.h> +fprintf(stdout, format, + weekday, month, day, hour, min); + +.fi +.RE +where +.I format +depends on locale, and may permute the arguments. +With the value +.RS +.nf +"%1$s, %3$d. %2$s, %4$d:%5$.2d\en" +.fi +.RE +one might obtain `Sonntag, 3. Juli, 10:02'. +.PP +To allocate a sufficiently large string and print into it +(code correct for both glibc 2.0 and glibc 2.1): +.RS +.nf + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> + +char * +make_message(const char *fmt, ...) +{ + /* Guess we need no more than 100 bytes. */ + int n, size = 100; + char *p, *np; + va_list ap; + + if ((p = malloc(size)) == NULL) + return NULL; + + while (1) { + /* Try to print in the allocated space. */ + va_start(ap, fmt); + n = vsnprintf(p, size, fmt, ap); + va_end(ap); + /* If that worked, return the string. */ + if (n > \-1 && n < size) + return p; + /* Else try again with more space. */ + if (n > \-1) /* glibc 2.1 */ + size = n+1; /* precisely what is needed */ + else /* glibc 2.0 */ + size *= 2; /* twice the old size */ + if ((np = realloc (p, size)) == NULL) { + free(p); + return NULL; + } else { + p = np; + } + } +} +.fi +.RE .SH "SEE ALSO" .BR printf (1), .BR asprintf (3), |
