APUE函数笔记四: 系统数据文件和信息

原创 2012年03月24日 15:00:02

第六章  系统数据文件和信息:

#include <pwd.h>
struct passwd * getpwuid(uid_t uid);
    ret is a static valuable
struct passwd * getpwnam(const char * name);
    ret is a static valuable
struct passwd * getpwent(void);
    ret is NULL, means EOF or error, and it is a static valuable
void setpwent(void);
void endpwent(void);
    struct passwd {
        char * pw_name;
        char * pw_passwd;
        ...
    };

#include <shadow.h>
struct spwd * getspnam(const char * name);
    ret is a static valuable
struct spwd * getspent(void);
    ret is NULL, means EOF or error, and it is a static valuable
void setspent(void);
void endspent(void);
    struct spwd {
        char * sp_namp;
        char * sp_pwdp;
        ...
    };

#include <grp.h>
struct group * getgrgid(gid_t gid);
    ret is a static valuable
struct group * getgrnam(const char * name);
    ret is a static valuable
struct group * getgrent(void);
    ret is NULL meams EOF or error, and it is a static valuable
void setgrent(void);
void endgrent(void);
    struct group {
        char *  gr_name;
        char *  gr_passwd;
        int     gr_gid;
        char ** gr_mem;
    };

#include <unistd.h>
int getgroups(int gidsetsize, gid_t grouplist[]);
    ret is 0 means error,
    if gidsetsize is 0, will return the group-total-size
    else copy groups to grouplist, and return copy-counts

#include <grp.h>        /* Linux */
#include <unistd.h>     /* FreeBSD, Mac OS X, Solaris */
int setgroups(int ngroups, const gid_t grouplist[]);
    ret is 0 means success and -1 means error
    ngroups must <= NGROUPS_MAX
    only superuser can call this function

#include <grp.h>        /* Linux, Solaris */
#include <unistd.h>     /* FreeBSD, Mac OS X */
int initgroups(const char * username, gid_t basegid);
    ret is 0 means success and -1 means error
    it call setgroups inside
    only superuser can call this function

#include <sys/utsname.h>
int uname(struct utsname * name);
    ret is -1 means error
    struct utsname {
        char sysname[];
        char nodename[];
        char release[];
        char version[];
        char machine[];
    };

#include <unistd.h>
int gethostname(char * name, int namelen);
    ret is 0 means success and -1 means error
    HOST_NAME_MAX: 64-65(Linux), 256-257(Others)

#include <time.h>
time_t time(time_t * calptr);
    if calptr not NULL, will copy ret to calptr
    ret is -1 means error

#include <sys/time.h>
int gettimeofday(struct timeval * restrict tp, void * restrict tzp);
    we must pass NULL to tzp
    alway return 0
    struct timeval {
        time_t tv_sec   /* seconds */
        long   tv_usec; /* microseconds */
    };

#include <time.h>
struct tm * gmtime(const time_t * calptr);
    ret point to a static tm object
struct tm * localtime(const time_t * calptr);
    ret point to a static tm object
    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; /* since 1970 */
        int tm_wday; /* [0-6] */
        int tm_yday; /* [0-365] */
        int tm_isdst; /* <0, 0, >0 */
    };

#include <time.h>
time_t mktime(struct tm * tmptr);
    ret is -1 means error

#include <time.h>
char * asctime(const struct tm * tmptr);
    ret point to a static block
char * ctime(const time_t * calptr);
    ret point to a static block

#include <time.h>
size_t strftime(char * restrict buf, size_t maxsize, 
                const char * restrict format, 
                const struct tm * restrict tmptr);
    ret is conv-size, 0 show failed

示例:

#include <stdio.h>
#include <pwd.h>

struct passwd * 
my_getpwnam(const char * name)
{
    struct passwd * ptr;

    setpwent();
    while ((ptr = getpwent()) != NULL) {
        if (strcmp(ptr->pw_name, name) == 0) {
            break;
        }
    }
    endpwent();

    return ptr;
}

void 
test(const char * name, const char * funcname, 
     struct passwd * get(const char *))
{
    struct passwd * ptr;

    if ((ptr = get(name)) == NULL) {
        printf("%s failed\n", funcname);
    }
    else {
        printf("name: %s, password: %s\n", ptr->pw_name, 
               ptr->pw_passwd == NULL || ptr->pw_passwd[0] == '\0' 
               ? "(null)" : ptr->pw_passwd);
    }
}

int
main(void)
{
    const char * const name = "adwardink";

    test(name, "getpwnam", getpwnam);
    test(name, "my_getpwnam", my_getpwnam);

    return 0;    
}

