1. 获取用户信息
1.1 获取用户名和uid
1.2 getuid与getlogin实例
1.3 获取详细的用户信息
1.4 getpwuid, getpwnam函数实例
1.5. getpwuid简单实现
2. 获取系统信息
2.1 相关函数
2.2 实例
本文简单的对linux中获取用户信息和系统相关信息进行学习。
1. 获取用户信息
1.1 获取用户名和uid
复制代码
1.2 getuid与getlogin实例
复制代码
测试:
复制代码
1.3 获取详细的用户信息
系统文件/etc/passwd包含一个用户帐号数据库。它由行组成,每行对应一个用户,包括:
用户名、加密口令、用户标识符(UID)、组标识符(GID)、全名、主目录和默认shell。
编程接口的数据结构:
strcut passwd {
char *pw_name;
char *pw_passwd;
uid_t pw_uid;
gid_t pw_gid;
char *pw_gecos;
char *pw_dir;
char *pw_shell;
}
复制代码
1.4 getpwuid, getpwnam函数实例
复制代码
测试:
复制代码
1.5. getpwuid简单实现
复制代码
2. 获取系统信息
2.1 相关函数
复制代码
2.2 实例
复制代码
复制代码
1.1 获取用户名和uid
1.2 getuid与getlogin实例
1.3 获取详细的用户信息
1.4 getpwuid, getpwnam函数实例
1.5. getpwuid简单实现
2. 获取系统信息
2.1 相关函数
2.2 实例
本文简单的对linux中获取用户信息和系统相关信息进行学习。
1. 获取用户信息
1.1 获取用户名和uid
- #include <sys/types.h>
- #include <unistd.h>
- uid_t getuid(void);
- char *getlogin(void);
- getuid函数返回程序关联的UID,它通常是启动程序的用户的UID。
- getlogin函数返回与当前用户关联的登录名。
- filename:1.c
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdio.h>
- int main(void)
- {
- printf("The UID is %d \n", getuid());
- printf("The login name is %s\n", getlogin());
- return 0;
- }
- [test@test_net code]$ ./1
- The UID is 500
- The login name is test
- [test@test_net code]$
系统文件/etc/passwd包含一个用户帐号数据库。它由行组成,每行对应一个用户,包括:
用户名、加密口令、用户标识符(UID)、组标识符(GID)、全名、主目录和默认shell。
编程接口的数据结构:
strcut passwd {
char *pw_name;
char *pw_passwd;
uid_t pw_uid;
gid_t pw_gid;
char *pw_gecos;
char *pw_dir;
char *pw_shell;
}
- #include <sys/types.h>
- #include <pwd.h>
- struct passwd *getpwuid(uid_t uid);
- struct passwd *getpwnam(const char *name);
- 这两个函数都返回一个指针,指向passwd结构。出错时,返回NULL,并设置errno。
- void endpwent(void);
- void setpwent(void);
- struct passwd *getpwent(void);
- 这一组函数能够扫描整个文件。getpwent函数依次返回每个用户的信息项,并且将指针后移,当到达文件尾时,返回一个空指针。endpwent终止,并释放资源。setpwent函数重置读指针到密码文件的开始位置。
- filename:2.c
- #include <assert.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <pwd.h>
- #include <stdio.h>
- int main(void)
- {
- struct passwd *pw;
- uid_t uid;
- char *login;
- uid = getuid();
- assert(uid != -1);
- printf("UID is %d\n", uid);
- login = getlogin();
- assert(login != NULL);
- printf("User is %s\n", login);
-
- pw = getpwuid(uid);
- assert(pw != NULL);
- printf("\n\n\n");
- printf("getpwuid:\n");
- printf("name = %s\nuid = %d\ngid = %d\nhome = %s\nshell = %s\n",\
- pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
- pw = getpwnam("root");
- assert(pw);
- printf("\n\n\n");
- printf("getpwnam(root):\n");
- printf("name = %s\nuid = %d\ngid = %d\nhome = %s\nshell = %s\n",\
- pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
- return 0;
- }
- [test@test_net code]$ ./2
- UID is 500
- User is test
- getpwuid:
- name = test
- uid = 500
- gid = 500
- home = /home/test
- shell = /bin/bash
- getpwnam(root):
- name = root
- uid = 0
- gid = 0
- home = /root
- shell = /bin/bash
- [test@test_net code]$
- filename:my_getpwuid.c
- #include <pwd.h>
- static struct passwd *
- my_getpwuid(uid_t uid)
- {
- struct passwd *ptr;
- setpwent();
- while ((ptr = getpwent()) != 0) {
- if (uid == ptr->pw_uid)
- break;
- }
- endpwent();
- return ptr;
- }
2.1 相关函数
- #include <unistd.h>
- int gethostname(char *name, size_t namelen);
- 该函数把机器的网络名写入name字符串。
- #include <sys/utsname.h>
- int uname(struct utsname *name);
- uname函数把主机信息写入name参数指向的结构。
- struct utsname {
- char sysname[];
- char nodename[];
- char release[];
- char version[];
- char machine[];
- };
- filename:3.c
- #include <unistd.h>
- #include <sys/utsname.h>
- #include <stdio.h>
- int main(void)
- {
- char computer[256];
- struct utsname uts;
- if (gethostname(computer, 255) != 0 || uname(&uts) != 0) {
- fprintf(stderr, "can not get host information\n");
- return 1;
- }
- printf("host name: %s\n", computer);
- printf("system name: %s\n", uts.sysname);
- printf("hardware : %s\n", uts.machine);
- printf("nodename : %s\n", uts.nodename);
- printf("release: %s\n", uts.release);
- printf("version: %s\n", uts.version);
- return 0;
- }
- [test@test_net code]$ ./3
- host name: test_net
- system name: Linux
- hardware : x86_64
- nodename : test_net
- release: 2.6.40.4-5.fc15.x86_64
- version: #1 SMP Tue Aug 30 14:38:32 UTC 2011
- [test@test_net code]$