5月8号作业

  1:使用 dup2 实现错误日志功能,使用 write 和 read 实现文件的拷贝功能,注意,代码中所有函数后面,紧跟perror输出错误信息,要求这些错误信息重定向到错误日志 err.txt 中去

1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <pthread.h>
9 #include <semaphore.h>
0 #include <wait.h>
1 #include <signal.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4 #include <sys/socket.h>
5 #include <sys/ipc.h>
6 #include <sys/sem.h>
7 #include <semaphore.h>
8 #include <sys/msg.h>
9 #include <sys/shm.h>
0 #include <sys/un.h>
1 
2 int main(int argc, const char *argv[])
3 {
4     int pf1=open("./err.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
5     int no=dup(2);
6     dup2(pf1,2);
7     if(pf1==-1)
8     {
9         perror("错误信息\n");
0         return 1;
1     }
2     int pf2=open(argv[1],O_RDONLY,0666);
3     if(pf2==-1)
4     {                                                               
5         perror("报错信息\n");
6         return 1;
7     }
8     int pf3=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC|O_APPEND,0666);
9     if(pf3==-1)
0     {
1         perror("报错信息\n");
2         return 1;
3     }
4     while(1)
5     {
6         char buf[100]={0};
7         int retval=read(pf2,buf,100);
8         if(retval==0)
9         {
0             break;
1         }
2         write(pf3,buf,100);
3     }
4     fflush(stderr);
5     close(pf1);
6     close(pf2);
7     close(pf3);
8     dup2(no,2);
9 
0 
1     return 0;
2 }

   2:判断一个文件是否拥有用户可写权限,如果拥有,则去除用户可写权限,如果不拥有,则加上用户可写权限
                权限更改函数:就是chmod函数,自己去man一下
                要求每一次权限更改成功之后,立刻在终端显示当前文件的权限信息 :使用 ls -l显示(使用 system函数配合shell指令 ls -l 来实现)

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 #include <sys/types.h>
 6 #include <sys/stat.h>
 7 #include <fcntl.h>
 8 #include <pthread.h>
 9 #include <semaphore.h>
10 #include <wait.h>
11 #include <signal.h>
12 #include <sys/socket.h>
13 #include <arpa/inet.h>
14 #include <sys/socket.h>
15 #include <sys/ipc.h>
16 #include <sys/sem.h>
17 #include <semaphore.h>
18 #include <sys/msg.h>
19 #include <sys/shm.h>
20 #include <sys/un.h>
21 
22 int main(int argc, const char *argv[])
23 {
24     int fp=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC);
25     struct stat buf={0};
26     stat(argv[1],&buf);
27     mode_t mode=buf.st_mode;
28     if((mode|S_IWUSR)==mode)
29     {
30         chmod(argv[1],mode&~S_IWUSR);
31         system("ls -l sstr.exe | lolcat");
32     }
33     else
34     {
35         chmod(argv[1],mode|S_IWUSR);
36         system("ls -l sstr.exe | lolcat");            
37     }
38     close(fp);
39 
40     return 0;
41 }
                                                         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值