2014年4月18日金曜日

開発環境

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

その他参考書籍

自分で考えてみよう(p.435)

コード(BBEdit, Emacs)

newshound2.c

#include <stdio.h>
#include <unistd.h>

#include "error.h"

int main(int argc, char *argv[])
{
  char *phrase = argv[1];
  char *vars[] = {"RSS_FEED=http://rss.cnn.com/rss/edition.rssl", NULL};
  FILE *out_file = fopen("stories.txt", "w");

  if (!out_file)
    error("stories.txtを開けません");

  pid_t pid = fork();
  if (pid == -1) {
    error("プロセスをフォークできません");
  }
  if (!pid) {
    if (dup2(fileno(out_file), 1) == -1)
      error("標準出力をリダイレクトできません");
    if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", phrase,
               NULL, vars) == -1)
      error("スクリプトを実行できません");
  }
  return (0);
}

Makefile

all: newshound2

newshound2: newshound2.c error.o
 cc -g -o newshound2 newshound2.c error.o

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

clean:
 rm -rf newshound2

入出力結果(Terminal)

$ make
cc -c -o error.o error.c
cc -g -o newshound2 newshound2.c error.o
$ ./newshound2 japan
$ cat stories.txt 
$ ./newshound2 u.s
$ cat stories.txt
Study: U.S. 'wary of futuristic tech'
Russia's red line
Pro-Russians thwart army
Corfu's amazing starry sky
Pope Francis continues to break tradition
Did U.S. miss a huge al Qaeda meet?
U.S.-Russia relations are icy ...
Rise of the mega-museums
How Louis Vuitton fortune was built 
$ 

0 コメント:

コメントを投稿