2018年2月25日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信 - お話は楽しい)、コードマグネット(p. 422)を取り組んでみる。

コードマグネット(p. 422)

//
//  main.c
//  sample1
//
//  Created by kamimura on 2018/02/26.
//  Copyright © 2018 kamimura. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

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

int main(int argc, const char * argv[]) {
    char *phrase = (char*)argv[1];
    char *vars[] = {
        "RSS_FEED=http://sitekamimura.blogspot.com/feeds/posts/default",
        NULL
    };
    FILE *f = fopen("stories.txt", "w");
    pid_t pid = fork();
    if (pid == -1) {
        error("プロセスをフォークできません");
    }
    if (!pid) {
        if (dup2(fileno(f), fileno(stdout)) == -1) {
            error("標準出力をリダイレクトできません");
        }
        if (execle("/usr/bin/python",
                   "/usr/bin/python",
                   "rssgossip.py", phrase, NULL,
                   vars) == -1) {
            error("スクリプトを実行できません");
        }
    }
    return 0;
}

入出力結果(Terminal)

$ ./rssgossip C
$ cat stories.txt 
 - Is Wikileaks founder Julian Assange about to leave the Ecuadorian embassy? - !
C - (fork(Python)unistd.h execle)
 - Could a crackdown on interest-only loans trigger a US-style housing meltdown? - !
C - ((Python)unistd.h execle)
 - Survivors of the Holocaust talk about how they defied the odds to rebuild their lives in Australia - !
 - Python - JavaScript -  - 1  1 - ()
C - (unistd.h execlexeclpstring.h strerror errno.h )
 - Did the market crash today and what does it mean for you? - !
C - (time.hasctime)
 - Economics Professor Justin Woolfers talks about today's share market sell off - !
C - ()
 - Python - JavaScript -  - 1  1 - ()
C -  - (stdarg.hva_listva_startva_argva_end)
C -  - ((enum))
 - There's another humanitarian crisis in Syria that could dwarf Aleppo - !
C -  - ((enum)switch )
 - Barnaby Joyce says end of his marriage 'one of the greatest failures of my life' - !
 - Python - JavaScript -  -  -  - ()
C -  - (sdtlib.h qsort)
 - Python - JavaScript -  - 1  1 - ()
 - Adam Bandt apologises to Jim Molan after accusing him of war crimes - !
$ ./rssgossip Python
$ cat stories.txt 
C - (fork(Python)unistd.h execle)
C - ((Python)unistd.h execle)
 - Python - JavaScript -  - 1  1 - ()
 - Python -  - 1  1 - ()
 - Python - JavaScript -  - 1  1 - ()
 - Python - JavaScript -  -  -  - ()
 - Python - JavaScript -  - 1  1 - ()
 - Python -  -  - (())
$ 

0 コメント:

コメントを投稿