0) { for (ssize_t i = 0; i < bytes_read; i++) { if (buffer[i] == '\n') current_line++; // 줄바꿈 문자로 줄 수 증가 write(STDOUT_FILENO, &buffer[i], 1); // 문자 출력 if (current_line >= lines) break; // 지정된 줄 수만큼 출력 시 종료 } "> 0) { for (ssize_t i = 0; i < bytes_read; i++) { if (buffer[i] == '\n') current_line++; // 줄바꿈 문자로 줄 수 증가 write(STDOUT_FILENO, &buffer[i], 1); // 문자 출력 if (current_line >= lines) break; // 지정된 줄 수만큼 출력 시 종료 } "> 0) { for (ssize_t i = 0; i < bytes_read; i++) { if (buffer[i] == '\n') current_line++; // 줄바꿈 문자로 줄 수 증가 write(STDOUT_FILENO, &buffer[i], 1); // 문자 출력 if (current_line >= lines) break; // 지정된 줄 수만큼 출력 시 종료 } ">
#include <unistd.h>  // read, write, close 함수 사용
#include <fcntl.h>   // open 함수 사용
#include <string.h>  // 문자열 처리 함수 사용
#include <errno.h>   // errno 사용 (오류 코드 확인)
#include <stdlib.h>  // atoi, EXIT_SUCCESS, EXIT_FAILURE 사용
#include <stdio.h>   // 표준 입출력 함수 사용

#define BUFFER_SIZE 1024 // 읽기 버퍼 크기를 1024바이트로 정의

// 파일의 첫 N줄을 출력하는 함수
void myhead(const char *filename, int lines) {
    int fd = filename ? open(filename, O_RDONLY) : STDIN_FILENO; // 파일 열기 또는 표준 입력 사용
    if (fd < 0) { // 파일 열기 실패
        perror("Error opening file"); // 오류 메시지 출력
        return;
    }

    char buffer[BUFFER_SIZE];
    ssize_t bytes_read;
    int current_line = 0;

    // 파일 읽기 및 출력
    while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
        for (ssize_t i = 0; i < bytes_read; i++) {
            if (buffer[i] == '\n') current_line++; // 줄바꿈 문자로 줄 수 증가
            write(STDOUT_FILENO, &buffer[i], 1);  // 문자 출력
            if (current_line >= lines) break;    // 지정된 줄 수만큼 출력 시 종료
        }
        if (current_line >= lines) break;
    }

    if (bytes_read < 0) { // 읽기 오류 발생
        perror("Error reading file");
    }

    if (fd != STDIN_FILENO) close(fd); // 파일 닫기
}

// main 함수
int main(int argc, char *argv[]) {
    int lines = 10;    // 기본 출력 줄 수
    int argIndex = 1;  // 파일 목록 시작 위치

    // -n 옵션 처리
    if (argc > 1 && strcmp(argv[1], "-n") == 0) {
        if (argc < 3) { // -n 뒤에 숫자가 없는 경우
            fprintf(stderr, "Missing argument for -n\n");
            return EXIT_FAILURE;
        }
        lines = atoi(argv[2]); // 줄 수 설정
        if (lines <= 0) { // 유효하지 않은 숫자인 경우
            fprintf(stderr, "Invalid line count: %s\n", argv[2]);
            return EXIT_FAILURE;
        }
        argIndex = 3; // 파일 목록 시작 위치 조정
    }

    // 파일 이름이 없는 경우 표준 입력 처리
    if (argc == argIndex) {
        myhead(NULL, lines);
    } else {
        // 여러 파일 처리
        for (int i = argIndex; i < argc; i++) {
            myhead(argv[i], lines);         // 파일 내용 출력
        }
    }

    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>      // open 함수
#include <unistd.h>     // read, write, close 함수
#include <errno.h>      // errno 처리
#include <string.h>     // strerror 함수

// 파일을 복사하는 함수
void mycp(const char *src, const char *dest) {
    int sourceFd = open(src, O_RDONLY); // 소스 파일 열기
    if (sourceFd == -1) 
    {
        perror("Error opening source file"); // 파일 열기 실패 시 오류 메시지 출력
        return;
    }

    int destFd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0644); // 대상 파일 열기
    if (destFd == -1) 
    {
        perror("Error opening destination file"); // 파일 열기 실패 시 오류 메시지 출력
        close(sourceFd); // 소스 파일 닫기
        return;
    }

    char buffer[1024];
    ssize_t bytesRead;
    while ((bytesRead = read(sourceFd, buffer, sizeof(buffer))) > 0) { // 소스 파일에서 데이터 읽기
        ssize_t bytesWritten = write(destFd, buffer, bytesRead); // 대상 파일로 데이터 쓰기
        if (bytesWritten == -1) 
        {
            perror("Error writing to destination file"); // 쓰기 실패 시 오류 메시지 출력
            break;
        }
    }

    if (bytesRead == -1) { // 읽기 실패 시 오류 메시지 출력
        perror("Error reading source file");
    }

    close(sourceFd); // 소스 파일 닫기
    close(destFd); // 대상 파일 닫기
}

int main(int argc, char *argv[]) 
{
    if (argc != 3) { // 인자가 정확히 2개가 아닌 경우 오류 메시지 출력
        fprintf(stderr, "Usage: %s source_file destination_file\n", argv[0]);
        return EXIT_FAILURE;
    }

    mycp(argv[1], argv[2]); // 파일 복사 수행

    return EXIT_SUCCESS;
}