/etc/passwd
root:x:0:0:root:/root:/bin/bash
squid:x:23:23::/var/spool/squid:/dev/null
nobody:x:65534:65534:Nobody:/home:/bin/sh
sar:x:205:105:Stephen Rago:/home/sar:/bin/bash
通过命令查看用户信息:
$ finger -p sar
Login: sar Name: Steve Rago
Directory: /home/sar Shell: /bin/sh
Office: SF 5-121, 555-1111 Home Phone: 555-2222
On since Mon Jan 19 03:57 (EST) on ttyv0 (messages off)
No Mail.
通过函数查看用户信息:
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
如果要查看整个文件的记录项:
#include <pwd.h>
struct passwd *getpwent(void);
//Returns: pointer if OK, NULL on error or end of file
void setpwent(void);
void endpwent(void);
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
// Both return: pointer if OK, NULL on error
void setspent(void);
void endspent(void);
#include <grp.h>
struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);
#include <grp.h>
struct group *getgrent(void);
// Returns: pointer if OK, NULL on error or end of file
void setgrent(void);
void endgrent(void);
#include <unistd.h>
int getgroups(int gidsetsize, gid_t grouplist[]);
// Returns: number of supplementary group IDs if OK, −1 on error
#include <grp.h> /* on Linux */
#include <unistd.h> /* on FreeBSD, Mac OS X, and Solaris */
int setgroups(int ngroups, const gid_t grouplist[]);
#include <grp.h> /* on Linux and Solaris */
#include <unistd.h> /* on FreeBSD and Mac OS X */
int initgroups(const char *username, gid_t basegid);
// Both return: 0 if OK, −1 on error
一般情况,对每个数据文件至少有3个函数:
- get()
- set()
- end()
struct utmp {
char ut_line[8]; /* tty line: "ttyh0", "ttyd0", "ttyp0", ... */
char ut_name[8]; /* login name */
long ut_time; /* seconds since Epoch */
};
#include <sys/utsname.h>
int uname(struct utsname *name);
// Returns: non-negative value if OK, −1 on error
struct utsname {
char sysname[]; /* name of the operating system */
char nodename[]; /* name of this node */
char release[]; /* current release of operating system */
char version[]; /* current version of this release */
char machine[]; /* name of hardware type */
};
历史上,有只返回主机名的接口:
#include <unistd.h>
int gethostname(char *name, int namelen);
// Returns: 0 if OK, −1 on error
ref : https://github.com/breakerthb/LinuxPrograming/blob/master/NoteBook/Time.md
