[06]APUE:系统数据文件和信息
[a] getpwent / setpwent / endpwent
#include <pwd.h>
struct passwd *getpwent(void) //成功返回指针,出错或到过文件末尾返回 NULL
void setpwent(void)
void endpwent(void)
struct passwd {
char *pw_name;
char *pw_passwd; //口令
uid_t pw_uid;
gid_t pw_gid;
char *pw_geos; //用户信息
char *pw_dir; //家目录
char *pw_shell;
char *pw_class; //用户访问类,仅 BSD
time_t pw_change; //下次修改口令时间,仅 BSD
time_t pw_expire; //账户有效时间,仅 BSD
}
- getpwent 逐条访问并返回下一项的结构体指针
- setpwent 打开并返绕至文件开头
- endpwent 关闭所有打开的相关文件
- 各系统至少支持 struct passwd 中的 7 项,FreeBSD 环境下特权进程可获取加密后的密码,其它系统需要使用 struct spwd
[b] getpwuid / getpwnam
#include <pwd.h>
struct passwd *getpwuid(uid_t uid)
struct passwd *getpwnam(const char *username)
//成功返回指针,出错返回 NULL
- 获取指定 uid 或 username 的全部账户信息
[c] getgrent / setgrent / endgrent
#include <grp.h>
struct group *getgrent(void) //成功返回指针,出错或到达文件末尾返回 NULL
void setgrent(void)
void endgrent(void)
struct group {
char *gr_name;
char *gr_passwd;
int gr_gid;
char **gr_mem;
}
[d] getgrgid / getgrnam
#include <grp.h>
struct group *getgrgid(gid_t gid)
struct group *getgrnam(const char *groupname)
//成功返回指针,出错返回 NULL
[e] getspent / setspent / endspent
#include <shadow.h>
struct spwd *getspent(void) //出错返回 NULL
void setspent(void)
void endspent(void)
struct spwd {
char *sp_namp;
char *sp_pwdp;int sp_lstchg;
int sp_min;
int sp_max;
int sp_warn;
int sp_inact;
int sp_expire;
unsigned int sp_flag;
}
- spwd 结构体条目一一对应于 /etc/shadow 的各字段
- 仅适用于 Linux 平台
- FreeBSD 下使用 /etc/master.passwd 及 其对应的 HASH 副本 /etc/spwd.db,通 getpwent 系统函数访问
[f] getspnam
#include <shadow.h>
struct spwd *getspnam(const char *username) //出错返回 NULL
- 仅适用于 Linux 平台
[g] getgroups
#include <unistd.h>
int getgroups(int gidsetsize, gid_t grouplist[]) //成功返回附属组 ID 数量,出错返回 -1
- 成功执行后,附属组信息将写入 grouplist[]
- gidsetsize 指定附属组 ID 的最大数量,若指定为 0,函数返回实际的附属组数量,但不将信息写入 grouplist[]
[h] getutxent / setutxent /endutxent
#include <utmpx.h>
struct utmpx *getutxent(void) //成功返回指针,出错或到达文件末尾返回 NULL
void setutxent(void)
void endutxent(void)
struct utmpx {
short ut_type; //条目类别
struct timeval ut_tv; //登陆起始时间
pid_t ut_pid; //进程 id
char ut_id[]; //记录标识
char ut_user[]; //用户登陆名
char ut_line[]; //tty 名称,形如 /dev/ttyN
char ut_host[]; //远程主机名称,FreeBSD 扩展项
}
- 用于提取用户登陆信息,即 last 命令的输出信息
[i] getutxuser
#include <utmpx.h>
struct utmpx *getutxuser(const char *user) //成功返回 utmpx 结构体指针,出错或到达文件末尾返回 NULL
- 提取指定用户名称的登陆信息,FreeBSD 的扩展功能
[j] uname / gethostname /sethostname
#include <sys/utsname.h>
int uname(struct utsname *name) //成功返回非负值,出错返回 -1
#include <unistd.h>
int gethostname(char *name, int len)
int sethostname(const char *name, int len)
//成功返回 0,出错返回 -1
struct utsname {
char sysname[]; //系统类别名,如 Linux
char nodename[]; //节点名称,不可用于网络通信
char release[]; //系统主版本号
char version[]; //系统次版本号
char machine[]; //硬 件信息
}
- 以上 3 个函数,各条目字段最大长度(含末尾的 null 字节)为 64(Linux) / 256(FreeBSD)
- gethostname 用于获取网络主机名称,信息写入 name 缓冲区,len 指定缓冲区大小,以 null 字节结尾
- sethostname 特权进程可设置网络主机名称,不需要添加 \0 字节
[k] time
#include <time.h>
time_t time(time_t *calptr) //成功返回时间值,出错返回 -1
- 返回的时间值是自 1970-01-01 00:00:00 以来经过的秒数
- 若参数不为 NULL,时间值同时写入 calptr 所指向的目标
[l] clock_gettime / clock_settime
#include <sys/time.h>
int clock_gettime(clockid_t clock_id, struct timespec *tsp) //成功返回 0,出错返回 -1
int clock_settime(clockid_t clock_id, const struct timespec *tsp) //同上
- clock_id 的值取 CLOCK_REALTIME 时,与 time 函数功能类似,但可以获取更高精度时间值,最高可精确到纳秒
[m] gmtime / localtime
#include <time.h>
struct tm *gmtime(const time_t *calptr)
struct tm *localtime(const time_t *calptr)
//成功返回指针,出错返回 NULL
struct tm {
int tm_sec; //0-60
int tm_min; //0-59
int tm_hour; //0-23
int tm_mday; //1-31
int tm_mon; //0-11
int tm_year; //自 1990 以后的年份计数
int tm_wday; //0-6
int tm_yday; //0-365
int tm_isdst; //夏令时标志:<0, 0, >0
}
- struct tm 中,除 tm_mday 外,其余字段均从 0 开始计数,实际的年份数值是 tm_year + 1990
- gmtime 将世界协调时间(UTC)时间信息写入 struct tm,localtime 则写入本地时间信息
[n] mktime
#include <time.h>
time_t mktime(struct tm *tmptr) //成功返回时间值,出错返回 -1
- 将 tm 结构转换成 time_t 时间值
[o] strftime
#include <time.h>
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm) //成功返回写入的字符数量,否则返回 0
- 常用于将 tm 时间转换成格式化字符串,之后通过 printf 等函数输出
- 写入 tm 结构体的信息,受时区 TZ 环境变量影响
- format 常用的格式有 %Y(四位年份,如 2019) / %y(两位年份,如 19) / %m(月) / %d(日) / %H(小时) / %M(分钟) /%S(秒)
[06]APUE:系统数据文件和信息的更多相关文章
- [APUE]系统数据文件与信息
一.口令文件 UNIX口令文件包含下表中的各个字段,这些字段包含在 由于历史原因,口令文件是/bin/passwd,而且是一个文本文件,每一行都包括了上表中的七个字段,字段之间用":&quo ...
- (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- apue学习笔记(第六章 系统数据文件和信息)
UNIX系统的正常运作需要使用大量与系统有关的数据文件,例如,口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件. 口令文件 UNIX系统口令文件包含如下字 ...
- UNIX系统高级编程——第六章-系统数据文件和信息-总结
口令文件: /* The passwd structure. */ struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* ...
- UNIX环境高级编程 第6章 系统数据文件和信息
UNIX系统的正常运作需要用到大量与系统有关的数据文件,例如系统用户账号.用户密码.用户组等文件.出于历史原因,这些数据文件都是ASCII文本文件,并且使用标准I/O库函数来读取. 口令文件 /etc ...
- 《UNIX环境高级编程》读书笔记之系统数据文件和信息(1)
1.UNIX系统口令文件包括了下图所看到的的各字段,这些字段包括在<pwd.h>中定义的passwd结构体中 POSIX定义了两个获取口令文件项的函数. 在给出用户登录名或用户ID后.这两 ...
- apue 第6章 系统数据文件和信息
在给出用户登录名或数值用户ID后,这两个函数就能查看相关项. #include <sys/types.h> #include <pwd.h> struct passwd *ge ...
- APUE学习笔记——6 系统数据文件与信息
1.用户口令:/etc/passwd文件 该文件中包含下列结构体信息.其中,当下主修熊passwd不再这里显示,是使用了一个占位符. struct passwd { char * pw_name; / ...
- linux c编程:系统数据文件和信息
linux系统相关的文件信息包含在/etc/passwd文件和/etc/group中.每次登录linux系统以及每次执行ls -l命令时都要使用口令文件.这些字段都包含在<pwd.h>中定 ...
随机推荐
- 实时消息平台NSQ的特性
NSQ是GO语言开发的可用于大规模系统中的实时消息服务,但是和RabbitMQ等相比,它具有什么特色,什么场景下选择NSQ呢? NSQ的自身特色很明显,最主要的优势在如下三个方面: 1,性能.在多个著 ...
- SqlServer字段说明查询
SELECT t.[name] AS 表名,c.[name] AS 字段名,cast(ep.[value] )) AS [字段说明] FROM sys.tables AS t INNER JOIN s ...
- Winform 窗体单例
有窗体Form1和窗体Form2,单击Form1上按钮,只弹出一个Form2. Form2里自定义一个方法,里面判断是否弹出Form2,没有时弹出Form2. public static Form2 ...
- 宏碁台式机,如何设置u盘启动
1.按delete进入BIOS2.Authentication->Secure Boot状态改为Disabled;Boot Options->Launch CSM状态改为Always;Bo ...
- web初学之重定向与请求转发
重定向与请求转发的问题 (1)RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的,是属于请求对象的方法. (2 ...
- 刷CM7固件 乐padA1-07专用固件
-------------------------------------------------------------------------------- 前几天在版 ...
- Servlet学习五——流的分发
在上一节中有提到,流的传输,可以考虑Stream,但如果需要同时分发流和其它信息,,就需要再考虑其它方式了. 在coding中,服务端查询结果都是以gson进行传输,当需要传输一个语音并且同时需要传输 ...
- 笔记9-徐 DBCC SHRINKFILE不起作用的原因
1 , , , , , , , , ,40) ,1 page_id pg_alloc ext_size obj_id index_id partition_number partition_id ia ...
- Unity3D 物体移动方式
1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...
- 提高你css技能的css开发技巧(转载)
一.resize实现图片对比 resize的语法如下: resize:none | both | horizontal | vertical 案例效果如下图 (鼠标移到左下角白色区域,往右侧拖动,实现 ...