apue 第6章 系统数据文件和信息

 

在给出用户登录名或数值用户ID后,这两个函数就能查看相关项。

#include <sys/types.h>
#include <pwd.h>

struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
两个函数返回值:成功返回指针,出错返回NULL
uid:需要获取信息的uid号

getpwuid例程

 1 #include <pwd.h>
 2 #include <sys/types.h>
 3 #include <stdio.h>
 4 
 5 int main()
 6 {
 7         uid_t my_uid;
 8 
 9         struct passwd *my_info;
10         my_info = getpwuid(getuid());
11 
12         printf("my name = [%s]\n", my_info->pw_name);
13         printf("my passwd = [%s]\n", my_info->pw_passwd);
14         printf("my uid = [%d]\n", my_info->pw_uid);
15         printf("my gid = [%d]\n", my_info->pw_gid);
16         printf("my gecos = [%s]\n", my_info->pw_gecos);
17         printf("my dir = [%s]\n", my_info->pw_dir);
18         printf("my shell = [%s]\n", my_info->pw_shell);
19 
20         return 0;
21 }
getpwuid

getpwnam例程

 1 #include <pwd.h>
 2 #include <sys/types.h>
 3 #include <stdio.h>
 4 
 5 int main()
 6 {
 7         char *username = "pi";
 8         struct passwd *my_info;
 9         my_info = getpwnam(username);
10         if(!my_info)
11         {
12                 perror("getpwnam error");
13                 exit(1);
14         }
15 
16         printf("my name = [%s]\n", my_info->pw_name);
17         printf("my passwd = [%s]\n", my_info->pw_passwd);
18         printf("my uid = [%d]\n", my_info->pw_uid);
19         printf("my gid = [%d]\n", my_info->pw_gid);
20         printf("my gecos = [%s]\n", my_info->pw_gecos);
21         printf("my dir = [%s]\n", my_info->pw_dir);
22         printf("my shell = [%s]\n", my_info->pw_shell);
23 
24         return 0;
25 }
getpwnam

 

也有些程序要查看整个口令文件。

#include <sys/types.h>
#include <pwd.h>

struct passwd *getpwent(void);
返回值:成功指针,出错或达文件尾端,返回NULL
void setpwent(void);
void endpwent(void);

  apue例程

getpwent

 

有另一组函数可用于访问阴影口令文件

#include <shadow.h>

struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
两个函数返回值:成功返回指针,出错NULL
void setspent(void);
vpid endspent(void);

 

 

查看组名或数值组ID

#include <sys/types.h>
#include <grp.h>

struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);
返回值:成功指针,出错NULL

 

针对口令文件的3个函数

#include <sys/types.h>
#include <grp.h>

struct group *getgrent(void);
返回值:成功指针,出错或达到文件尾端,返回NULL
void setgrent(void);
void endgrent(void);

 

获取和设置附属组ID

#include <sys/types.h>
#include <unistd.h>
int getgroups(int size, gid_t list[]);
返回值:成功返回附属组ID数量,出错-1
#include <grp.h>
int setgroups(size_t size, const gid_t *list);
两个函数的返回值:成功0,出错-1

 

返回与主机和操作系统有关的信息

#include <sys/utsname.h>

int uname(struct utsname *buf);
返回值:成功非负值,出错-1

 

返回主机名

#include <unistd.h>

int gethostname(char *name, int namelen);
返回值:成功0,出错-1

 

时间部分

time函数返回当前时间和日期

#include <time.h>

time_t time(time_t *t);
返回值:成功返回时间值,出错-1

 

把时间表示为秒和纳秒

#include <time.h>

int clock_gettime(clockid_t clk_id, struct timespec *tp);
返回值:成功0,出错-1

 

clock_getres函数把参数

#include <time.h>

int clock_getres(clockid_t clk_id, struct timespec *res);
返回值:成功0,出错-1

 

要对特定的时钟设置时间,可以调用clock_settime函数

#include <time.h>

int clock_settime(clockid_t clk_id, const struct timespec *tp);
返回值:成功0,出错-1

 

SUSv4指定gettimeofday已经弃用,然而一些程序仍然使用这个函数,因为与time相比,提供了更高的精度

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);
返回值:总是0

 

#include <time.h>

struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
返回值:指向分解的tm结构的指针,出错NULL

localtime和gmtime之间的区别是,localtime是转换成本地时间,gmtime是将时间结构分解成年月日时分秒周日。

 

以本地时间的年、月、日等作为参数,将其变换成time_t值

#include <time.h>

time_t mktime(struct tm *tm);
返回值:成功返回日历时间,出错-1

 

类似printf的时间值的函数,通过多个参数来定制产生的字符串

#include <time.h>

size_t strftime(char s, size_t max, const char *format, const struct tm *tm);
返回值:若有空间,返回存入数组的字符数,否则返回0

 

字符串时间转换成分解时间

#include <time.h>

char *strptime(const char *s, const char *format, struct tm *tm);
返回值:指向上次解析的字符的下一个字符的指针,否则返回NULL

 

posted @ 2017-12-30 11:08  习惯就好233  阅读(157)  评论(0编辑  收藏  举报