OSDN Git Service

(split) LDP: Update drafts based on LDP v3.40.
[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 .\"
31 .\" This file was generated with po4a. Translate the source file.
32 .\"
33 .\"*******************************************************************
34 .TH STRTOL 3 2010\-09\-20 GNU "Linux Programmer's Manual"
35 .SH 名前
36 strtol, strtoll, strtoq \- 文字列を long int に変換する
37 .SH 書式
38 .nf
39 \fB#include <stdlib.h>\fP
40 .sp
41 \fBlong int strtol(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP
42 .sp
43 \fBlong long int strtoll(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP
44 .fi
45 .sp
46 .in -4n
47 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
48 .in
49 .sp
50 .ad l
51 \fBstrtoll\fP():
52 .RS 4
53 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE
54 || _POSIX_C_SOURCE\ >=\ 200112L;
55 .br
56 または \fIcc\ \-std=c99\fP
57 .RE
58 .ad
59 .SH 説明
60 \fBstrtol\fP()  関数は、 \fInptr\fP の文字列の最初の部分を、 \fIbase\fP を基数として long int に変換する。 この
61 \fIbase\fP は 2 から 36 までの値 あるいは特別な意味を持つ値 0 でなければならない。
62 .PP
63 文字列の先頭には、任意の数の空白があってもよく (空白は \fBisspace\fP(3) で判定される)、また数字の直前には \(aq+\(aq か
64 \(aq\-\(aq の 符号があってもよい。 \fIbase\fP が 0 または 16 の場合には、文字列の先頭に "0x" を置くことが
65 でき、その場合には文字列は 16進数として扱われる。 これ以外の文字列で \fIbase\fP が 0 の場合は、 文字列が \(aq0\(aq
66 で始まるときは 8進数として、 それ以外のときは 10進数として扱われる。
67 .PP
68 数字を表す文字列は \fIlong int\fP に変換されるが、基数に対して 有効でない数字が現れた時点で変換は終了する。(11進数以上では
69 \(aqA\(aq は 大文字・小文字に関わらず 10 を表し、 \(aqB\(aq は 11 を表現し、 以下同様に、 \(aqZ\(aq は 35
70 を表す。)
71 .PP
72 \fIendptr\fP がヌル値 (NULL) でない場合は、最初に現れた不正な文字が \fBstrtol\fP()  によって \fI*endptr\fP
73 に保存されている。 文字列に有効な数字がひとつもなければ、 \fBstrtol\fP()  は \fInptr\fP の元の値を \fI*endptr\fP に代入する
74 (そして 0 を返す)。 特に、\fI*nptr\fP が \(aq\e0\(aq 以外で、返された \fI**endptr\fP が \(aq\e0\(aq
75 ならば、文字列全体が有効だったことになる。
76 .PP
77 \fBstrtoll\fP()  関数は \fBstrtol\fP()  と同様だが、long long int 型の値を返す。
78 .SH 返り値
79 アンダーフローもオーバーフローも起きなかった場合、 \fBstrtol\fP()  関数は 変換された値を返す。オーバーフローした場合には
80 \fBLONG_MAX\fP が返り、 アンダーフローした場合には \fBLONG_MIN\fP が返る。オーバーフロー、 アンダーフローのいずれの場合にも
81 大域変数 \fIerrno\fP には \fBERANGE\fP が設定される。 \fBstrtoll\fP()  も同様であるが、 \fBLONG_MIN\fP と
82 \fBLONG_MAX\fP の代わりに \fBLLONG_MIN\fP と \fBLLONG_MAX\fP が返される。
83 .SH エラー
84 .TP 
85 \fBEINVAL\fP
86 (C99 にはない) 指定された \fIbase\fP がサポートされていない値である。
87 .TP 
88 \fBERANGE\fP
89 結果の値が範囲外である。
90 .LP
91 実装によっては、変換が行われなかった場合 (数字がなく、0 を返した場合)、 \fIerrno\fP に \fBEINVAL\fP が設定される場合がある。
92 .SH 準拠
93 \fBstrtol\fP()  は SVr4, 4.3BSD, C89, C99 と POSIX.1\-2001 に準拠している。 \fBstrtoll\fP()
94 は C99 と POSIX.1\-2001 に準拠している。
95 .SH 注意
96 \fBstrtol ()\fP からは成功、失敗どちらの場合でも 0, \fBLONG_MAX\fP, \fBLONG_MIN\fP (\fBstrtoll\fP()  では
97 \fBLLONG_MAX\fP, \fBLLONG_MIN\fP)  が返る可能性があるので、 プログラムは関数を呼び出す前に \fIerrno\fP を 0
98 に設定し、呼び出し後に \fIerrno\fP が 0 以外の値かどうかを確認しエラーが発生したかどうかを判断する 必要がある。
99
100 "C" 以外のロケールの場合、その他の文字列も受け付けられるかもしれない。 (例えば、現在のロケールの 1000
101 毎の区切り文字がサポートされているかもしれない。)
102 .LP
103 BSD には、
104 .sp
105 .in +4n
106 .nf
107 \fBquad_t strtoq(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP
108 .sp
109 .in
110 .fi
111 という完全に同様の定義を持つ関数がある。 使用中のアーキテクチャのワード長次第であるが、この関数は \fBstrtoll\fP()  か
112 \fBstrtol\fP()  と等価となることもある。
113 .SH 例
114 以下のプログラムは \fBstrtol\fP()  の使用例である。 最初のコマンドライン引き数には \fBstrtol\fP()
115 が数字として解釈する文字列を指定する。 (省略可能な) 二番目の引き数には 変換に使用される基数を指定する (この引き数は \fBatoi\fP(3)
116 を使って数値に変換される。 \fBatoi\fP(3)  は \fBstrtol\fP()  よりも簡単なインタフェースを持つ関数で、
117 その中ではエラーチェックは行われない)。 このプログラムの実行例をいくつか以下に示す:
118 .in +4n
119 .nf
120
121 $\fB ./a.out 123\fP
122 strtol() returned 123
123 $\fB ./a.out \(aq    123\(aq\fP
124 strtol() returned 123
125 $\fB ./a.out 123abc\fP
126 strtol() returned 123
127 Further characters after number: abc
128 $\fB ./a.out 123abc 55\fP
129 strtol: Invalid argument
130 $\fB ./a.out \(aq\(aq\fP
131 No digits were found
132 $\fB ./a.out 4000000000\fP
133 strtol: Numerical result out of range
134 .fi
135 .in
136 .SS プログラムのソース
137 \&
138 .nf
139 #include <stdlib.h>
140 #include <limits.h>
141 #include <stdio.h>
142 #include <errno.h>
143
144 int
145 main(int argc, char *argv[])
146 {
147     int base;
148     char *endptr, *str;
149     long val;
150
151     if (argc < 2) {
152         fprintf(stderr, "Usage: %s str [base]\en", argv[0]);
153         exit(EXIT_FAILURE);
154     }
155
156     str = argv[1];
157     base = (argc > 2) ? atoi(argv[2]) : 10;
158
159     errno = 0;    /* To distinguish success/failure after call */
160     val = strtol(str, &endptr, base);
161
162     /* Check for various possible errors */
163
164     if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
165             || (errno != 0 && val == 0)) {
166         perror("strtol");
167         exit(EXIT_FAILURE);
168     }
169
170     if (endptr == str) {
171         fprintf(stderr, "No digits were found\en");
172         exit(EXIT_FAILURE);
173     }
174
175     /* If we got here, strtol() successfully parsed a number */
176
177     printf("strtol() returned %ld\en", val);
178
179     if (*endptr != \(aq\e0\(aq)        /* Not necessarily an error... */
180         printf("Further characters after number: %s\en", endptr);
181
182     exit(EXIT_SUCCESS);
183 }
184 .fi
185 .SH 関連項目
186 \fBatof\fP(3), \fBatoi\fP(3), \fBatol\fP(3), \fBstrtod\fP(3), \fBstrtoul\fP(3)
187 .SH この文書について
188 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
189 である。プロジェクトの説明とバグ報告に関する情報は
190 http://www.kernel.org/doc/man\-pages/ に書かれている。