dup ,dup2,fcntl,ioctl系统调用
1 1. dup ,dup2 函数2 int dup(intoldfd)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 2stat2 #include
3 #include
4 #include
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 structstat {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
2 #include
3 #include
4 #include
5 #include
6 #include
7
8 int main(int argc , char *argv[]){9 structstat buf;10 /*check param number*/
11 if(argc != 2){12 printf("Usage mystat \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 [fubin@localhost C]$ ./my_chmod example.c42 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,umask2 1.chmod/fchmod3 修改文件的存取权限4 2.chown/fchown/lchown5 修改文件的用户id和组id6 3.ftruncate/truncate7 改变文件大小8 4.utime9 改变文件的st_mtime和st_ctime域,即存取时间和修改时间。10 5.umask11 使用文件创建时使用的屏蔽字
屏蔽文件权限eg:
1 #include
2 #include
3 #include
4 #include
5
6 intmain(){7
8 umask(0);//don't shield any permission
9 if(creat("umasktest.txt",S_IRWXU|S_IRWXG|S_IRWXO) < 0){10 perror("creat");11 exit(1);12 }13
14 umask(S_IRWXO);//shield other user's permission
15 if(creat("umasktest1.txt",S_IRWXU|S_IRWXG|S_IRWXO) < 0){16 perror("creat");17 exit(1);18 }19
20 return 0;21 }22
23 output:24 -rwxrwx---. 1 fubin fubin 0 Jan 6 17:34umasktest1.txt25 -rwxrwxrwx. 1 fubin fubin 0 Jan 6 17:34umasktest.txt26
3.文件的移动和删除
1.文件的移动
1 rename 修改文件名或移动文件位置2 #include
3 int rename(const char *oldpath, const char *newpath);4 将oldpath文件夹名改为newpath,若newpath存在,则源文件会被删除5 用rename实现简单linux下mv的功能6 #include
7 #include
8 int main(int argc , char **argv){9
10 if(argc != 3){11 perror("my_mv ");12 exit(0);13 }14
15 if(rename(argv[1],argv[2]) < 0) {16 perror("rename");17 exit(1);18 }19
20 return 0;21 }22
23 incompatible implicit declaration of built-in function 'exit'
24 警告:exit函数在stdlib库中,需要包含#include进来
2.文件的删除
1 unlink 文件的删除2 rmdir 目录的删除3 remove 封装了unlink和rmdir4
5 unlink:文件的链接数为0且没有进程打开这个文件,文件被删除且其占用的磁盘空间被释放。6 //如果文件的链接数为0但是有进程打开了这个文件,文件暂时不删除,知道所有打开这个文件的进程结束时文件才被删除。
7 使用这一点可以确保程序崩溃时,他所创建的临时文件也不会保留。8 #include
9 int unlink(const char *pathname);10 //参数pathname指向一个符号链接,连接被删除。若 指向一个套接字(socket),FIFO(命名管道),设备文件,该名字被删除,但已经打开这个文件的进程仍然可以使用。
11
3.目录的创建和删除
1.目录的创建 mkdir
2.目录的删除 rmdir