第六章 系统数据文件和信息
口令文件
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;
}
分享到:
相关推荐
将libapue.a从编译好的APUE源码库中提取出来,与自己写的c源文件放在同一目录下,并有写好的Makefile文件,在任何Linux系统下都能工作
APUE第一版源码组织文件 sourcefiles APUE
apue
UNIX环境高级编程第三版源码,Linux编程经典。。。。。。
UNIX环境高级编程配套头文件与error.c文件
将apue.h和error.c两个文件copy到该目录下。(apue.h位于 your_apue_path/inlcude ; error.c位于your_apue_path/lib ) 以我的了路径为例: #cp /home/ucfree/apue.2e/inlcude/apue.h . #cp /home/ucfree/apue.2e/...
apue部分习题答案,有需要的同学自行下载
本压缩包包含《Unix环境高级编程》中的所有例程和书中程序中用到的apue.h文件(在压缩包中include文件夹中)。
UNIX网络编程 APUE.CHM 经典的文件
本章开始讨论UNIX系统,先说明可用的文件I/O函数——打开文件、读文件、写文件等等。 大多数UNIX文件I/O只需用到5个函数: open read write lseek close 然后说明不同缓存器长度对read和write函数的影响。本章所...
Unix环境高级编程apue.h文件及使用时遇到的问题的解决方案
典型的服务有执行新程序、打开文件、读文件、分配存储区、获得当前时间等等,本书集中阐述了 U N I X操作系统各种版本所提供的服务。以严格的步进方式、不超前引用尚未说明过的术语的方式来说明 U N I X几乎是不可能...
本书[APUE中文UNIXpdf完整版]全面介绍了UNIX系统的程序设计界面—系统调用界面和标准C库提供的许多函数。 本书的前15章着重于理论知识的阐述,主要内容包括UNIX文件和目录、进程环境、进 程控制、进程间通信以及...
通过实用案例讲解apue的使用,涉及文件操作,socket操作,信号,进程间通讯
apue代码包
Apue unix高级编程的源码
APUE3+代码+APUE2(中文).经典中的经典。我就不做过多介绍了
apue的中文版,内容清晰,LINUX编程不可不看的好书!
压缩包里面有两个东西 1.是如何配置好apue.h该同文件及使用。 2.附上本人修改后已经能使用的apue.h源代码src.tar.bz2
APUE的源码,绝对经典。