#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

void error(char *msg);

int main(int argc, char *argv[]) {
    char *phrase = argv[1];
    char *vars[] = {"RSS_FEED=helloworld", NULL};
    FILE *f = fopen("stories.txt", "w");

    if(!f) {
        error("stories.txt를 열 수 없습니다.");
    }

    pid_t pid = fork();

    if(pid == -1) {
        error("프로세스를 포크할 수 없습니다.");        
    }

    if(!pid) {
        if(dup2(fileno(f), 1) == -1 ) {
            error("표준 출력으로 리다이렉션할 수 없습니다.");
        }

        if(execle("/usr/bin/ls", "/usr/bin/ls", "-lrt", NULL, vars) == -1) {
            error("ls 명령어를 실행할 수 없습니다.");
        }
    }
    return 0;
}

void error(char *msg){
    fprintf(stderr, "%s: %s\n", msg, strerror(errno));
    exit(1);
}

+ Recent posts