getpwent_r

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

名前

getpwent_r, fgetpwent_r - パスワードファイルのエントリーを リエントラントで取り出す  

ライブラリ

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

書式

#include <pwd.h>

int getpwent_r(size_t size;
               struct passwd *restrict pwbuf,
               char buf[restrict size], size_t size,
               struct passwd **restrict pwbufp);
int fgetpwent_r(size_t size;
               FILE *restrict stream, struct passwd *restrict pwbuf,
               char buf[restrict size], size_t size,
               struct passwd **restrict pwbufp);

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

getpwent_r(),

    glibc 2.19 以降:
        _DEFAULT_SOURCE
    glibc 2.19 以前:
        _BSD_SOURCE || _SVID_SOURCE

fgetpwent_r():

    glibc 2.19 以降:
        _DEFAULT_SOURCE
    glibc 2.19 以前:
        _SVID_SOURCE
 

説明

関数 getpwent_r() と fgetpwent_r() は getpwent(3) と fgetpwent(3) のリエントラント (reentrant) 版である。 前者は、 setpwent(3) によって初期化されたストリームから、次のパスワードエントリーを読み込む。 後者は、 stream から次のパスワードエントリーを読み込む。

passwd 構造体は、<pwd.h> で以下のように定義されている:

struct passwd {
    char    *pw_name;      /* ユーザー名 */
    char    *pw_passwd;    /* ユーザーのパスワード */
    uid_t    pw_uid;       /* ユーザー ID */
    gid_t    pw_gid;       /* グループ ID */
    char    *pw_gecos;     /* ユーザー情報 */
    char    *pw_dir;       /* ホームディレクトリ */
    char    *pw_shell;     /* シェルプログラム */
};

この構造体のフィールドの詳細は passwd(5) を参照のこと。

The nonreentrant functions return a pointer to static storage, where this static storage contains further pointers to user name, password, gecos field, home directory and shell. The reentrant functions described here return all of that in caller-provided buffers. First of all there is the buffer pwbuf that can hold a struct passwd. And next the buffer buf of size size that can hold additional strings. The result of these functions, the struct passwd read from the stream, is stored in the provided buffer *pwbuf, and a pointer to this struct passwd is returned in *pwbufp.  

返り値

On success, these functions return 0 and *pwbufp is a pointer to the struct passwd. On error, these functions return an error value and *pwbufp is NULL.  

エラー

ENOENT
次のエントリーがない。
ERANGE
十分なバッファー空間が与えられていない。 もっと大きなバッファーで再度実行すること。
 

属性

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

In the above table, pwent in race:pwent signifies that if any of the functions setpwent(), getpwent(), endpwent(), or getpwent_r() are used in parallel in different threads of a program, then data races could occur.  

バージョン

Other systems use the prototype

struct passwd *
getpwent_r(struct passwd *pwd, char buf[.size], int size);

より良いものでは、以下のようになっている。

int
getpwent_r(struct passwd *pwd, char buf[.size], int size,
           FILE **pw_fp);
 

標準規格

なし。  

歴史

These functions are done in a style resembling the POSIX version of functions like getpwnam_r(3).  

注意

関数 getpwent_r() は本当のリエントラントではない。 なぜなら、ストリームの読み込み位置を 他の全てのスレッドと共有しているためである。  

例

#define _GNU_SOURCE
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFLEN 4096

int
main(void)
{
    struct passwd pw;
    struct passwd *pwp;
    char buf[BUFLEN];
    int i;

    setpwent();
    for (;;) {
        i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
        if (i)
            break;
        printf("%s (%jd)\tHOME %s\tSHELL %s\n", pwp->pw_name,
               (intmax_t) pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
    }
    endpwent();
    exit(EXIT_SUCCESS);
}
 

関連項目

fgetpwent(3), getpw(3), getpwent(3), getpwnam(3), getpwuid(3), putpwent(3), passwd(5)


 

Index

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

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