==============================================================================
LAB> 사용자의 정보를 구하는 프로그래밍을 해보자.
# vi struct.c
-- struct.c --
#include <stdio.h>
struct test
{
int i;
int j;
};
int main()
{
struct test abc;
abc.i = 10;
abc.j = 20;
printf("abc.i = %d, abc.j = %d \n", abc.i, abc.j);
return 0;
}
-- struct.c --
# gcc -o struct struct.c
# ./struct
abc.i = 10, abc.j = 20
# cp struct.c struct2.c
# vi struct2.c
-- struct2.c --
#include <stdio.h>
struct test
{
int i;
int j;
};
int main()
{
struct test abc, *p;
p = &abc;
p->i = 10;
p->j = 20;
printf("p->i = %d, p->j = %d \n", p->i, p->j);
return 0;
}
# vi userinfo.c
-- userinfo.c --
#include <stdio.h> // /usr/include/stdio.h
#include <unistd.h> // /usr/include/unistd.h
#include <pwd.h> // /usr/include/pwd.h
#include <sys/types.h> // /usr/include/sys/types.h
int main()
{
char username[] = "bluesky";
struct passwd *userpw;
userpw = getpwnam(username);
printf("account : %s \n"
"uid : %d \n"
"gid : %d \n"
"comment : %d \n"
"home dir : %s \n"
"shell : %s \n", userpw->pw_name, userpw->pw_uid
, userpw->pw_gid, userpw->pw_gecos
, userpw->pw_dir, userpw->pw_shell
);
return 0;
}
-- userinfo.c --
# gcc -o userinfo userinfo.c
# ./userinfo
account : bluesky
uid : 7000
gid : 9000
comment : 148201500
home dir : /home1/bluesky
shell : /bin/sh
# usermod -g 100 bluesky
# grep bluesky /etc/passwd
bluesky:x:7000:100:testuser1:/home1/bluesky:/bin/sh
# ./userinfo
account : bluesky
uid : 7000
gid : 100
comment : 157347867
home dir : /home1/bluesky
shell : /bin/sh
==============================================================================