介紹
獲得訪問影子密碼檔案的接口。頭檔案
#include <sys/types.h>#include <shadow.h>
語法
struct spwd* getspent( void );struct spwd* getspent_r( struct spwd* result,
char* buffer,
int buflen );
參數
這些參數只適用於getspent_r():resultA 結構體spwd,指針更多內容見. putspent();
bufferA 輸出buffer;
bufsize 緩衝區bufferA 位元組大小。
庫
libc說明
第一次調用時會取得第一項組數據,之後每調用一次就會返回下一項數據,直到已無任何數據時返回NULL。讀取數據完畢後可使用endspent()來關閉該組檔案
注意 | fgetspent(),getspent(), getspnam(), and 共享相同的靜態緩衝區 |
例子
#include <stdio.h>#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
/*
* This program loops, reading a login name from standard
* input and checking to see if it is a valid name. If it
* is not valid, the entire contents of the name in the
* password database are printed.
*/
int main(int argc, char** argv)
{
struct spwd* sp;
char buf[80];
setpwent( );
while( gets( buf ) != NULL ) {
if( ( sp = getspnam( buf ) ) != ( struct spwd * )0 ) {
printf( "Valid login name is: %s\n", sp->sp_namp );
} else {
setspent( );
while( ( sp=getspent( ) ) != ( struct spwd * )0 )
printf( "%s\n", sp->sp_namp );
}
}
endspent();
return( EXIT_SUCCESS );
}