Files
kernel-study/userprog/cp/cp.c
T
2025-12-30 01:20:20 +00:00

42 lines
901 B
C

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: cp <source> <dest>\n");
return 1;
}
int src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
perror("cp: source open error");
return 1;
}
// dest 파일 생성 (이미 있으면 덮어쓰기)
int dest_fd = open(argv[2], O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (dest_fd < 0) {
perror("cp: dest open error");
close(src_fd);
return 1;
}
char buf[BUF_SIZE];
int n;
// EOF(0)가 될 때까지 읽어서 씀
while ((n = read(src_fd, buf, BUF_SIZE)) > 0) {
if (write(dest_fd, buf, n) != n) {
perror("cp: write error");
break;
}
}
close(src_fd);
close(dest_fd);
return 0;
}