OSDN Git Service

(split) Convert contrib and obsolete pages to UTF-8.
[linuxjm/LDP_man-pages.git] / obsolete / man3 / snprintf.3
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\" (c) 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
3 .\"
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.
7 .\"
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.
12 .\" 
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
19 .\" professionally.
20 .\" 
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" 
24 .\" Added _GNU_SOURCE, aeb, Sat Jul  5 23:10:04 MET 1997
25 .\"
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>
29 .\"
30 .TH SNPRINTF 3  "16 September 1995" "GNU" "Linux Programmer's Manual"
31 .\"O .SH NAME
32 .\"O .snprintf, vsnprintf \- formatted output conversion
33 .SH 名前
34 snprintf, vsnprintf \- 指定した書式で出力を行う
35 .\"O .SH SYNOPSIS
36 .SH 書式
37 .B #define _GNU_SOURCE
38 .br
39 .B #include <stdio.h>
40 .sp
41 .BI "int snprintf ( char *" str ", size_t " n ", "
42 .br
43 .BI "                         const char *" format ", ... );"
44 .sp
45 .B #include <stdarg.h>
46 .sp
47 .BI "int vsnprintf ( char *" str ", size_t " n ", "
48 .br
49 .BI "                         const char *" format ", va_list " ap " );"
50 .\"O .SH DESCRIPTION
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.
57 .SH 説明
58 \fBsnprintf\fP 関数は、\fIformat\fP で指定された書式に従い、それ以降に
59 与えられた引数を整形して出力し、出力は文字列 \fIstr\fP に書き込まれる。
60 この関数は \fBsprintf\fP(3) に似ているが、 出力される
61 最大文字数 \fIn\fP を指定できる点が異なる。
62 文字列の終端を示すヌル文字も最大文字数に含まれるため、
63 出力文字列 \fIstr\fP には少なくとも \fIn\fP 文字割り当てる必要がある。
64 .PP
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.)
76 .SH 返り値
77 出力が長く途中でカットされた場合には -1 を返し、それ以外は \fIstr\fP に保存
78 された文字数(文字列の終端を示す'\e0'を除く)を返す。
79 (但し glibc 2.0.6 までの仕様である。
80 glibc 2.1 以降では、これらの関数は、もし利用可能な十分なスペースがあれば
81 書き込まれたであろう文字数(文字列の終端を示す'\e0'を除く)を返す。)
82 .\"O .SH EXAMPLE
83 .SH 例
84 .br
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 のどちらでも正しく動作する。
89 .br
90 .sp
91 .nf
92 .\"O      /* Construct a message describing the value of a 
93 .\"O         variable whose name is NAME and whose value is 
94 .\"O         VALUE. */
95     /* 変数名とその値を記述する "value of 変数名 is 値"
96        というメッセージを作成する関数 */
97     char *
98     make_message (char *name, char *value)
99     {
100 .\"O       /* Guess we need no more than 100 chars of space. */
101       /* 出力バッファは100文字分で十分であると仮定 */
102       int size = 100;
103       char *buffer = (char *) xmalloc (size);
104       while (1)
105         {
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)
113             return buffer;
114 .\"O           /* Else try again with more space. */
115 .\"O           if (nchars > -1)
116 .\"O             size = nchars+1;  /* precisely what is needed */
117 .\"O           else
118 .\"O             size *= 2;        /* twice the old size */
119           /* 失敗すれば、さらにバッファを確保して再試行 */
120           if (nchars > -1)
121             size = nchars+1;  /* 必要なバッファ長を正確に確保 */
122           else
123             size *= 2;        /* バッファ長を2倍にする */
124           buffer = (char *) xrealloc (buffer, size);
125         }
126     }
127 .fi
128 .RE
129 .\"O .SH "CONFORMING TO"
130 .\"O These are GNU extensions, expected to become part of ISO C9x.
131 .SH 準拠
132 これらの関数は GNU による拡張である。ISO C9x の一部となる予定である。
133 .\"O .SH "SEE ALSO"
134 .SH 関連項目
135 .BR printf "(3), " sprintf "(3), " vsprintf "(3), " stdarg (3)