#include <stdio.h>
#include <shadow.h>

struct spwd * 
my_getspnam(const char * name)
{
    struct spwd * ptr;

    setspent();
    while ((ptr = getspnam(name)) != NULL) {
        if (strcmp(ptr->sp_namp, name) == 0) {
            break;
        }
    }
    endspent();

    return ptr;
}

void 
test(const char * name, const char * funcname, 
     struct spwd * get(const char *))
{
    struct spwd * ptr;

    if ((ptr = get(name)) == NULL) {
        printf("%s failed\n", funcname);
    }
    else {
        printf("name: %s, password: %s\n", ptr->sp_namp, 
               ptr->sp_pwdp == NULL || ptr->sp_pwdp[0] == '\0' 
               ? "(null)" : ptr->sp_pwdp);
    }
}

int
main(void)
{
    const char * const name = "adwardink";

    test(name, "getspnam", getspnam);
    test(name, "my_getspnam", my_getspnam);

    return 0;
}

#include <stdio.h>
#include <grp.h>

struct group * 
my_getgrnam(const char * name)
{
    struct group * ptr;

    setgrent();
    while ((ptr = getgrent()) != NULL) {
        if (strcmp(ptr->gr_name, name) == 0) {
            break;
        }
    }
    endgrent();

    return ptr;
}

void 
test(const char * name, const char * funcname, 
     struct group * get(const char *))
{
    struct group * ptr;

    if ((ptr = get(name)) == NULL) {
        printf("%s failed\n", funcname);
    }
    else {
        printf("name: %s, password: %s\n", ptr->gr_name, 
               ptr->gr_passwd == NULL || ptr->gr_passwd[0] == '\0' 
               ? "(null)" : ptr->gr_passwd);
    }
}

void 
my_print(const char * format, const char * info)
{
    printf(format, info == NULL || info[0] == '\0' ? "(null)" : info);
}

void 
showgroup(void)
{
    struct group * ptr;
    char ** mem;

    setgrent();
    while ((ptr = getgrent()) != NULL) {
        my_print("group name: %s\n", ptr->gr_name);
        my_print("password: %s\n", ptr->gr_passwd);
    /*  my_print("group id: %d\n", ptr->gr_gid); */
        mem = ptr->gr_mem;
        while (mem != NULL && *mem != NULL) {
            my_print("user name: %s\n", *mem);
            ++mem;
        }
    }
    endgrent();
}

int
main(void)
{
    const char * const name = "adwardink";

    test(name, "getgrnam", getgrnam);
    test(name, "my_getgrnam", my_getgrnam);

    showgroup();

    return 0;    
}

#include <stdio.h>
#include <unistd.h>
#include <sys/utsname.h>

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX   (280)
#endif

int
main(void)
{
    struct utsname uts;
    char hostname[HOST_NAME_MAX];

    if (uname(&uts) == -1) {
        printf("uname error\n");
    }
    else {
        printf("sysname:  %s\n", uts.sysname);
        printf("nodename: %s\n", uts.nodename);
        printf("release:  %s\n", uts.release);
        printf("version:  %s\n", uts.version);
        printf("machine:  %s\n", uts.machine);
    }

    if (gethostname(hostname, HOST_NAME_MAX) == -1) {
        printf("gethostname error\n");
    }
    else {
        printf("hostname: %s\n", hostname);
    }

    return 0;
};

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

void 
gettime(void)
{
    struct timeval tv;
    struct tm * ptm;

    gettimeofday(&tv, NULL);
    if ((ptm = localtime(&tv.tv_sec)) == NULL) {
        printf("localtime error\n");
        return;
    }
    
    printf("microtime: %02d:%02d:%02d:%ld\n", ptm->tm_hour, ptm->tm_min,
                                              ptm->tm_sec, tv.tv_usec);
}

void 
testtimefuncs(void)
{
    typedef struct tm * (*Fgettm)(const time_t *);
    Fgettm fgtm[2] = { gmtime, localtime };
    const char * funcname[2] = { "gmtime", "localtime" };

    time_t pt;
    struct tm * ptm;
    char * ptr;
    int i;

    if (time(&pt) == -1) {
        printf("time error\n");
    }
    else {
        printf("time: %ld\n", pt);
    }

    if ((ptr = ctime(&pt)) == NULL) {
        printf("ctime error\n");
    }
    else {
        printf("ctime: %s", ptr);
    }

    for (i = 0; i < sizeof(funcname) / sizeof(funcname[0]); ++i) {
        if ((ptm = fgtm[i](&pt)) == NULL) {
            printf("%s error\n", funcname[i]);
        }
        else {
            printf("%s ok\n", funcname[i]);
            if ((ptr = asctime(ptm)) == NULL) {
                printf("asctime error\n");
            }
            else {
                printf("asctime: %s", ptr);
            }

            if (mktime(ptm) == pt) {
                printf("mktime(%s(pt)) == pt\n", funcname[i]);
            }
            else {
                printf("mktime(%s(pt)) != pt\n", funcname[i]);
            }
        }
    }
}

