[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>中定 ...
随机推荐
- JVM基本结构
以下是JVM的一个基本架构图,在这个基本架构图中,栈有两部份,Java线程栈以及本地方法栈,栈的概念与C/C++程序基本上都是一个概念,里面存放的都是栈帧,一个栈帧代表的就是一个函数的调用,在栈帧里面 ...
- linux 命令总结
①用find命令查找并删除文件 用脚本创建测试数据: [root@greymouster ceshidata]# for n in `seq 10`> do > date -s " ...
- Ubuntu 14.10安装SecureCRT 7.3(转)
原文 :http://blog.csdn.net/chszs/article/details/40623169 1.软件准备 Ubuntu14.04 x64 下载SecureCRT7.3的版本:scr ...
- [小哥Allegro72讲速成视频]
http://v.qq.com/vplus/df932a993679cf80a0b6c87bb849e22c 第01讲 Allegro常用组件介绍 视频链接:http://v.qq.com/boke/ ...
- springmvc 拦截器,不拦截jsp文件
spring mvc的拦截器只拦截controller不拦截jsp文件,如果不拦截jsp文件也会给系统带安全性问题. 解决方案有两种: 1.将所有的jsp文件放入到WEB-INF文件夹下,这样用户是直 ...
- MediaBrowserService 音乐播放项目
MediaBrowserService 音乐播放项目,本项目主要有如下功能: (1):支持播放在线音乐 (2):按住home键退出页面后显示通知栏部分播放提示, (3) : 支持切换上下首歌曲 本项 ...
- python之路-Day10
操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者消费者模型 ...
- 一个页面中显示多个button时总行数计算公式。
总行数 = (按钮总数 + 每一行按钮数 - 1) / 每一行按钮数. 同理.假设我们要显示一定总数的item.每页固定数量,则总页数为. 总页数 = (总显示数量 + 每页显示的数量 - 1) / ...
- 双二次Lagrange 有限元计算特征值程序(基于iFEM)
function lambda = c0P2(h) %% Mesh [node,elem] = squarequadmesh([,,,],h); elem = elem(:,[,,,]); showm ...
- String easy 结束日
(1)Compare Version Numbers 解题思路:把字符串分割成字符串数组,然后取两个字符串数组的最大长度,从数组的第一个元素开始比较,注意把String型转换成Int型(Integer ...