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例程
#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例程
有另一组函数可用于访问阴影口令文件
#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章 系统数据文件和信息的更多相关文章
- apue学习笔记(第六章 系统数据文件和信息)
UNIX系统的正常运作需要使用大量与系统有关的数据文件,例如,口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件. 口令文件 UNIX系统口令文件包含如下字 ...
- UNIX环境高级编程 第6章 系统数据文件和信息
UNIX系统的正常运作需要用到大量与系统有关的数据文件,例如系统用户账号.用户密码.用户组等文件.出于历史原因,这些数据文件都是ASCII文本文件,并且使用标准I/O库函数来读取. 口令文件 /etc ...
- UNIX系统高级编程——第六章-系统数据文件和信息-总结
口令文件: /* The passwd structure. */ struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* ...
- APUE学习笔记——6 系统数据文件与信息
1.用户口令:/etc/passwd文件 该文件中包含下列结构体信息.其中,当下主修熊passwd不再这里显示,是使用了一个占位符. struct passwd { char * pw_name; / ...
- (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- [APUE]系统数据文件与信息
一.口令文件 UNIX口令文件包含下表中的各个字段,这些字段包含在 由于历史原因,口令文件是/bin/passwd,而且是一个文本文件,每一行都包括了上表中的七个字段,字段之间用":&quo ...
- 《UNIX环境高级编程》读书笔记之系统数据文件和信息(1)
1.UNIX系统口令文件包括了下图所看到的的各字段,这些字段包括在<pwd.h>中定义的passwd结构体中 POSIX定义了两个获取口令文件项的函数. 在给出用户登录名或用户ID后.这两 ...
- [06]APUE:系统数据文件和信息
[a] getpwent / setpwent / endpwent #include <pwd.h> struct passwd *getpwent(void) //成功返回指针,出错或 ...
- linux c编程:系统数据文件和信息
linux系统相关的文件信息包含在/etc/passwd文件和/etc/group中.每次登录linux系统以及每次执行ls -l命令时都要使用口令文件.这些字段都包含在<pwd.h>中定 ...
随机推荐
- PHP curl_copy_handle函数
curl_copy_handle — 复制一个cURL句柄和它的所有选项 说明 resource curl_copy_handle ( resource $ch ) 复制一个cURL句柄并保持相同的选 ...
- STM32输入捕获TIM2四通道
相比于一通道,原子的例程里因为清了计数时间,所以要对程序进行修改. 记录上升沿后的计数,然后记录下降沿的计数.相减后计算高电平时间,对于定时器中断间隔的边界要分开处理. 这里因为我的接收机时间是1ms ...
- Java操作Redis小案例
1.下载jar包. http://download.csdn.net/detail/u011637069/9594840包含本案例全部代码和完整jar包. 2.连接到redis服务. package ...
- centos查看磁盘空间大小
查看磁盘空间大小 df -h 查看当前文件夹所有文件大小 du -sh 查看指定文件夹大小 du -h /data 查看指定文件夹下所有文件的大小 du -h /data/ 查看指定文件大小 du - ...
- VMWARE ESXI 虚拟硬盘的格式:精简置备、厚置备延迟置零、厚置备置零
精简置备(thin): 精 简配置就是无论磁盘分配多大,实际占用存储大小是现在使用的大小,即用多少算多少.当客户机有输入输出的时候,VMkernel首先分配需要的空间并进行 清零操作,也就是说如果使用 ...
- linux下的命令是如何运行的
linux下的命令分为内建命令.可执行文件.脚本文件 shell终端里键入一个命令,如ls.cd.bash,shell会先查询一个环境变量PATH,它存了各种可执行文件的路径,输入$PATH可以打印变 ...
- 洛谷P4169 [Violet]天使玩偶/SJY摆棋子(CDQ分治)
[Violet]天使玩偶/SJY摆棋子 题目传送门 解题思路 用CDQ分治开了氧气跑过. 将输入给的顺序作为第一维的时间,x为第二维,y为第三维.对于距离一个询问(ax,ay),将询问分为四块,左上, ...
- 【刷题笔记】LeetCode 48. Rotate Image
题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...
- git使用记录八:不同提交的指定文件的差异
不同提交的指定文件的差异 git diff commit-id1 commit-id2 path-to-filename
- Java业务代理模式~
业务代理模式用于解耦表示层和业务层. 它基本上用于减少表示层代码中的业务层代码的通信或远程查找功能.在业务层有以下实体. 客户端(Client) - 表示层代码可以是JSP,servlet或UI ja ...