背景:
用c语言来处理系统数据文件的系统调用.适合于Unix及Linux等.
正文:
1.口令文件
#include <sys/types.h> #include <pwd.h>
struct passwd *getpwuid(uid_t uid); struct passwd *getpwnam(const char *name); //ok:指针 no:NULL
//遍历整个passwd文件
struct passwd *getppwent(void); //ok:指针,指向下一项目 no: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_gecos;
char *pw_dir;
char *pw_shell;
};
例如:
#include "ourhdr.h" #include <sys/types.h> #include <pwd.h>
int main(void){ struct passwd *pw; pw=getpwnam("jack"); puts(pw->pw_name); puts(pw->pw_passwd); puts(pw->pw_dir); printf("uid:%d/n", pw->pw_uid); printf("gid:%d/n", pw->pw_gid); pw=getpwuid ; puts(pw->pw_name); puts(pw->pw_passwd); puts(pw->pw_dir); printf("uid:%d/n", pw->pw_uid); printf("gid:%d/n", pw->pw_gid); return 0; }
|
getpwnam函数:
#include <sys/types.h> #include <pwd.h> #include <stddef.h> #include <string.h>
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/passwd <pwd.h>
/etc/group <grp.h> >>>与pwd相似
/etc/hosts <netdb.h>
/etc/networks <netdb.h>
/etc/protocols <netdb.h>
/etc/services <netdb.h>
3.组文件group
3.1组结构
struct group{
char *gr_name;
char *gr_passwd;
int gr_gid;
char **gr_mem; >>>指向各用户名指针的数组
};
3.2相关函数
#include <sys/types.h> #include <grp.h>
struct group *getgrgid(gid_t gid); struct group *getgrnam(const char *name);
struct group *getgrent(void);
void setgrent(void);
void endgrent(void); //类似pwd相应函数
|
3.3添加组ID
#include <sys/types.h> #include <unistd.h>
int getgroups(int gidsetsize, gid_t grouplist[]); int setgroups(int groups, const gid_t grouplist[]); //ok:组ID数 no:-1
int initgroups(const char *usrname, gid_t basegid); //ok:0 no:-1
|
4.登录会计
用户登录时要写一个结构,以记录登录上来的用户的信息以供who等程序使用
struct utmp{ char ut_line[8]; char ut_name[8]; long ut_time; };
|
用户登录时login程序添写这个结构并将之写入utmp文件,之后再将之追回到wtmp(由last使用)文件中,路径可能是:/var/run/utmp和/var/log/wtmp均为二进制文件
5.系统标识
5.1uname函数
#include <sys/utsname.h>
int uname(struct utsname *name); //提供给uname程序输出
|
struct utsname{
char sysname[9]; >>>操作系统名字
char nodename[9]; >>>结点名字
char release[9]; >>>操作系统发行版
char version[9]; >>>发行版本
char machine[9]; >>>硬件类型名称
};
5.2gethostname函数
#include <unistd.h>
int gethostname(char *name, int namelen); //ok:0 no:-1
int sethostname(const char *newname);
|
6.时间和日期
Unix内核提供的基本时间是国际标准时间公元1970.1.1 00:00:00以来经过的秒数,用time_t表示,称为日历时间.
特点:
a:国际标准时间而非酵时间;
b:可自动进行转换;
c:将时间和日期作一个量值保存.time函数返回当前时间和日期.gettimeofday和settimeofday提供了微秒级的精度
href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List" />
#
include
<
time
.
h
>
time_t
time
(
time_t
*
ptr
); //ok:
时间值,ptr与返回值一致 no:-1
|
将返回值格式化成可读的格式: localtime mktime ctime strftime //均受到TZ时区的影响,gmtime同localtime但转换成的是国际标准时间
#
include
<
time
.
h
>
struct
tm
*
gmtime
(
const
time_t
*
ptr
);
struct
tm
*
localtime
(
const
time_t
*
ptr
); //将秒数转成可读的格式
time_t mktime(struct tm *ptr); //与localtime相反,将可读格式转成秒数 ok:时间 no:-1
char *ctime(const time_t *ptr);
char *asctime(const struct tm *ptr); //生成形式为26字节字符串,与date输出类似
size_t strftime(char *buf, size_t maxsize, const char *format, const struct tm *ptr); //ok:
存入数组的字符数不包含null no:0
|
tm结构:
struct tm{
int tm_sec; //[0, 61]可以有润秒
int tm_min; //[0,59]
int tm_hout; //[0,23]
int tm_mday; //[1,31]
int tm_mon; //[0,11]
int tm_year; //从1900到现在的年数
int tm_wday; //[0,6]
int tm_yday; //[0,365]
int tm_isdst; //daylight saving time flag:<0, 0, >0;夏时制为正
};
strftime中format说明:
该函数很像sprintf,目的是将后面的ptr结构体以format制定的格式输出到长度为maxsize的buf中,具体的格式化变量如下:
示例:
time_t t; time(&t); struct tm *lt; lt=localtime(&t); char timeBuf[100]; strftime(timeBuf, 100, "DT:%H:%M:%S=%X", lt); puts(timeBuf);
|
|