1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <unistd.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 int main(int argc, const char *argv[])
22 {
23 int errfd=open("./err.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
24 if(errfd==-1)
25 {
26 perror("no open err.txt");
27 return -1;
28 }
29 if(dup2(errfd,2)==-1)
30 {
31 perror("dup2 error");
32 close(errfd);
33 return -1;
34 }
35 close(errfd);
36 int rfd=open(argv[1],O_RDONLY);
37 if(rfd==-1)
38 {
39 perror("no open argv[1]");
40 return -1;
41 }
42 int wfd=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0666);
43 if(wfd==-1)
44 {
45 perror("no open argv[2]");
46 return -1;
47 }
48 char str[100]={0};
49 int a=0;
50 while((a=read(rfd,str,100)))
51 {
52 if(a==-1)
53 {
54 perror("read error");
55 break;
56 }
57 if(write(wfd,str,a)==-1)
58 {
59 perror("write error");
60 break;
61 }
62 }
63 if(close(rfd)==-1)
64 {
65 perror("close rfd error");
66 return -1;
67 }
68 if(close(wfd)==-1)
69 {
70 perror("close wfd error");
71 return -1;
72 }
73 return 0;
74 }
~ 5 #include <unistd.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 int main(int argc, const char *argv[])
22 {
23 int file_exist=access(argv[1],F_OK);
24 if(file_exist==EOF)
25 {
26 perror("file no exist");
27 return -1;
28 }
29 struct stat buf={0};
30 if(stat(argv[1],&buf)==EOF)
31 {
32 perror("stat error");
33 return -1;
34 }
35 mode_t mode=buf.st_mode;
36 if((mode|S_IWUSR)==mode)
37 {
38 if(chmod(argv[1],(mode &~S_IWUSR))==EOF)
39 {
40 perror("chmod error");
41 return -1;
42 }
43 }
44 else
45 {
46 if(chmod(argv[1],(mode|S_IWUSR))==EOF)
47 {
48 perror("chmod error");
49 return -1;
50 }
51 }
52 char str[200]={0};
53 snprintf(str,200,"ls -l %s",argv[1]);
54 system(str);
55 return 0;
56 }
~
~
~