rpmatchのヘルプ・マニュアル
日本語 英語
rpmatch --help
man rpmatch
RPMATCH(3) Linux Programmer’s Manual RPMATCH(3)
名前
rpmatch - 質問への応答が肯定か否定かを判定する
書式
#include
int rpmatch(const char *response);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
rpmatch(): _SVID_SOURCE
説明
rpmatch() は yes/no の質問に対するユーザからの応答を処理する。国際化
(I18N) に対応している。
response にはユーザからの応答を格納した NULL 終端文字列が入っている必要
がある。たいていは、 fgets(3) や getline(3) で取り込んだものであろう。
プ ログラムが setlocale(3) を呼び出して環境変数の変更を有効にした場合、
環境変数 LANG, LC_MESSAGES, LC_ALL がユーザの言語設定として考慮される。
ロ ケ ー ル に 関わらず、^[Yy] にマッチする応答は常に肯定だと解釈され、
^[Nn] にマッチする応答は常に否定だと解釈される。
返り値
response を検査した後、 rpmatch() は否定的な応答 ("no") と認識した場 合
は 0 を返し、肯定的な応答 ("yes") と認識した場合は 1 を返す。 response
の値を解釈できなかった場合は -1 を返す。
エラー
返り値 -1 が返った場合、入力が不正であったか、他の何らかのエラーがあ っ
たことを意味する。返り値が 0 以外かどうかを確認するだけでは十分ではない
。
rpmatch() は、 regcomp(3) や regexec(3) が失敗する理由のどれかで失敗 す
る こ とがある。エラーの原因を errno や他の何かで知ることはできないが、
errno は正規表現エンジンの失敗の原因を示している (但し、このケ ー ス と
response の値を認識できずに失敗した場合を区別することはできない)。
準拠
rpmatch() はどの標準でも必須となっていないが、 Linux 以外にも利用できる
システムもいくつかは存在する。
バグ
rpmatch() の実装は response の最初の 1 文字だけを見ているようである。そ
の 結果、 "nyes" は 0 を返し、 "ynever; not in a million years" は 1 を
返すことになる。入力文字列をもっと厳密に解釈した方がよいだろう。例え ば
、 (regex(7) で説明されている拡張正規表現を使って) ^([yY]|yes|YES)$ や
^([nN]|no|NO)$ で解釈するなど。
例
以下のプログラムは、コマンドライン引き数で指定された文字列を rpmatch()
に渡した場合の結果を表示する。
#define _SVID_SOURCE
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
if (argc != 2 || strcmp(argv[1], "--help") == 0) {
fprintf(stderr, "%s response\n", argv[0]);
exit(EXIT_FAILURE);
}
setlocale(LC_ALL, "");
printf("rpmatch() returns: %d\n", rpmatch(argv[1]));
exit(EXIT_SUCCESS);
}
関連項目
fgets(3), getline(3), nl_langinfo(3), regcomp(3), setlocale(3)
GNU 2007-07-26 RPMATCH(3)
RPMATCH(3) Linux Programmer’s Manual RPMATCH(3)
NAME
rpmatch - determine if the answer to a question is affirmative or nega-
tive
SYNOPSIS
#include
int rpmatch(const char *response);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
rpmatch(): _SVID_SOURCE
DESCRIPTION
rpmatch() handles a user response to yes or no questions, with support
for internationalization.
response should be a null-terminated string containing a user-supplied
response, perhaps obtained with fgets(3) or getline(3).
The user’s language preference is taken into account per the environ-
ment variables LANG, LC_MESSAGES, and LC_ALL, if the program has called
setlocale(3) to effect their changes.
Regardless of the locale, responses matching ^[Yy] are always accepted
as affirmative, and those matching ^[Nn] are always accepted as nega-
tive.
RETURN VALUE
After examining response, rpmatch() returns 0 for a recognized negative
response ("no"), 1 for a recognized positive response ("yes"), and -1
when the value of response is unrecognized.
ERRORS
A return value of -1 may indicate either an invalid input, or some
other error. It is incorrect to only test if the return value is non-
zero.
rpmatch() can fail for any of the reasons that regcomp(3) or regexec(3)
can fail; the cause of the error is not available from errno or any-
where else, but indicates a failure of the regex engine (but this case
is indistinguishable from that of an unrecognized value of response).
CONFORMING TO
rpmatch() is not required by any standard, but is available on a few
other systems.
BUGS
The rpmatch() implementation looks at only the first character of
response. As a consequence, "nyes" returns 0, and "ynever; not in a
million years" returns 1. It would be preferable to accept input
strings much more strictly, for example (using the extended regular
expression notation described in regex(7)): ^([yY]|yes|YES)$ and
^([nN]|no|NO)$.
EXAMPLE
The following program displays the results when rpmatch() is applied to
the string given in the program’s command-line argument.
#define _SVID_SOURCE
#include
#include
#include
#include
int
main(int argc, char *argv[])
{
if (argc != 2 || strcmp(argv[1], "--help") == 0) {
fprintf(stderr, "%s response\n", argv[0]);
exit(EXIT_FAILURE);
}
setlocale(LC_ALL, "");
printf("rpmatch() returns: %d\n", rpmatch(argv[1]));
exit(EXIT_SUCCESS);
}
SEE ALSO
fgets(3), getline(3), nl_langinfo(3), regcomp(3), setlocale(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2007-07-26 RPMATCH(3)