int 
main(void)
{
    gettime();
    testtimefuncs();
    return 0;
}

#include <stdio.h>
#include <time.h>

int
main(void)
{
    time_t caltime;
    struct tm * ptm;
    const int MAXLINE = 64;
    char line[MAXLINE];

    if ((caltime = time(NULL)) == -1) {
        printf("time error\n");
    }

    if ((ptm = localtime(&caltime)) == NULL) {
        printf("localtime error\n");
    }

    if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", ptm) == 0) {
        printf("strftime error\n");
    }

    fputs(line, stdout);

    return 0;
}

#include <stdio.h>
#include <time.h>

int
main(void)
{
    time_t caltime;
    struct tm * ptm1;
    struct tm * ptm2;
    const int MAXLINE = 80;
    char line[MAXLINE];

    if ((caltime = time(NULL)) == -1) {
        printf("time error\n");
    }

    if ((ptm1 = localtime(&caltime)) == NULL) {
        printf("localtime error\n");
    }

    if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", ptm1) == 0) {
        printf("strftime error\n");
    }

    fputs(line, stdout);
    fputs("sleep 2 minutes...\n", stdout);
    sleep(2 * 60);

    if ((caltime = time(NULL)) == -1) {
        printf("time error\n");
    }

    if ((ptm2 = localtime(&caltime)) == NULL) {
        printf("localtime error\n");
    }

    if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", ptm2) == 0) {
        printf("strftime error\n");
    }

    fputs(line, stdout);

    if (ptm1 == ptm2) {
        printf("static\n");
    }
    else {
        printf("non-static\n");
    }

    return 0;
}



相关文章推荐

《APUE》笔记-第六章-系统数据文件和信息

1.口令文件 口令文件,即Unix系统用户数据库,存储在/etc/passwd中,是一个ASCII文件,包含的字段信息在定义的passwd数据结构中。   pwd.h文件所在位置:/usr/i...

APUE笔记---第六章系统数据文件和信息操作

APUE笔记—第六章系统数据文件和信息操作1. 口令文件Linux系统口令文件包含以下字段:struct passwd { char *pw_name; /* username...
  • men_wen
  • men_wen
  • 2016年10月31日 22:21
  • 319

读书笔记-APUE第三版-(6)系统数据文件和信息

常见系统数据文件 下表列出了常见的系统数据文件及其查找函数。 以/etc/passwd文件为例,读取数据的程序框架如下: void get_pw_entry() { struct pa...
  • cargogo
  • cargogo
  • 2014年05月05日 20:25
  • 1259

apue:系统数据文件和信息

unix数据文件相关操作的函数与概念
  • Mr_Avin
  • Mr_Avin
  • 2017年02月06日 13:20
  • 263

[APUE]第六章 系统数据文件和信息

第六章 系统数据文件和信息 口令文件 struct passwd {   char * pw_name; /* Username, POSIX.1 */   char * ...

linux系统数据文件和信息--unix环境高级编程读书笔记

linux中对文件passwd,group,utmp,wtmp的操作,以及uname函数和时间操作的一些介绍,总的来说,是对linunx中的一些系统文件操作的相关介绍。...

UNIX环境高级编程学习笔记(七)系统数据文件和信息

系统数据文件和信息

《UNIX环境高级编程》六系统数据文件和信息读书笔记

UNIX系统的正常运作需要使用大量与系统有关的数据文件,例如,口令文件/etc/passwd和组文件/etc/group等。 1、口令文件 口令文件的各字段包含在pwd.h>中定义的passwd结...

UNIX环境高级编程学习之路(四)----系统数据文件和信息

对于UNIX环境编程,工作中经常会用到相关知识,作为学习UNIX环境编程的经典书籍--UNIX环境高级编程,是每个UNIX编程人员必看的经典书籍之一,为了将相关知识重新进行学习,以系统的整合所学知识,...

Linux系统数据文件和信息

以下内容全部摘自UNIX环境高级编程(第3版)口令文件UNIX系统口令文件(POSIX.1则将其称为用户数据库)包含下图中所示的各字段,这些字段包含在中定义的passwd结构中 POSIX.1定义了...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:APUE函数笔记四: 系统数据文件和信息
举报原因:
原因补充:

(最多只允许输入30个字)