zoukankan      html  css  js  c++  java
  • [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 特权进程可设置网络主机名称,不需要添加 字节

    [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(秒)
  • 相关阅读:
    FCKEditor使用说明
    如何查看索引或table所佔用的實際空間
    java字节输入流
    文件的读写操作
    利用JProfiler
    javascript面向对象技术基础(五)
    深刻理解Linux进程间通信(IPC)
    JAVA中操作数据库方式与设计模式的应用 2
    javascript面向对象技术基础(六)
    FCKeditor在线编辑器
  • 原文地址:https://www.cnblogs.com/hadex/p/6128431.html
Copyright © 2011-2022 走看看