C함수 로그인 유저 이름으로 사용자 정보 구하기 getpwnam()
로그인 유저 이름으로 사용자 정보를 구합니다. 사용자 정보는 pwd.h 에 정의된 struct passwd에 따릅니다.
- 헤더: pwd.h
- 형태: struct passwd *getpwnam( char *user_name)
- 인수: strong>char * 사용자 이름
- 반환: struct passwd * 사용자 정보를 담고 있는 struct passwd 포인터
struct passwd
struct passwd{
char *pw_name; /* Username. */
char *pw_passwd; /* Password. */
__uid_t pw_uid; /* User ID. */
__gid_t pw_gid; /* Group ID. */
char *pw_gecos; /* Real name. */
char *pw_dir; /* Home directory. */
char *pw_shell; /* Shell program. */
};
C언어 getpwnam() 함수 예제
#include <stdio.h>
#include <unistd.h> // getlogin
#include <pwd.h> // getpwnam
#include <sys/types.h> // uid_t
int main()
{
char *user_name;
struct passwd *user_pw;
user_name = getlogin(); // 로그인 이름 구하기
user_pw = getpwnam( user_name); // 로그인 이름으로 사용자 정보 구하기
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 );
return 0;
}
C언어 getpwnam() 예제 실행 결과
]$ ./a.out
user name :jwmx
user id :500
group id :500
reall name :
home directory :/home/jwmx
shell program :/bin/bash
]$
'컴퓨터 > 프로그래밍' 카테고리의 다른 글
C언어 getpwent 함수 암호파일로부터 사용자 정보 구하기 (4) | 2020.03.14 |
---|---|
C언어 사용자 ID로 사용자 정보 구하기 함수 getpwuid() (0) | 2020.03.14 |
C언어 로그인 유저 이름 구하기 함수 getlogin() (0) | 2020.03.14 |