dup ,dup2,fcntl,ioctl系统调用
1 1. dup ,dup2 函数 2 int dup(int oldfd) 3 int dup(int oldfd , int newfd)
dup用来复制参数oldfd的文件描述符
duo2可以用newfd来指定新文件描述符的数值
2.fcntl函数
对已经打开的文件描述符进行各种控制操作,以及改变已经打开文件的各种属性
3.ioctl函数
控制设备,不能用其他函数进行控制操作都可以用ioctl来进行
3.文件属性的操作
1. shell里可通过ls来获取文件属性,那么在程序里怎么获取文件属性呢?
用到stat/fstat/lstat
1 man 2 stat 2 #include<sys/types.h> 3 #include<sys/stat.h> 4 #include<unistd.h> 5 int stat(const char *file_name,struct stat *buf); 6 int fstat(int filedes,struct stat *buf); 7 int lstat(const char *fime_name ,struct stat *buf);
stat 获取由参数file_name指定的文件名的状态信息,保存参数到struct stat *buf中
fstat 与stat 区别,fstat通过文件描述符来指定文件
lstat 与stat,对于符号连接文件,lstat返回的是符号连接文件本身的状态信息。而stat返回的是符号连接文件指向的文件的状态信息。
1 struct stat { 2 dev_t st_dev; /* ID of device containing file */ 3 ino_t st_ino; /* inode number */ 4 mode_t st_mode; /* protection */ 5 nlink_t st_nlink; /* number of hard links */ 6 uid_t st_uid; /* user ID of owner */ 7 gid_t st_gid; /* group ID of owner */ 8 dev_t st_rdev; /* device ID (if special file) */ 9 off_t st_size; /* total size, in bytes */ 10 blksize_t st_blksize; /* blocksize for file system I/O */ 11 blkcnt_t st_blocks; /* number of 512B blocks allocated */ 12 time_t st_atime; /* time of last access */ 13 time_t st_mtime; /* time of last modification */ 14 time_t st_ctime; /* time of last status change */ 15 };
eg:获取文件属性
79,0-1 Bot
1 #include<stdio.h> 2 #include<time.h> 3 #include<sys/stat.h> 4 #include<unistd.h> 5 #include<sys/types.h> 6 #include<errno.h> 7 8 int main(int argc , char * argv[]){ 9 struct stat buf; 10 /*check param number*/ 11 if(argc != 2){ 12 printf("Usage mystat <file_name>\n"); 13 exit(0); 14 } 15 /*get file attr*/ 16 if(stat(argv[1],&buf) == -1){ 17 perror("stat:"); 18 exit(1); 19 } 20 //print file attr 21 printf("device is :%d\n",buf.st_dev); 22 printf("inode is :%d\n",buf.st_ino); 23 printf("mode is :%d\n",buf.st_mode); 24 printf("number of hard links is :%d\n",buf.st_nlink); 25 printf("user ID of owner is :%d\n",buf.st_uid); 26 printf("group ID of owner is :%d\n",buf.st_gid); 27 printf("device type (if inode device ) is :%d\n",buf.st_rdev); 28 29 printf("total size ,in bytes is :%d\n",buf.st_size); 30 printf("blocksize for filesystem I/O is :%d\n",buf.st_blksize); 31 printf("number of blocks allocated is :%d\n",buf.st_blocks); 32 33 printf("time of last access is :%s\n",ctime(&buf.st_atime)); 34 printf("time of last modification is :%s\n",ctime(&buf.st_mtime)); 35 printf("time of last change is :%s\n",ctime(&buf.st_ctime)); 36 37 return 0; 38 39 } 40 output : 41 [[email protected] C]$ ./my_chmod example.c 42 device is :2050 43 inode is :407124 44 mode is :33152 45 number of hard links is :1 46 user ID of owner is :500 47 group ID of owner is :500 48 device type (if inode device ) is :0 49 total size ,in bytes is :0 50 blocksize for filesystem I/O is :4096 51 number of blocks allocated is :0 52 time of last access is :Mon Jan 5 23:23:36 2015 53 time of last modification is :Mon Jan 5 23:29:37 2015 54 time of last change is :Mon Jan 5 23:29:37 2015
2.设置文件属性
1 chmod/fchmod , chown/fchown/lchown,truncate/ftruncate,utime,umask 2 1.chmod/fchmod 3 修改文件的存取权限 4 2.chown/fchown/lchown 5 修改文件的用户id和组id 6 3.ftruncate/truncate 7 改变文件大小 8 4.utime 9 改变文件的st_mtime和st_ctime域,即存取时间和修改时间。 10 5.umask 11 使用文件创建时使用的屏蔽字
时间: 2024-11-05 21:35:24