C함수 암호파일에서 읽기 위치를 첫 행으로 이동 setpwent()
패스워드 파일의 읽기 위치를 첫 행으로 이동합니다.
- 헤더: pwd.h
- 형태: void setpwent(void)
- 인수: -
- 반환: -
getpwent()는 /etc/passwd 파일 내용을 호출할 때 마다 행 하나씩 사용자 정보를 출력합니다. 다시 처음 행으로 읽기 위치를 변경하려면 setpwent()를 호출합니다. 암호 파일을 모두 사용했다면 endpwent()함수를 호출하여 암호 파일을 닫습니다.
/etc/passwd 파일 내용이 아래와 같다면,
getpwent()를 호출할 때 마다 행 하나씩 정보를 구하게 됩니다.
setpwent()를 호출하면 읽을 위치가 첫 행으로 이동합니다.
C언어 setpwent() 함수 예제
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
int main()
{
struct passwd *user_pw;
while( NULL != ( user_pw = getpwent()) ) {
printf( "user name :%s\n", user_pw->pw_name );
printf( "user id :%d\n", user_pw->pw_uid );
printf( "group id :%d\n", user_pw->pw_gid );
printf( "reall name :%s\n", user_pw->pw_gecos );
printf( "home directory :%s\n", user_pw->pw_dir );
printf( "shell program :%s\n", user_pw->pw_shell );
}
printf( "---------------------------------\n");
setpwent(); // 암호 파일의 첫 번째 행으로 이동
user_pw = getpwent(); // 암호 파일 첫 행을 출력
printf( "user name :%s\n", user_pw->pw_name );
printf( "user id :%d\n", user_pw->pw_uid );
printf( "group id :%d\n", user_pw->pw_gid );
printf( "reall name :%s\n", user_pw->pw_gecos );
printf( "home directory :%s\n", user_pw->pw_dir );
printf( "shell program :%s\n", user_pw->pw_shell );
endpwent(); // 암호 파일 닫음
return 0;
}
C언어 setpwent() 함수 예제 실행 결과
]$ ./a.out
user name :root
user id :0
group id :0
reall name :root
home directory :/root
shell program :/bin/bash
user name :bin
user id :1
group id :1
reall name :bin
home directory :/bin
shell program :/sbin/nologin
:
user name :jwmx
user id :500
group id :500
reall name :
home directory :/home/jwmx
shell program :/bin/bash
---------------------------------
user name :root
user id :0
group id :0
reall name :root
home directory :/root
shell program :/bin/bash
]$
'컴퓨터 > 프로그래밍' 카테고리의 다른 글
C언어 암호파일 닫기 함수 endpwent() (0) | 2020.03.14 |
---|---|
C언어 getpwent 함수 암호파일로부터 사용자 정보 구하기 (4) | 2020.03.14 |
C언어 로그인 유저 이름으로 사용자 정보 구하기 함수 getpwnam() (2) | 2020.03.14 |