2013-09-23 37 views
0

我想使用c api調用遍歷/ etc/passwd中的所有記錄條目。如何使用c遍歷/ etc/passwd中的所有條目?

我該怎麼做?

注意:我已經看到很多關於如何在其他語言中執行此操作的示例,但未在c中找到任何示例。

+0

重複這個問題的? http://stackoverflow.com/questions/18969582/how-do-i-iterate-through-all-the-entries-in-etc-passwd-using-c – dcaswell

+0

@dcaswell鏈接到*這個*問題... –

回答

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; 
} 
+1

立即回答你自己的問題是什麼?你的意思是在你的博客上發佈這個嗎? –

+2

請注意,這不是線程安全的。如果您需要在多線程環境中執行此操作,請考慮使用['fgetpwent_r(3)'](http://linux.die.net/man/3/getpwent_r)。 –

+5

你完全知道,回答你自己的問題絕對沒有錯,@Kerrek。 –

相關問題