1、文件拷贝、错误日志
#include <allhead.h>
int main(int argc, const char *argv[])
{
int efd=open("./err.txt",O_WRONLY | O_CREAT | O_TRUNC,0664);
dup2(efd,STDERR_FILENO);
int fd1=open(argv[1],O_RDONLY);
//perror("被拷贝文件打开状态:");
int fd2=open(argv[2],O_WRONLY | O_CREAT | O_TRUNC,0664);
perror("拷贝文件打开状态:");
char buf[20]={0};
while(1)
{
int res=read(fd1,buf,1);
perror("读取文件状态:");
int res1=write(fd2,buf,1);
perror("写入文件状态:");
if(res1==-1){break;}
if(res==0){break;}
}
close(fd2);
close(fd1);
return 0;
}
2、判断权限进行更改
#include <allhead.h>
int main(int argc, const char *argv[])
{
struct stat mode;
char str[100]="ls -l ";
strcat(str,argv[1]);
int res=stat(argv[1],&mode);
printf("%d\n",mode.st_mode);
if((S_IWUSR | mode.st_mode) == mode.st_mode)
{printf("有可写权限\n");
chmod(argv[1],mode.st_mode&(~S_IWUSR));
printf("可写权限已清除\n");
system(str);
}
else
{
printf("没有可写权限\n");
chmod(argv[1],mode.st_mode|(S_IWUSR));
printf("已添加可写权限\n");
system(str);
}
return 0;
}