您的位置:首页 > 其它

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

2016-07-01 16:48 405 查看
1. 口令文件

  获取口令文件项的函数:

  struct passwd *getwuid(uid_t uid); //获取用户id

  struct passwd *getpwnam(const char *name);  //获取用户的登录名

  获取的不仅仅是登录名和用户ID

  struct passwd *getpwent(void); //获取一个记录项

  void setpwent(void);  //反绕它所使用的文件,使它定位到文件开始处

  void endpwent(void); //关闭这些文件

2. 组文件

  struct group *getgrid(gid_t gid); 

  struct group *getgrnam(const char* name);

 

  如果要搜索整个组文件

  struct group *getgrent(void);

  void setgrent(void);

  void endgrent(void);

3. 附属组ID

  int getgroups(int gidsetsize, gid_t grouplist[]); //将进程所属用户的各附属组ID填写到grouplist中

  int setgroups(int ngroups, const gid_t grouplist[]); //为调用进程设置附属组ID表

  int initgroups(const char *username, gid_t basegid); //basegid是username在口令文件中的组ID

4. 其他数据文件

  对这些文件都有跟组文件处理类似的函数,get函数,set函数,end函数。

5. 登陆账户记录

  utmp文件记录当前登录到系统的各个用户;wtmp文件跟踪各个登陆和注销时间。

6. 系统标识

  int unmae(struct utsname *name); //返回与主机和操作系统有关的信息,有sysname,nodename,release(操作系统的),version,machine。

  int gethostname(char *name, int namelen); //得到主机名,就是TCP/IP网络上主机的名字。

7. 时间和日期

  time_t time(time_t *calptr); //返回当前时间和日期

  int clock_gettime(clockid_t clock_id, struct timespec *tsp);  //clock_id是时钟类型标识符

  int clock_getres(clockid_t clock_id, struct timespec *tsp);

  int clock_settime(clockid_t clock_id, const struct timespec *tsp); //特定时钟设置时间

  struct tm *gmtime(const time_t *calptr); //和下面的函数一样,返回tm结构的指针,出错返回NULL

  struct tm *localtime(const time_t *calptr); 

  time_t mktime(struct tm *tmptr); //以本地时间的年月日作为参数,将其变换成time_t值

  size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr);

  size_t strftime_l(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr, locale_t locale);   

  char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tmptr); //这个函数与上两个相反,这个函数是字符串转化成时间输出,上两个是把时间转换成字符串进行输出#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
time_t t;
struct tm *tmp;
char buf1[16], buf2[64];

time(&t);
tmp = localtime(&t);
if(strftime(buf1, 16, "time and date: %r, %a %b %d, %Y", tmp) == 0)
printf("buffer length 16 is too small\n");
else
printf("%s\n", buf1);
if(strftime(buf2, 64, "time and date: %r, %a %b %d, %Y", tmp) == 0)
printf("buffer length 200 is too small\n");
else
printf("%s\n", buf2);
exit(0);
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: