第六章 系统数据文件和信息
口令文件
struct passwd
{
char * pw_name; /* Username, POSIX.1 */
char * pw_passwd; /* Password */
__uid_t pw_uid; /* User ID, POSIX.1 */
__gid_t pw_gid; /* Group ID, POSIX.1 */
char * pw_gecos; /* Real Name or Comment field */
char * pw_dir; /* Home directory, POSIX.1 */
char * pw_shell; /* Shell Program, POSIX.1 */
char *pw_class;
time_t pw_change;
time_t pw_expire;
};
struct passwd* getpwuid(uid_t uid)//通过用户ID获得相应的passwd结构
struct passwd* getpwnam(const char* name)//通过用户名称获得相应的passwd结构
void setpwent()//初始化,把全局变量指针赋值到开始值[l1]
struct passwd* getpwent()//获得一个passwd结构,如果遍历完返回指针式空
void endpwent()//释放内部结构,并且关闭文件
阴影文件(密码)
struct spwd
{
char *sp_namp; /* login name */
char *sp_pwdp; /* encrypted password */
sptime sp_lstchg; /* date of last change */
sptime sp_min; /* minimum number of daysbetween changes */
sptime sp_max; /* maximum number of days betweenchanges */
sptime sp_warn; /* number of days of warningbefore password
expires */
sptime sp_inact; /* number of days after passwordexpires
until the account becomesunusable. */
sptime sp_expire; /* days since 1/1/70 until accountexpires*/
unsigned long sp_flag; /* reserved for future use */
};
struct spwd* getspnam(const char*name)//通过用户名获得相应的口令信息
void setspent();
struct spwd* getspent()
void endspent[l2] ()
组文件
在grp.h的头文件中可以看到组的结构
/* The group structure. */
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)
void setgrent()
strcut group* getgrent()
void endgrent()
附加组ID
就是原来一个用户仅仅有一个组ID,之后觉得不够用,就把组ID进行扩展,加入了附加组ID
int getgroups(int gidsetsize, gid_t grouplist[])//数组groulist保存组ID,gidsetsize 数组元素个数(最大16)
int setgroups(int ngroups, const gid_tgrouplist[])设置附加组ID
系统标识:
int uname(strcut utsname* name)
int gethostname(char*name, int namelen );
时间和日期
time_t time(time_t *calptr)//返回值和参数都是相对于1970的秒数
int gettimeofday(strcut timeval * tp, void*tzp)//或者比time更精准的时间
struct timeval{
time_ttv_sec;//秒
longtv_usec;//微妙
}
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of themonth */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of theweek */
int tm_yday; /* day in theyear */
int tm_isdst; /* daylightsaving time */
};
struct tm* gmtime(const time_t *calptr)//转换成国际标准时间
struct tm* localtime(const time_t calptr).转换成当地时间
time_t mktime(struct tm* tmptr)把tm转化成time_t结构
const* astime(const struct tm* tmptr)转化成年月日的字符串时间,用户输出
const*ctime(const time_t * calptr)把time_t 转化
[l1]
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <pwd.h>
using namespace std;
int main(int argc, char** argv)
{
setpwent();
structpasswd* pw;
while((pw=getpwent()))
{
cout<<"name= "<<pw->pw_name<<endl;
}
endpwent();
cout<<"filehas finished"<<endl;
return0;
}
[l2]#include <stdlib.h>
#include <iostream>
#include <shadow.h>
using namespace std;
int main(int argc, char** argv)
{
setspent();
structspwd* sp;
inti = 0;
while((sp = getspent()))
{
cout<<"name"<<i<<"= "<<sp->sp_namp<<endl;
cout<<"passwd"<<i<<"= "<<sp->sp_pwdp<<endl;
}
endspent();
return0;
}