2015年2月26日木曜日

開発環境

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

その他参考書籍

エクササイズ(p.447)

コード(BBEdit, Emacs)

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stopif.h>
#include <sys/wait.h>

int main(int argc, char **argv) {
  char *vars[] = {"RSS_FEED=http://rss.cnn.com/rss/edition.rss", NULL};
  int fd[2];
  Stopif(pipe(fd) == -1, exit(1), "パイプを作成できません: %s", strerror(errno));
  pid_t pid = fork();
  Stopif(pid == -1, exit(1), "プロセスをフォークできません: %s",
         strerror(errno));  
  if (!pid) {
    close(fd[0]);
    dup2(fd[1], 1);
    Stopif(execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", "-u",
                  argv[1], NULL, vars) == -1,
           exit(1), "スクリプトを実行できません: %s", strerror(errno));    
  }
  dup2(fd[0], 0);
  close(fd[1]);
  char line[255];
  while (fgets(line, 255, stdin))
    if (line[0] == '\t') printf("%s", line + 1);
}

入出力結果(Terminal)

$ make news_opener && ./news_opener US
clang ...
http://edition.cnn.com/2015/02/25/us/new-york-terror-plot/index.html?eref=edition
http://edition.cnn.com/2015/02/25/us/bikram-yoga-sex-assault-lawsuits/index.html?eref=edition
http://edition.cnn.com/2015/02/25/world/amanpour-norway-ine-eriksen-soreide/index.html?eref=edition
http://edition.cnn.com/video/data/2.0/video/world/2015/02/23/pkg-mclaughlin-russia-anti-american-sentiment-growing.cnn.html?eref=edition
http://edition.cnn.com/video/data/2.0/video/world/2015/02/24/lkl-foster-octopus-crab-battle.cnn.html?eref=edition
$

0 コメント:

コメントを投稿