OSDN Git Service

(split) LDP_man-pages: release ioctl.2 [JM:00378]
[linuxjm/LDP_man-pages.git] / draft / man3 / strtol.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified Sun Jul 25 10:53:39 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
29 .\"
30 .\" Japanese Version Copyright (c) 1999 AKAMATSU, Kazuo
31 .\"         all rights reserved.
32 .\" Translated Thu Jan 9 21:00:00 JST 1999
33 .\"         by AKAMATSU, Kazuo
34 .\" Updated Sun Apr  8 JST 2001 by Kentaro Shirakata <argrath@ub32.org>
35 .\" Updated Sat Mar 23 JST 2002 by Kentaro Shirakata <argrath@ub32.org>
36 .\" Updated & Modified 2006-07-20,
37 .\"         Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
38 .\"
39 .\" WORD:       base            ´ð¿ô
40 .\"
41 .TH STRTOL 3  2010-09-20 "GNU" "Linux Programmer's Manual"
42 .SH Ì¾Á°
43 .\"O strtol, strtoll, strtoq \- convert a string to a long integer
44 strtol, strtoll, strtoq \- Ê¸»úÎó¤ò long int ¤ËÊÑ´¹¤¹¤ë
45 .SH ½ñ¼°
46 .nf
47 .B #include <stdlib.h>
48 .sp
49 .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
50 .sp
51 .BI "long long int strtoll(const char *" nptr ", char **" endptr \
52 ", int " base );
53 .fi
54 .sp
55 .in -4n
56 .\"O Feature Test Macro Requirements for glibc (see
57 .\"O .BR feature_test_macros (7)):
58 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
59 .RB ( feature_test_macros (7)
60 »²¾È):
61 .in
62 .sp
63 .ad l
64 .BR strtoll ():
65 .RS 4
66 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE ||
67 _POSIX_C_SOURCE\ >=\ 200112L;
68 .br
69 or
70 .I cc\ -std=c99
71 .RE
72 .ad
73 .\"O .SH DESCRIPTION
74 .SH ÀâÌÀ
75 .\"O The
76 .\"O .BR strtol ()
77 .\"O function converts the initial part of the string
78 .\"O in \fInptr\fP to a long integer value according to the given \fIbase\fP,
79 .\"O which must be between 2 and 36 inclusive, or be the special value 0.
80 .BR strtol ()
81 ´Ø¿ô¤Ï¡¢ \fInptr\fP ¤Îʸ»úÎó¤ÎºÇ½é¤ÎÉôʬ¤ò¡¢
82 \fIbase\fP ¤ò´ð¿ô¤È¤·¤Æ long int ¤ËÊÑ´¹¤¹¤ë¡£
83 ¤³¤Î \fIbase\fP ¤Ï 2 ¤«¤é 36 ¤Þ¤Ç¤ÎÃÍ
84 ¤¢¤ë¤¤¤ÏÆÃÊ̤ʰÕÌ£¤ò»ý¤ÄÃÍ 0 ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
85 .PP
86 .\"O The string may begin with an arbitrary amount of white space (as
87 .\"O determined by
88 .\"O .BR isspace (3))
89 .\"O followed by a single optional \(aq+\(aq or \(aq\-\(aq sign.
90 .\"O If \fIbase\fP is zero or 16, the string may then include a
91 .\"O "0x" prefix, and the number will be read in base 16; otherwise, a
92 .\"O zero \fIbase\fP is taken as 10 (decimal) unless the next character
93 .\"O is \(aq0\(aq, in which case it is taken as 8 (octal).
94 ʸ»úÎó¤ÎÀèƬ¤Ë¤Ï¡¢Ç¤°Õ¤Î¿ô¤Î¶õÇò¤¬¤¢¤Ã¤Æ¤â¤è¤¯ (¶õÇò¤Ï
95 .BR isspace (3)
96 ¤ÇȽÄꤵ¤ì¤ë)¡¢¤Þ¤¿¿ô»ú¤ÎľÁ°¤Ë¤Ï \(aq+\(aq ¤« \(aq\-\(aq ¤Î
97 Éä¹æ¤¬¤¢¤Ã¤Æ¤â¤è¤¤¡£
98 \fIbase\fP ¤¬ 0 ¤Þ¤¿¤Ï 16 ¤Î¾ì¹ç¤Ë¤Ï¡¢Ê¸»úÎó¤ÎÀèƬ¤Ë "0x" ¤òÃÖ¤¯¤³¤È¤¬
99 ¤Ç¤­¡¢¤½¤Î¾ì¹ç¤Ë¤Ïʸ»úÎó¤Ï 16¿Ê¿ô¤È¤·¤Æ°·¤ï¤ì¤ë¡£
100 ¤³¤ì°Ê³°¤Îʸ»úÎó¤Ç \fIbase\fP ¤¬ 0 ¤Î¾ì¹ç¤Ï¡¢
101 ʸ»úÎ󤬠\(aq0\(aq ¤Ç»Ï¤Þ¤ë¤È¤­¤Ï 8¿Ê¿ô¤È¤·¤Æ¡¢
102 ¤½¤ì°Ê³°¤Î¤È¤­¤Ï 10¿Ê¿ô¤È¤·¤Æ°·¤ï¤ì¤ë¡£
103 .PP
104 .\"O The remainder of the string is converted to a
105 .\"O .I long int
106 .\"O value
107 .\"O in the obvious manner, stopping at the first character which is not a
108 .\"O valid digit in the given base.
109 .\"O (In bases above 10, the letter \(aqA\(aq in
110 .\"O either upper or lower case represents 10, \(aqB\(aq represents 11, and so
111 .\"O forth, with \(aqZ\(aq representing 35.)
112 ¿ô»ú¤òɽ¤¹Ê¸»úÎó¤Ï
113 .I long int
114 ¤ËÊÑ´¹¤µ¤ì¤ë¤¬¡¢´ð¿ô¤ËÂФ·¤Æ
115 Í­¸ú¤Ç¤Ê¤¤¿ô»ú¤¬¸½¤ì¤¿»þÅÀ¤ÇÊÑ´¹¤Ï½ªÎ»¤¹¤ë¡£(11¿Ê¿ô°Ê¾å¤Ç¤Ï \(aqA\(aq ¤Ï
116 Âçʸ»ú¡¦¾®Ê¸»ú¤Ë´Ø¤ï¤é¤º 10 ¤òɽ¤·¡¢ \(aqB\(aq ¤Ï 11 ¤òɽ¸½¤·¡¢
117 °Ê²¼Æ±Íͤˡ¢ \(aqZ\(aq ¤Ï 35 ¤òɽ¤¹¡£)
118 .PP
119 .\"O If \fIendptr\fP is not NULL,
120 .\"O .BR strtol ()
121 .\"O stores the address of the
122 .\"O first invalid character in \fI*endptr\fP.
123 .\"O If there were no digits at
124 .\"O all,
125 .\"O .BR strtol ()
126 .\"O stores the original value of \fInptr\fP in
127 .\"O \fI*endptr\fP (and returns 0).
128 \fIendptr\fP ¤¬¥Ì¥ëÃÍ (NULL) ¤Ç¤Ê¤¤¾ì¹ç¤Ï¡¢ºÇ½é¤Ë¸½¤ì¤¿ÉÔÀµ¤Êʸ»ú¤¬
129 .BR strtol ()
130 ¤Ë¤è¤Ã¤Æ \fI*endptr\fP ¤ËÊݸ¤µ¤ì¤Æ¤¤¤ë¡£
131 ʸ»úÎó¤ËÍ­¸ú¤Ê¿ô»ú¤¬¤Ò¤È¤Ä¤â¤Ê¤±¤ì¤Ð¡¢
132 .BR strtol ()
133 ¤Ï \fInptr\fP
134 ¤Î¸µ¤ÎÃͤò \fI*endptr\fP ¤ËÂåÆþ¤¹¤ë (¤½¤·¤Æ 0 ¤òÊÖ¤¹)¡£
135 .\"O In particular, if \fI*nptr\fP is not \(aq\\0\(aq but \fI**endptr\fP
136 .\"O is \(aq\\0\(aq on return, the entire string is valid.
137 Æäˡ¢\fI*nptr\fP ¤¬ \(aq\\0\(aq °Ê³°¤Ç¡¢ÊÖ¤µ¤ì¤¿ \fI**endptr\fP ¤¬
138 \(aq\\0\(aq ¤Ê¤é¤Ð¡¢Ê¸»úÎóÁ´ÂΤ¬Í­¸ú¤À¤Ã¤¿¤³¤È¤Ë¤Ê¤ë¡£
139 .PP
140 .\"O The
141 .\"O .BR strtoll ()
142 .\"O function works just like the
143 .\"O .BR strtol ()
144 .\"O function but returns a long long integer value.
145 .BR strtoll ()
146 ´Ø¿ô¤Ï
147 .BR strtol ()
148 ¤ÈƱÍͤÀ¤¬¡¢long long int ·¿¤ÎÃͤòÊÖ¤¹¡£
149 .SH ÊÖ¤êÃÍ
150 .\"O The
151 .\"O .BR strtol ()
152 .\"O function returns the result of the conversion,
153 .\"O unless the value would underflow or overflow.
154 .\"O If an underflow occurs,
155 .\"O .BR strtol ()
156 .\"O returns
157 .\"O .BR LONG_MIN .
158 .\"O If an overflow occurs,
159 .\"O .BR strtol ()
160 .\"O returns
161 .\"O .BR LONG_MAX .
162 .\"O In both cases, \fIerrno\fP is set to
163 .\"O .BR ERANGE .
164 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤â¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤âµ¯¤­¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
165 .BR strtol ()
166 ´Ø¿ô¤Ï
167 ÊÑ´¹¤µ¤ì¤¿ÃͤòÊÖ¤¹¡£¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤·¤¿¾ì¹ç¤Ë¤Ï
168 .B LONG_MAX
169 ¤¬Ê֤ꡢ
170 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤·¤¿¾ì¹ç¤Ë¤Ï
171 .B LONG_MIN
172 ¤¬Ê֤롣¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¡¢
173 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤Î¤¤¤º¤ì¤Î¾ì¹ç¤Ë¤â
174 Âç°èÊÑ¿ô \fIerrno\fP ¤Ë¤Ï
175 .B ERANGE
176 ¤¬ÀßÄꤵ¤ì¤ë¡£
177 .\"O Precisely the same holds for
178 .\"O .BR strtoll ()
179 .\"O (with
180 .\"O .B LLONG_MIN
181 .\"O and
182 .\"O .B LLONG_MAX
183 .\"O instead of
184 .\"O .B LONG_MIN
185 .\"O and
186 .\"O .BR LONG_MAX ).
187 .BR strtoll ()
188 ¤âƱÍͤǤ¢¤ë¤¬¡¢
189 .B LONG_MIN
190 ¤È
191 .B LONG_MAX
192 ¤ÎÂå¤ï¤ê¤Ë
193 .B LLONG_MIN
194 ¤È
195 .B LLONG_MAX
196 ¤¬ÊÖ¤µ¤ì¤ë¡£
197 .SH ¥¨¥é¡¼
198 .TP
199 .B EINVAL
200 .\"O (not in C99)
201 .\"O The given
202 .\"O .I base
203 .\"O contains an unsupported value.
204 (C99 ¤Ë¤Ï¤Ê¤¤)
205 .I base
206 ¤¬Âбþ¤·¤Æ¤¤¤Ê¤¤ÃͤǤ¢¤ë¡£
207 .TP
208 .B ERANGE
209 .\"O The resulting value was out of range.
210 ·ë²Ì¤ÎÃͤ¬Èϰϳ°¤Ç¤¢¤ë¡£
211 .LP
212 .\"O The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
213 .\"O no conversion was performed (no digits seen, and 0 returned).
214 ¼ÂÁõ¤Ë¤è¤Ã¤Æ¤Ï¡¢ÊÑ´¹¤¬¹Ô¤ï¤ì¤Ê¤«¤Ã¤¿¾ì¹ç (¿ô»ú¤¬¤Ê¤¯¡¢0 ¤òÊÖ¤·¤¿¾ì¹ç)¡¢
215 \fIerrno\fP ¤Ë \fBEINVAL\fP ¤¬ÀßÄꤵ¤ì¤ë¾ì¹ç¤¬¤¢¤ë¡£
216 .SH ½àµò
217 .\"O .BR strtol ()
218 .\"O conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and
219 .\"O .BR strtoll ()
220 .\"O to C99 and POSIX.1-2001.
221 .BR strtol ()
222 ¤Ï SVr4, 4.3BSD, C89, C99 ¤È POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
223 .BR strtoll ()
224 ¤Ï C99 ¤È POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
225 .\"O .SH NOTES
226 .SH Ãí°Õ
227 .\"O Since
228 .\"O .BR strtol ()
229 .\"O can legitimately return 0,
230 .\"O .BR LONG_MAX ,
231 .\"O or
232 .\"O .B LONG_MIN
233 .\"O .RB ( LLONG_MAX
234 .\"O or
235 .\"O .B LLONG_MIN
236 .\"O for
237 .\"O .BR strtoll ())
238 .\"O on both success and failure, the calling program should set
239 .\"O .I errno
240 .\"O to 0 before the call,
241 .\"O and then determine if an error occurred by checking whether
242 .\"O .I errno
243 .\"O has a nonzero value after the call.
244 .B strtol ()
245 ¤«¤é¤ÏÀ®¸ù¡¢¼ºÇԤɤÁ¤é¤Î¾ì¹ç¤Ç¤â
246 0,
247 .BR LONG_MAX ,
248 .B LONG_MIN
249 .RB ( strtoll ()
250 ¤Ç¤Ï
251 .BR LLONG_MAX ,
252 .BR LLONG_MIN )
253 ¤¬ÊÖ¤ë²ÄǽÀ­¤¬¤¢¤ë¤Î¤Ç¡¢
254 ¥×¥í¥°¥é¥à¤Ï´Ø¿ô¤ò¸Æ¤Ó½Ð¤¹Á°¤Ë
255 .I errno
256 ¤ò 0 ¤ËÀßÄꤷ¡¢¸Æ¤Ó½Ð¤·¸å¤Ë
257 .I errno
258 ¤¬ 0 °Ê³°¤ÎÃͤ«¤É¤¦¤«¤ò³Îǧ¤·¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤«¤É¤¦¤«¤òȽÃǤ¹¤ë
259 ɬÍפ¬¤¢¤ë¡£
260
261 .\"O In locales other than the "C" locale, other strings may also be accepted.
262 .\"O (For example, the thousands separator of the current locale may be
263 .\"O supported.)
264 "C" °Ê³°¤Î¥í¥±¡¼¥ë¤Î¾ì¹ç¡¢¤½¤Î¾¤Îʸ»úÎó¤â¼õ¤±ÉÕ¤±¤é¤ì¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
265 (Î㤨¤Ð¡¢¸½ºß¤Î¥í¥±¡¼¥ë¤Î 1000 Ëè¤Î¶èÀÚ¤êʸ»ú¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£)
266 .LP
267 .\"O BSD also has
268 BSD ¤Ë¤Ï¡¢
269 .sp
270 .in +4n
271 .nf
272 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
273 .sp
274 .in
275 .fi
276 .\"O with completely analogous definition.
277 ¤È¤¤¤¦´°Á´¤ËƱÍͤÎÄêµÁ¤ò»ý¤Ä´Ø¿ô¤¬¤¢¤ë¡£
278 .\"O Depending on the wordsize of the current architecture, this
279 .\"O may be equivalent to
280 .\"O .BR strtoll ()
281 .\"O or to
282 .\"O .BR strtol ().
283 »ÈÍÑÃæ¤Î¥¢¡¼¥­¥Æ¥¯¥Á¥ã¤Î¥ï¡¼¥ÉĹ¼¡Âè¤Ç¤¢¤ë¤¬¡¢¤³¤Î´Ø¿ô¤Ï
284 .BR strtoll ()
285 ¤«
286 .BR strtol ()
287 ¤ÈÅù²Á¤È¤Ê¤ë¤³¤È¤â¤¢¤ë¡£
288 .\"O .SH EXAMPLE
289 .SH Îã
290 .\"O The program shown below demonstrates the use of
291 .\"O .BR strtol ().
292 .\"O The first command-line argument specifies a string from which
293 .\"O .BR strtol ()
294 .\"O should parse a number.
295 .\"O The second (optional) argument specifies the base to be used for
296 .\"O the conversion.
297 .\"O (This argument is converted to numeric form using
298 .\"O .BR atoi (3),
299 .\"O a function that performs no error checking and
300 .\"O has a simpler interface than
301 .\"O .BR strtol ().)
302 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï
303 .BR strtol ()
304 ¤Î»ÈÍÑÎã¤Ç¤¢¤ë¡£
305 ºÇ½é¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤Ë¤Ï
306 .BR strtol ()
307 ¤¬¿ô»ú¤È¤·¤Æ²ò¼á¤¹¤ëʸ»úÎó¤ò»ØÄꤹ¤ë¡£
308 (¾Êά²Äǽ¤Ê) ÆóÈÖÌܤΰú¤­¿ô¤Ë¤Ï
309 ÊÑ´¹¤Ë»ÈÍѤµ¤ì¤ë´ð¿ô¤ò»ØÄꤹ¤ë
310 (¤³¤Î°ú¤­¿ô¤Ï
311 .BR atoi (3)
312 ¤ò»È¤Ã¤Æ¿ôÃͤËÊÑ´¹¤µ¤ì¤ë¡£
313 .BR atoi (3)
314 ¤Ï
315 .BR strtol ()
316 ¤è¤ê¤â´Êñ¤Ê¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò»ý¤Ä´Ø¿ô¤Ç¡¢
317 ¤½¤ÎÃæ¤Ç¤Ï¥¨¥é¡¼¥Á¥§¥Ã¥¯¤Ï¹Ô¤ï¤ì¤Ê¤¤)¡£
318 .\"O Some examples of the results produced by this program are the following:
319 ¤³¤Î¥×¥í¥°¥é¥à¤Î¼Â¹ÔÎã¤ò¤¤¤¯¤Ä¤«°Ê²¼¤Ë¼¨¤¹:
320 .in +4n
321 .nf
322
323 .RB "$" " ./a.out 123"
324 strtol() returned 123
325 .RB "$" " ./a.out \(aq    123\(aq"
326 strtol() returned 123
327 .RB "$" " ./a.out 123abc"
328 strtol() returned 123
329 Further characters after number: abc
330 .RB "$" " ./a.out 123abc 55"
331 strtol: Invalid argument
332 .RB "$" " ./a.out \(aq\(aq"
333 No digits were found
334 .RB "$" " ./a.out 4000000000"
335 strtol: Numerical result out of range
336 .fi
337 .in
338 .\"O .SS Program source
339 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
340 \&
341 .nf
342 #include <stdlib.h>
343 #include <limits.h>
344 #include <stdio.h>
345 #include <errno.h>
346
347 int
348 main(int argc, char *argv[])
349 {
350     int base;
351     char *endptr, *str;
352     long val;
353
354     if (argc < 2) {
355         fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
356         exit(EXIT_FAILURE);
357     }
358
359     str = argv[1];
360     base = (argc > 2) ? atoi(argv[2]) : 10;
361
362     errno = 0;    /* To distinguish success/failure after call */
363     val = strtol(str, &endptr, base);
364
365     /* Check for various possible errors */
366
367     if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
368             || (errno != 0 && val == 0)) {
369         perror("strtol");
370         exit(EXIT_FAILURE);
371     }
372
373     if (endptr == str) {
374         fprintf(stderr, "No digits were found\\n");
375         exit(EXIT_FAILURE);
376     }
377
378     /* If we got here, strtol() successfully parsed a number */
379
380     printf("strtol() returned %ld\\n", val);
381
382     if (*endptr != \(aq\\0\(aq)        /* Not necessarily an error... */
383         printf("Further characters after number: %s\\n", endptr);
384
385     exit(EXIT_SUCCESS);
386 }
387 .fi
388 .\"O .SH "SEE ALSO"
389 .SH ´ØÏ¢¹àÌÜ
390 .BR atof (3),
391 .BR atoi (3),
392 .BR atol (3),
393 .BR strtod (3),
394 .BR strtoul (3)