-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32207696.c
62 lines (53 loc) · 1.34 KB
/
32207696.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* mycp.c : copy command program
* @author : Joungwoo Lee
* @email : [email protected]
* @version : 1.0
* @date : 2022. 11. 04
**/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_BUF 64
int main(int argc, char *argv[]){
char buf[MAX_BUF];
int fd, fd2, read_size , write_size ;
struct stat stt;
if(argc != 3){
printf("mycp 실행 오류입니다.\n");
exit(1);
}
fd = open(argv[1],O_RDONLY);
fstat(fd, &stt);
fd2 = open(argv[2], O_RDWR | O_CREAT | O_EXCL, stt.st_mode);
if(fd<0 || fd2<0 ){
printf("Can't create file with errno %d\n", errno);
exit(1);
}
read_size=read(fd, buf, MAX_BUF);
if(read_size<0){
printf("파일을 읽을 수 없습니다.\n");
exit(1);
}
write_size=write(fd2, buf, read_size);
if(write_size<0){
printf("파일을 작성할 수 없습니다.\n");
exit(1);
}
while(1)
{
read_size = read(fd, buf, MAX_BUF);
if(read_size==0)
break;
write_size= write(STDOUT_FILENO, buf ,read_size);
}
printf("복사 완료!!\n");
close(fd);
close(fd2);
return 0;
exit(0);
}