您的位置:首页 > 其它

6、系统数据文件和信息

2017-09-26 22:04 127 查看
对于每个数据文件至少包含3个函数:get(读下一个记录),set,end

1、根据口令文件(/etc/passwd)得到passwd结构(/usr/include/pwd.h)

struct passwd
{
char *pw_name;                /* Username.  */
char *pw_passwd;              /* Password.  */
__uid_t pw_uid;               /* User ID.  */
__gid_t pw_gid;               /* Group ID.  */
char *pw_gecos;               /* Real name.  */
char *pw_dir;                 /* Home directory.  */
char *pw_shell;               /* Shell program.  */
};


//根据uid或用户名得到结构
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);

//下面是三个函数被上面函数调用
//返回口令文件头部
void setpwent(void);
//返回口令文件的下一个记录项
struct passwd *getpwent(void);
//关闭文件
void endpwent(void);


struct passwd *getpwnam(const char *name)
{
struct passwd *ptr;
setpwent();
while((ptr = getpwent()) != NULL)
{
if(strcmp(name, ptr->pw_name) == 0)
break;
}
endpwent();

return(ptr);
}


2、阴影文件-加密文件的另一个存放文件,阴影文件对应的结构

/etc/shadow 和 /usr/include/shadow.h

struct spwd
{
char *sp_namp;              /* Login name.  */
char *sp_pwdp;              /* Encrypted password.  */
long int sp_lstchg;         /* Date of last change.  */
long int sp_min;            /* Minimum number of days between changes.  */
long int sp_max;            /* Maximum number of days between changes.  */
long int sp_warn;           /* Number of days to warn user to change
the password.  */
long int sp_inact;          /* Number of days the account may be
inactive.  */
long int sp_expire;         /* Number of days since 1970-01-01 until
account expires.  */
unsigned long int sp_flag;  /* Reserved.  */
};


struct spwd *getspanam(const char *name);
struct spwd *getspent(void);

void setspent(void);
void endspend(void);


3、组文件和结构,类似上面

/etc/group 和 /usr/include/grp.h

struct group
{
char *gr_name;              /* Group name.  */
char *gr_passwd;            /* Password.    */
__gid_t gr_gid;             /* Group ID.    */
char **gr_mem;              /* Member list. */
};


struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);

struct group *getgrent(void);
void setgrent(void);
void endgrent(void);


4、附属组,一个用户可能属于多个组,当前组和一些附属组

//把进程所属用户的各附属ID写到参数grouplist[]中
int getgroups(int gidsetsize, git_t grouplist[]);
//为进程所属用户设置附属ID
int setgroups(int ngroups, const git_t grouplist[]);


5、登录信息记录,utmp文件记录当前登录到系统的各个用户(var/run/utmp);wtmp文件跟踪各个登录和注销事件(var/log/wtmp)。有结构与文件对应

6、系统标识

int uname(struct utsname *name);


7、时间和日期

协调世界时:公元1970年1月1日00:00:00这一时间以来经过的秒数。

//返回当前时间和日期(从实时系统时钟获取),也存到calptr指向的地址
time_t time(time_t *calptr);

//获取特定时钟时间,clock_id可以是
CLOCK_REALTIME      实时系统时间
CLOCK_MONOTONIC、    不带负跳数的实时系统时间
CLOCK_PROCESS_CPUTIME_ID、调用进程的CPU时间
CLOCK_THREAD_CPUTIME_UP 调用线程的CPU时间
//struct timespec 时间转化为秒和纳秒存放,该函数比上个函数得到时间精度高
int clock_gettime(clockid_t clock_id, struct timespec *tsp);

//根据不同时钟,转换时间为不同精度
int clock_getres(clockid_t clock_id, struct timespec *tsp);
//设置特定时钟的时间
int clock_settime(clockid_t clock_id, const struct timespec *tsp);


struct timespec
{
__time_t tv_sec;            /* Seconds.  */
long int tv_nsec;           /* Nanoseconds.  */
};

struct tm
{
int tm_sec;                   /* Seconds.     [0-60] (1 leap second) */
int tm_min;                   /* Minutes.     [0-59] */
int tm_hour;                  /* Hours.       [0-23] */
int tm_mday;                  /* Day.         [1-31] */
int tm_mon;                   /* Month.       [0-11] */
int tm_year;                  /* Year - 1900.  */
int tm_wday;                  /* Day of week. [0-6] */
int tm_yday;                  /* Days in year.[0-365] */
int tm_isdst;                 /* DST.         [-1/0/1]*/

#ifdef  __USE_BSD
long int tm_gmtoff;           /* Seconds east of UTC.  */
__const char *tm_zone;        /* Timezone abbreviation.  */
#else
long int __tm_gmtoff;         /* Seconds east of UTC.  */
__const char *__tm_zone;      /* Timezone abbreviation.  */
#endif
};


//时间转换,分解时间-日历时间
struct tm *gmtime(const time_t *calptr);
struct tm *localtime(const time_t *calptr);

time_t mktime(struct tm *tmptr);和gmtime相反


时间格式定制

//tmptr待处理对象,format格式控制,buf输出到buf,maxsize buf长度
size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr);

//和上面相反
char *strptime(char *restrict buf,  const char *restrict format, const struct tm *restrict tmptr);


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t t;
struct tm *tmp;
char buf[64];

time(&t);
tmp = localtime(&t);

strftime(buf, 64, "time and date: %r, %d %b %d, %Y ", tmp);
printf("%s\n",buf);
exit(0);
}
~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息