[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>中定 ...
随机推荐
- nslookup命令
nslookup命令可以从本地DNS服务器中查看所有的IP地址和域名信息(它就像一本互联网电话簿).例如,想要找到www.baidu.com的IP地址就可以使用nslookup命令. nslookup ...
- centos 7.2 网卡配置文件 及 linux bridge的静态配置
在 centos 7.2 系统内, 网卡的配置文件在: /etc/sysconfig/network-scripts/ 下. 命名规则: ifcfg-xxxx. xxx为设备名称. 通过分析 ne ...
- OpenCV Template Matching Subpixel Accuracy
OpenCV has function matchTemplate to easily do the template matching. But its accuracy can only reac ...
- SqlLocalDB使用笔记
一.介绍 SqlLocalDB是VS安装时附带的数据库软件,相当于精简版的SQL Express. 二.使用 VS版本为2015,默认安装位置为:C:\Program Files\Microsoft ...
- html5 01
(1)Html 5的概念 能做什么? 为什么学习? HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web应用开发的一系列标准,成为第一个将Web做为应用开发平台的HTML ...
- 使用scala开发spark入门总结
使用scala开发spark入门总结 一.spark简单介绍 关于spark的介绍网上有很多,可以自行百度和google,这里只做简单介绍.推荐简单介绍连接:http://blog.jobbole.c ...
- 关于Jquery的delegate绑定事件无效
今天在做一个页面,用的是easyui页面有很多的tabs,里面都放了iframe 需要在load事件动态调整iframe高度 发现始终无法使用delegate来绑定load事件. 纠结了一下午发现了问 ...
- mac系统,git上刚刚checkout出来的文件,一检查,发现已经被修改过了,怎么破???
如下图中所示: 事实上,checkout之后什么都还没做,这些文件为何就被修改? 检查一下别的电脑上所存放的同一套源码,原来出问题的文件都是同名文件,只不过是有大小写区分而已!!! linux系统可以 ...
- QT征程之初识qt
下载 https://www.qt.io/cn/download-open-source/ 下载QT离线安装包 Qt 5.5.1 for Linux 32-bit (546 MB) (info ...
- let it be
回家路上听到电台里主持人在介绍这首歌,听得我两眼模糊,真的太应最近的心情了. let it be.