本期主要学习
1. 时间获取 time locatime mktime
3. 文件属性和权限的获取 stat getpwuid getgrgid readlink
时间获取:
1.time
time_t time(time_t *tloc);
功能:
返回 1970-1-1到现在的秒数 (格林威治时间)
参数:
tloc: 存放 秒数 空间首地址
返回值:
成功 返回 秒数
失败 返回 -1
2.localtime
struct tm *localtime(const time_t *timep);
功能:
将 秒数 转换为 本地时间
参数:
timep: 存放 秒数 空间首地址
返回值:
成功 返回 结构体时间
失败 返回 NULL
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 */
};
注意: 1. 结构体 tm 中,月份的计算是从0开始的,计算正确月份 +1 2.结构体 tm 中,年份的计算是从1900年开始,计算正确年份 +1900
3.mktime
time_t mktime(struct tm *tm);
功能:
将 本地时间 转换为 秒数
文件属性和权限的获取:
1.stat
int stat(const char *pathname, struct stat *statbuf );
功能:
将pathname对应的 文件信息 放入 statbuf 中
参数:
pathname: 文件路径 字符串的首地址
statbuf: 存放文件信息 空间的首地址
返回值:
成功 返回 0
失败 返回 -1
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
nlink_t st_nlink; /* Number of hard links */
uid_t st_uid; /* User ID of owner */
gid_t st_gid; /* Group ID of owner */
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
};
/etc/passwd 口令文件
/etc/group 组信息文件
2.getpwuid
struct passwd *getpwuid(uid_t uid);
功能:
通过 UID 获得 对应的用户信息
参数:
uid: 用户的ID号
返回值:
成功 返回 包含 用户信息 的 结构体
失败 返回 NULL
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 */
};
3.getgrgid
struct group *getgrgid(gid_t gid);
功能:
通过 组ID 获得 组信息
参数:
gid: 组的ID号
返回值:
成功 返回 包含组信息的结构体
失败 返回 NULL
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.readlink
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);
功能:
读取 连接文件 本身的内容
参数:
pathname: 连接文件的路径
buf: 存放数据空间首地址
bufsiz: 最大存放数据字节数
返回值:
成功 返回 读到字节个数
失败 返回 -1
软连接和硬连接:
1.软连接(符号连接)
通过文件名连接,所有能够看到的 连接文件 均为 软连接文件
ln -s file.txt a.txt 创建一个连接项 file.txt的文件 名字叫: a.txt
当file.txt 输入 abc 的文件内容 a.txt 文件内容也显示 abc
当a.txt 输入 123 file.txt 文件内容也会显示 123
In:创建链接文件 -s:软连接
2.硬链接
通过 文件对应的 inode 节点连接
ln file.txt b.txt
注意: 1.软连接若删除 file.txt ,file.txt和a.txt的连接关系 仍然存在,创建file.txt可恢复, 因为软连接是通过文件名实现连接的 2.硬连接若删除 file.txt ,file.txt和a.txt的连接关系 不存在。
作业:
1.编写程序实现ls -l的功能查看当前目录下所有文件的详细信息