Linux day4

stat readdir dup2

stat函数

Linux day4
获得文件信息
int stat(const charpathname,strct stat buf);
struct timespec{
_kernel_time_t tv_sec; /seconds/当前时间到1970.1.1 0:0:0的秒数
long tv_nsec; /nanoseconds/纳秒
1s = 1000ms 毫秒
1ms = 1000 us 微秒
1us = 1000 ns 纳秒
Linux day4
stat函数参数
pathname 文件名
stract stat
buf 传出参数,定义 stract stat sb; &sb
返回值
返回成功 0,返回失败 -1;设置errno
需要使用 struct passwd
getpwuid(uid_t uid);获得用户名需要传入uid
struct passwd
{
charpw_name;username用户名
char
pw_passwd; user password
uid_t pw_uid; ser ID
gid_t pw _gid; groupID
charpw_gecos; user information
char
pw_dir; home directory
charpw_shell; shell program
struct group
getgrgid(gid_t gid);
}
struct group*getgrgid(gid_t gid);获得组名

struct group{
chargr_name;/group name/组名
char
gr_password;/group password/
gid_t gr_gid; /group ID/
char**gr_mem;/NULL-terminsted array of pointers
to names of group members/
};

获得本地实际
struct tm *localtime(const time_t*timep):
传入参数 timep对应stat函数得到的结构体的秒数(time_类型)
返回tm结构
siruci imi
int tm sec:;/*Seconds (0-60)/
int tm_min; Minutes0-59分钟
int tm_hour;Hours(0-23)小时
int tm_mday; / Day of the month (1-31)/
int tm_mon;Month(0-11)/月,需要+1
int tm_year;year-1900/年,需要+1900
int tm_wday; / Day of the week(0-6, Sunday=0)'/
int tm_yday; /*Day in the year (0-365, Jan=0)'/
int tm_isdst; /" Daylight saving time*/

strct stat
dev_t st dev ;/ID of device containing file/设备编号
ino_t st_ino;/* inode numberinode*/节点
mode_t st_mode; /protection/类型与权限
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; /blocksize 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.
efore Linux 2.6, see NOTES.

gdb调试

p 查看结构体信息
p/o 按照8进制显示数据

注意stat 与lstat的区别
stat碰到连接会追溯到源文件,穿透 lstat不会穿透

int truncate(const*path,off_t length);
path文件名
length 长度如果大于原文件,直接拓展,如果小于原文件,截断为length长度

创建硬链接
int link(const charoldpath,const charnewpath);
oldpath 原文件
newpath 硬链接文件

创建软链接
int symlink(const chartarget,const charlinkpath);

读取链接
ssize_t readlink(const charpathname,charbuf,size_t bufsiz);
pathname 链接名
buf 缓冲区
bufsize 缓冲区大小

返回值:成功返回buf填充的大小,失败返回-1

删除软硬链接
int unlink(const char*pathname);
pathname 对应的链接

errno 错误信息
dep2 
作用:复制文件描述符
函数原型:int dep2(int oldfd,int newfd);
调用后,后指向前
返回值:
失败返回-1,设置errno
成功返回新文件描述符

dep
作用:复制文件描述符,可用于备份
原型:int dup(int oldfd)
返回值
失败反回-1,设置errno;
成功返回新的文件描述符