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

#include <sys/types.h>
#include <pwd.h> struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
两个函数返回值:成功返回指针,出错返回NULL
uid:需要获取信息的uid号

getpwuid例程

 #include <pwd.h>
#include <sys/types.h>
#include <stdio.h> int main()
{
uid_t my_uid; struct passwd *my_info;
my_info = getpwuid(getuid()); printf("my name = [%s]\n", my_info->pw_name);
printf("my passwd = [%s]\n", my_info->pw_passwd);
printf("my uid = [%d]\n", my_info->pw_uid);
printf("my gid = [%d]\n", my_info->pw_gid);
printf("my gecos = [%s]\n", my_info->pw_gecos);
printf("my dir = [%s]\n", my_info->pw_dir);
printf("my shell = [%s]\n", my_info->pw_shell); return ;
}

getpwuid

getpwnam例程

 #include <pwd.h>
#include <sys/types.h>
#include <stdio.h> int main()
{
char *username = "pi";
struct passwd *my_info;
my_info = getpwnam(username);
if(!my_info)
{
perror("getpwnam error");
exit();
} printf("my name = [%s]\n", my_info->pw_name);
printf("my passwd = [%s]\n", my_info->pw_passwd);
printf("my uid = [%d]\n", my_info->pw_uid);
printf("my gid = [%d]\n", my_info->pw_gid);
printf("my gecos = [%s]\n", my_info->pw_gecos);
printf("my dir = [%s]\n", my_info->pw_dir);
printf("my shell = [%s]\n", my_info->pw_shell); return ;
}

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数量,出错-
#include <grp.h>
int setgroups(size_t size, const gid_t *list);
两个函数的返回值:成功0,出错-

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

#include <sys/utsname.h>

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

返回主机名

#include <unistd.h>

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

时间部分

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

#include <time.h>

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

把时间表示为秒和纳秒

#include <time.h>

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

clock_getres函数把参数

#include <time.h>

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

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

#include <time.h>

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

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);
返回值:成功返回日历时间,出错-

类似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

apue 第6章 系统数据文件和信息的更多相关文章

  1. apue学习笔记(第六章 系统数据文件和信息)

    UNIX系统的正常运作需要使用大量与系统有关的数据文件,例如,口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件. 口令文件 UNIX系统口令文件包含如下字 ...

  2. UNIX环境高级编程 第6章 系统数据文件和信息

    UNIX系统的正常运作需要用到大量与系统有关的数据文件,例如系统用户账号.用户密码.用户组等文件.出于历史原因,这些数据文件都是ASCII文本文件,并且使用标准I/O库函数来读取. 口令文件 /etc ...

  3. UNIX系统高级编程——第六章-系统数据文件和信息-总结

    口令文件: /* The passwd structure. */ struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* ...

  4. APUE学习笔记——6 系统数据文件与信息

    1.用户口令:/etc/passwd文件 该文件中包含下列结构体信息.其中,当下主修熊passwd不再这里显示,是使用了一个占位符. struct passwd { char * pw_name; / ...

  5. (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  6. [APUE]系统数据文件与信息

    一.口令文件 UNIX口令文件包含下表中的各个字段,这些字段包含在 由于历史原因,口令文件是/bin/passwd,而且是一个文本文件,每一行都包括了上表中的七个字段,字段之间用":&quo ...

  7. 《UNIX环境高级编程》读书笔记之系统数据文件和信息(1)

    1.UNIX系统口令文件包括了下图所看到的的各字段,这些字段包括在<pwd.h>中定义的passwd结构体中 POSIX定义了两个获取口令文件项的函数. 在给出用户登录名或用户ID后.这两 ...

  8. [06]APUE:系统数据文件和信息

    [a] getpwent / setpwent / endpwent #include <pwd.h> struct passwd *getpwent(void) //成功返回指针,出错或 ...

  9. linux c编程:系统数据文件和信息

    linux系统相关的文件信息包含在/etc/passwd文件和/etc/group中.每次登录linux系统以及每次执行ls -l命令时都要使用口令文件.这些字段都包含在<pwd.h>中定 ...

随机推荐

  1. FMXUI ANDROID下连续按多次返回出现异常

    在ANDROID下,按返回键后,默认是关闭当前Frame,但连接按返回键,会对当前Frame执行多次关闭动作,​​因为已经释放过对象,再次关闭会出现异常错误,​解决办法:定义一个标识如FClosed: ...

  2. hdu1166:敌兵布阵(树状数组或线段树)

    题目描述: 一堆废话不用看...... 输入: 第一行一个整数T,表示有T组数据.每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表 ...

  3. HDU 6055 Regular polygon —— 2017 Multi-University Training 2

    Regular polygon Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. 分享几套bootstrap后台模板【TP5版】

    分享几套bootstrap后台模板[TP5版],模板来源于网络,需要的拿走.1.AdminLTE 链接: http://pan.baidu.com/s/1o7BXeCM 密码: zfhy 2.Boot ...

  5. 重写ArcGIS的TiledMapServiceLayer调用天地图瓦片

    require(["esri/layers/TiledMapServiceLayer"], function () { dojo.declare("com.StrongI ...

  6. 高并发大流量专题---3、前端优化(减少HTTP请求次数)

    高并发大流量专题---3.前端优化(减少HTTP请求次数) 一.总结 一句话总结: 图片地图:使用<map><area></area></map>标签. ...

  7. Docker容器数据卷-Volume详解

    Docker中的数据可以存储在类似于虚拟机磁盘的介质中,在Docker中称为数据卷(Data Volume).数据卷可以用来存储Docker应用的数据,也可以用来在Docker容器间进行数据共享.数据 ...

  8. C++ allocator类学习理解

    前言 在学习STL中containers会发现C++ STL里定义了很多的容器(containers),每一个容器的第二个模板参数都是allocator类型,而且默认参数都是allocator.但是a ...

  9. 104、Tensorflow 的变量重用

    import tensorflow as tf # 在不同的变量域中调用conv_relu,并且声明我们想创建新的变量 def my_image_filter(input_images): with ...

  10. git私有仓库提交代码

    #首次提交 #克隆版本库到本地 git clone http://192.168.3.107:9002/develop/zhong.git cd zhong #创建忽略文件(忽略文件自行编辑) tou ...