Linux-时间接口-005

学习重点:

1.函数接口
2.【ls-l】命令的实现

1【time】

1.1函数原型

【time_t time(time_t *tloc);】

1.2函数功能

返回1970-1-1到现在的秒数(格林威治时间)

1.3函数参数

1.3.1【tloc】

存放秒数空间首地址
存放的秒数:如果【tloc】非空,返回值也会存储在tloc指向的内存中;自【1970-01-01 00:00:00+0000(UTC)】时起到现在的秒数。

1.4返回值

【成功】:返回秒数
【失败】:返回-1 

1.5源码示例

在这里插入图片描述

2【localtime】

2.1函数原型

【struct tm *localtime(const time_t *timep);】

2.2函数功能

将秒数转换为本地时间

2.3函数参数

2.3.1【timep】

存放秒数空间首地址

2.4返回值

【成功】:返回结构体时间
【失败】:返回NULL
包含时间信息的结构体(详见【man】手册【man localtime】):
struct tm {
int tm_sec;    /* Seconds (0-60) */
int tm_min;    /* Minutes (0-59) */
int tm_hour;   /* Hours (0-23) */
int tm_mday;   /* Day of the month (1-31) */
int tm_mon;    /* Month (0-11) */
int tm_year;   /* Year - 1900 */
int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst;  /* Daylight saving time */
};

2.5源码示例

在这里插入图片描述

注意:

(1)【tm】结构体中的年份:是从【1990】年计算的,要得到正确年份,则应【+1990】。
(2)【tm】结构体中的月份:是从【0】开始计算的,要得到正确月份,则应【+1】。

3【mktime】

3.1函数原型

【time_t mktime(struct tm *tm);】

3.2函数功能

将本地时间转换为秒数

3.3函数参数

3.3.1【tm】

包含时间信息的结构体,用于输入本地时间。

结构体说明(详见【man】手册:【man mktime】):
struct tm {
           int tm_sec;    /* Seconds (0-60) */
           int tm_min;    /* Minutes (0-59) */
           int tm_hour;   /* Hours (0-23) */
           int tm_mday;   /* Day of the month (1-31) */
           int tm_mon;    /* Month (0-11) */
           int tm_year;   /* Year - 1900 */
           int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
           int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
           int tm_isdst;  /* Daylight saving time */
};

3.4返回值

【成功】:返回时间(time_t类型)
【失败】:返回-1(time_t类型)

3.5源码示例

在这里插入图片描述

4文件属性获取

【man 2 stat】
【man 7 inode】

4.1【stat】

4.1.1函数原型

【int stat(const char *pathname, struct stat *statbuf);】

4.1.2函数功能

将pathname对应的文件信息放入statbuf中

4.1.3函数参数

4.1.3.1【pathname】
文件路径字符串的首地址
4.1.3.2【statbuf】
存放文件信息空间的首地址
结构体说明(详见【man】手册,【man stat】):
struct stat {
    dev_t     st_dev;         /* ID of device containing file(文件所在设备的 ID) */
    ino_t     st_ino;         /* Inode number (文件对应 inode 节点编号)*/
    mode_t    st_mode;        /* File type and mode(文件对应的模式) */
    nlink_t   st_nlink;       /* Number of hard links(文件的链接数) */
    uid_t     st_uid;         /* User ID of owner(文件所有者的用户 ID) */
    gid_t     st_gid;         /* Group ID of owner(文件所有者的组 ID) */
    dev_t     st_rdev;        /* Device ID (if special file) 设备号(指针对设备文件)*/
    off_t     st_size;        /* Total size, in bytes */
    blksize_t st_blksize;     /* Block size for filesystem I/O */
    blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

    /* Since Linux 2.6, the kernel supports nanosecond
        precision for the following timestamp fields.
        For the details before Linux 2.6, see NOTES. */

    struct timespec st_atim;  /* Time of last access */
    struct timespec st_mtim;  /* Time of last modification */
    struct timespec st_ctim;  /* Time of last status change */

#define st_atime st_atim.tv_sec      /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
【st_dev】:文件所在的设备。
【st_ino】:文件的【inode】号。
【st_mode】:文件的模式,如【文件类型】【文件权限】都记录在该变量中。
【st_nlink】:记录文件的【硬链接数】,即为该文件创建了多少个硬链接文件。
【st_uid】:文件所有者的用户ID。 
【st_gid】:文件所有者的组ID。
【st_rdev】:该变量记录了设备号,设备号只针对于设备文件,包括字符设备文件和块设备文件。
【st_size】:该变量记录了文件的大小,以字节为单位。
【st_atim】:该变量记录了文件最后被访问的时间,是【struct timespec】类型变量。
【st_mtim】:该变量记录了文件内容最后被修改的时间,是【struct timespec】类型变量。
【st_ctim】:该变量记录了文件状态最后被改变的时间,是【struct timespec】类型变量。

4.1.4返回值

【成功】:返回0 
【失败】:返回-1 

4.1.5文件属性详解

既然【stat】函数是获取指定路径文件的属性,那么使用【stat】函数的关键在于要知道文件有那些属性。
4.1.5.1文件类型

4.2【getpwuid】

4.2.1函数原型

【struct passwd *getpwuid(uid_t uid);】

4.2.2函数功能

通过UID获得对应的用户信息

4.2.3函数参数

4.2.3.1【uid】
用户的ID号

4.2.4返回值

【成功】:返回包含用户信息的结构体
【失败】:返回NULL
结构体说明(详见【man】手册:【man getpwuid】):
struct passwd {
    char   *pw_name;       /* username */
    char   *pw_passwd;     /* user password */
    uid_t   pw_uid;        /* user ID */
    gid_t   pw_gid;        /* group ID */
    char   *pw_gecos;      /* user information */
    char   *pw_dir;        /* home directory */
    char   *pw_shell;      /* shell program */
};

4.3【getgrgid】

4.3.1函数原型

【struct group *getgrgid(gid_t gid);】

4.3.2函数功能

通过组ID获得组信息

4.3.3函数参数

4.3.3.1【gid】
组的ID号

4.3.4返回值

【成功】:返回包含组信息的结构体
【失败】:返回NULL
结构体说明(详见【man】手册:【man getgrgid】):
struct group {
    char   *gr_name;        /* group name */
    char   *gr_passwd;      /* group password */
    gid_t   gr_gid;         /* group ID */
    char  **gr_mem;         /* NULL-terminated array of pointers to names of group members */
};

4.4【readlink】

4.4.1函数原型

【ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);】

4.4.2函数功能

读取连接文件本身的内容

4.4.3函数参数

4.4.3.1【pathname】
链接文件的路径
4.4.3.2【buf】
存放数据空间首地址
4.4.3.3【bufsiz】
最大存放数据字节数

4.4.4返回值

【成功】:返回读到字节个数
【失败】:返回-1 

4.5链接属性

4.4.1软连接(符号链接)

通过文件名链接,所有能够看到的连接文件均为软连接文件
【ln -s file.txt a.txt】 

4.4.2硬链接

通过文件对应的【inode】节点链接     
【ln file.txt b.txt】 

4.6写一个程序实现【ls-l】命令

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值