GMS Linux: start?
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
TARGET = cp
|
||||
SRCS = cp.c
|
||||
CC = gcc
|
||||
CFLAGS = -static -Wall
|
||||
|
||||
OUT_BIN ?= $(TARGET)
|
||||
|
||||
all: $(OUT_BIN)
|
||||
|
||||
$(OUT_BIN): $(SRCS)
|
||||
@echo " [CC] Compiling to $(OUT_BIN)..."
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
@@ -0,0 +1,42 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user