2014年4月19日土曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセス間通信)、エクササイズ(p.447)を解いてみる。

その他参考書籍

エクササイズ(p.447)

コード(BBEdit, Emacs)

news_opener.c

#include <stdio.h>
#include <unistd.h>
#include "error.h"
#include "open_url.h"

int main(int argc, char *argv[])
{
    char *phrase = argv[1];
    char *vars[] = {"RSS_FEED=http://rss.cnn.com/rss/edition.rss", NULL};
    int fd[2];

    if (pipe(fd) == -1)
      error("パイプを作成できません");
    pid_t pid = fork();
    if (pid == -1)
      error("プロセスをフォークできません");
    if (!pid) {
      close(fd[0]);
      dup2(fd[1], 1);
      if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", "-u",
                 phrase, NULL, vars) == -1)
        error("スクリプトを実行できません");
    }
    dup2(fd[0], 0);
    close(fd[1]);
    char line[255];
    while (fgets(line, 255, stdin))
      if (line[0] == '\t')
        open_url(line + 1);
    return (0);
}

Makefile

all: news_opener

news_opener: news_opener.c error.o open_url.o
 cc -g -o news_opener news_opener.c error.o open_url.o

error.o: error.c error.h
 cc -c -o error.o error.c

open_url.o: open_url.c open_url.h
 cc -c -o open_url.o open_url.c

clean:
 rm -rf news_opener

入出力結果(Terminal)

$ make && ./news_opener u.s
cc -c -o open_url.o open_url.c
cc -g -o news_opener news_opener.c error.o open_url.o
http://edition.cnn.com/2014/04/18/opinion/allison-ukraine-civil-war/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/2014/04/16/world/asia/south-korea-sinking-ship-texts/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/2014/04/18/world/terrorist-messages-surface/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/2014/04/18/showbiz/miley-cyrus-hospitalized/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/video/data/2.0/video/international/2014/03/24/vo-corfu-time-lapsed-night-sky.vasilis-metallino.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/2014/04/18/world/jihadist-twitter-state-department-trolls-terrorists/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/2014/04/18/opinion/allison-ukraine-civil-war/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/2014/04/10/world/europe/british-museum-tate-modern/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
http://edition.cnn.com/2014/02/26/business/how-a-fortune-was-made/index.html?eref=edition
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
$ 

0 コメント:

コメントを投稿