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

本文介绍了操作系统中用于管理用户认证、组管理和时间日期处理的相关API,包括获取用户及组信息的函数、附属组ID的操作以及时间和日期的获取与格式化等关键功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}


  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值