rpmatch

Section: C Library Functions (3)
Updated: 2026-02-22
Index JM Home Page roff page
 

名前

rpmatch - 質問への応答が肯定か否定かを判定する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <stdlib.h>

int rpmatch(const char *response);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

rpmatch():

    Since glibc 2.19:
        _DEFAULT_SOURCE
    glibc 2.19 and earlier:
        _SVID_SOURCE
 

説明

rpmatch() は yes/no の質問に対するユーザーからの応答を処理する。 国際化 (I18N) に対応している。

response にはユーザーからの応答を格納したヌル終端文字列が入っている必要がある。 たいていは、 fgets(3) や getline(3) で取り込んだものであろう。

プログラムが setlocale(3) を呼び出して環境変数の変更を有効にした場合、 環境変数 LANG, LC_MESSAGES, LC_ALL が ユーザーの言語設定として考慮される。

Regardless of the locale, responses matching ^[Yy] are always accepted as affirmative, and those matching ^[Nn] are always accepted as negative.  

返り値

response を検査した後、 rpmatch() は否定的な応答 ("no") と認識した場合は 0 を返し、 肯定的な応答 ("yes") と認識した場合は 1 を返す。 response の値を解釈できなかった場合は -1 を返す。  

エラー

返り値 -1 が返った場合、入力が不正であったか、他の何らかのエラーが あったことを意味する。返り値が 0 以外かどうかを確認するだけでは 十分ではない。

rpmatch() can fail for any of the reasons that regcomp(3) or regexec(3) can fail; the error is not available from errno or anywhere else, but indicates a failure of the regex engine (but this case is indistinguishable from that of an unrecognized value of response).  

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
rpmatch() Thread safetyMT-Safe locale
 

標準規格

なし。  

歴史

GNU, FreeBSD, AIX.  

バグ

The YESEXPR and NOEXPR of some locales (including "C") only inspect the first character of the response. This can mean that "yno" et al. resolve to 1. This is an unfortunate historical side-effect which should be fixed in time with proper localisation, and should not deter from rpmatch() being the proper way to distinguish between binary answers.  

例

以下のプログラムは、コマンドライン引数で 指定された文字列を rpmatch() に渡した場合の結果を表示する。

#define _DEFAULT_SOURCE
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define streq(...)  (strcmp(__VA_ARGS__) == 0)

int
main(int argc, char *argv[])
{
    if (argc != 2 || streq(argv[1], "--help")) {
        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)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
属性
標準規格
歴史
バグ
例
関連項目

This document was created by man2html, using the manual pages.