0

我想只使用 c api 调用来遍历 /etc/passwd 中的所有记录条目。

我怎样才能做到这一点?

注意:我在其他语言中看到了很多如何做到这一点的示例,但没有在 c 中找到任何示例。

4

1 回答 1

2

这个小程序对我有用:

#include <stdio.h>
#include <pwd.h>

int main(int argc, char **argv) {

    struct passwd *pw;

    setpwent();
    while ( (pw = getpwent()) != NULL )
    {
        printf("%s\n", pw->pw_name);
    }
    endpwent();

    return 0;
}
于 2013-09-23T21:58:10.193 回答