1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\" (c) 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" Added _GNU_SOURCE, aeb, Sat Jul 5 23:10:04 MET 1997
26 .\" Japanese Version Copyright (c) 1998 Akihiro Motoki all rights reserved.
27 .\" Translated Mon May 25 1998 by Akihiro Motoki <motoki@hal.t.u-tokyo.ac.jp>
28 .\" Modified Thu Nov 11 1999 by Akihiro Motoki <motoki@ptl.abk.nec.co.jp>
30 .TH SNPRINTF 3 "16 September 1995" "GNU" "Linux Programmer's Manual"
32 .\"O .snprintf, vsnprintf \- formatted output conversion
34 snprintf, vsnprintf \- »ØÄꤷ¤¿½ñ¼°¤Ç½ÐÎϤò¹Ô¤¦
37 .B #define _GNU_SOURCE
41 .BI "int snprintf ( char *" str ", size_t " n ", "
43 .BI " const char *" format ", ... );"
45 .B #include <stdarg.h>
47 .BI "int vsnprintf ( char *" str ", size_t " n ", "
49 .BI " const char *" format ", va_list " ap " );"
51 .\"O \fBsnprintf\fP writes output to the string \fIstr\fP, under control of
52 .\"O the \fIformat\fP string that specifies how subsequent arguments are
53 .\"O converted for output. It is similar to \fBsprintf\fP(3), except that
54 .\"O \fIn\fP specifies the maximum number of characters to produce. The
55 .\"O trailing null character is counted towards this limit, so you should
56 .\"O allocate at least \fIn\fP characters for the string \fIstr\fP.
58 \fBsnprintf\fP ´Ø¿ô¤Ï¡¢\fIformat\fP ¤Ç»ØÄꤵ¤ì¤¿½ñ¼°¤Ë½¾¤¤¡¢¤½¤ì°Ê¹ß¤Ë
59 Í¿¤¨¤é¤ì¤¿°ú¿ô¤òÀ°·Á¤·¤Æ½ÐÎϤ·¡¢½ÐÎϤÏʸ»úÎó \fIstr\fP ¤Ë½ñ¤¹þ¤Þ¤ì¤ë¡£
60 ¤³¤Î´Ø¿ô¤Ï \fBsprintf\fP(3) ¤Ë»÷¤Æ¤¤¤ë¤¬¡¢ ½ÐÎϤµ¤ì¤ë
61 ºÇÂçʸ»ú¿ô \fIn\fP ¤ò»ØÄê¤Ç¤¤ëÅÀ¤¬°Û¤Ê¤ë¡£
62 ʸ»úÎó¤Î½ªÃ¼¤ò¼¨¤¹¥Ì¥ëʸ»ú¤âºÇÂçʸ»ú¿ô¤Ë´Þ¤Þ¤ì¤ë¤¿¤á¡¢
63 ½ÐÎÏʸ»úÎó \fIstr\fP ¤Ë¤Ï¾¯¤Ê¤¯¤È¤â \fIn\fP ʸ»ú³ä¤êÅö¤Æ¤ëɬÍפ¬¤¢¤ë¡£
65 .\"O \fBvsnprintf\fP is the equivalent of \fBsnprintf\fP with the variable
66 .\"O argument list specified directly as for \fBvprintf\fP.
67 \fBvsnprintf\fP ´Ø¿ô¤Ï²ÄÊѤΰú¿ô¥ê¥¹¥È¤ò¤â¤Ã¤¿ \fBsnprintf\fP ¤ÈÅù²Á¤Ç¤¢¤ë¡£
68 °ú¿ô¥ê¥¹¥È¤Ë¤Ä¤¤¤Æ¤Ï \fBvprintf\fP ¤Î¹à¤ÇÀâÌÀ¤µ¤ì¤Æ¤¤¤ë¡£
69 .\"O .SH "RETURN VALUE"
70 .\"O If the output was truncated, the return value is -1, otherwise it is
71 .\"O the number of characters stored, not including the terminating null.
72 .\"O (Thus until glibc 2.0.6. Since glibc 2.1 these functions
73 .\"O return the number of characters (excluding the trailing null)
74 .\"O which would have been written to the final string if enough space
75 .\"O had been available.)
77 ½ÐÎϤ¬Ä¹¤¯ÅÓÃæ¤Ç¥«¥Ã¥È¤µ¤ì¤¿¾ì¹ç¤Ë¤Ï -1 ¤òÊÖ¤·¡¢¤½¤ì°Ê³°¤Ï \fIstr\fP ¤ËÊݸ
78 ¤µ¤ì¤¿Ê¸»ú¿ô(ʸ»úÎó¤Î½ªÃ¼¤ò¼¨¤¹'\e0'¤ò½ü¤¯)¤òÊÖ¤¹¡£
79 (⤷ glibc 2.0.6 ¤Þ¤Ç¤Î»ÅÍͤǤ¢¤ë¡£
80 glibc 2.1 °Ê¹ß¤Ç¤Ï¡¢¤³¤ì¤é¤Î´Ø¿ô¤Ï¡¢¤â¤·ÍøÍѲÄǽ¤Ê½½Ê¬¤Ê¥¹¥Ú¡¼¥¹¤¬¤¢¤ì¤Ð
81 ½ñ¤¹þ¤Þ¤ì¤¿¤Ç¤¢¤í¤¦Ê¸»ú¿ô(ʸ»úÎó¤Î½ªÃ¼¤ò¼¨¤¹'\e0'¤ò½ü¤¯)¤òÊÖ¤¹¡£)
85 .\"O Here is an example program which dynamically enlarges its output buffer,
86 .\"O and works correctly under both glibc 2.0 and glibc 2.1.
87 ½ÐÎϥХåե¡¤òưŪ¤Ë³ÈÄ¥¤¹¤ë¥×¥í¥°¥é¥àÎã¤ò°Ê²¼¤Ë¼¨¤¹¡£
88 ¤³¤Î¥×¥í¥°¥é¥à¤Ï¡¢glibc 2.0 ¤È glibc 2.1 ¤Î¤É¤Á¤é¤Ç¤âÀµ¤·¤¯Æ°ºî¤¹¤ë¡£
92 .\"O /* Construct a message describing the value of a
93 .\"O variable whose name is NAME and whose value is
95 /* ÊÑ¿ô̾¤È¤½¤ÎÃͤòµ½Ò¤¹¤ë "value of ÊÑ¿ô̾ is ÃÍ"
96 ¤È¤¤¤¦¥á¥Ã¥»¡¼¥¸¤òºîÀ®¤¹¤ë´Ø¿ô */
98 make_message (char *name, char *value)
100 .\"O /* Guess we need no more than 100 chars of space. */
101 /* ½ÐÎϥХåե¡¤Ï100ʸ»úʬ¤Ç½½Ê¬¤Ç¤¢¤ë¤È²¾Äê */
103 char *buffer = (char *) xmalloc (size);
106 .\"O /* Try to print in the allocated space. */
107 /* ³ÎÊݺѤߤΥХåե¡¤Ë½ÐÎϤ·¤è¤¦¤È¤¹¤ë */
108 int nchars = snprintf (buffer, size,
109 "value of %s is %s", name, value);
110 .\"O /* If that worked, return the string. */
111 /* À®¸ù¤¹¤ì¤Ð¡¢¥Ð¥Ã¥Õ¥¡Æâ¤Îʸ»úÎó¤òÊÖ¤¹ */
112 if (nchars > -1 && nchars < size)
114 .\"O /* Else try again with more space. */
115 .\"O if (nchars > -1)
116 .\"O size = nchars+1; /* precisely what is needed */
118 .\"O size *= 2; /* twice the old size */
119 /* ¼ºÇÔ¤¹¤ì¤Ð¡¢¤µ¤é¤Ë¥Ð¥Ã¥Õ¥¡¤ò³ÎÊݤ·¤ÆºÆ»î¹Ô */
121 size = nchars+1; /* ɬÍפʥХåե¡Ä¹¤òÀµ³Î¤Ë³ÎÊÝ */
123 size *= 2; /* ¥Ð¥Ã¥Õ¥¡Ä¹¤ò£²Çܤˤ¹¤ë */
124 buffer = (char *) xrealloc (buffer, size);
129 .\"O .SH "CONFORMING TO"
130 .\"O These are GNU extensions, expected to become part of ISO C9x.
132 ¤³¤ì¤é¤Î´Ø¿ô¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë¡£ISO C9x ¤Î°ìÉô¤È¤Ê¤ëͽÄê¤Ç¤¢¤ë¡£
135 .BR printf "(3), " sprintf "(3), " vsprintf "(3), " stdarg (3)