2014年11月13日木曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信: お話は楽しい)、自分で考えてみよう(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.rss", 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/python", "/usr/bin/python", "rssgossip.py", phrase,
               NULL, vars) == -1)
      error("スクリプトを実行できません");
  }
  return 0;
}

Makefile

P=newshound2
CC=cc
CFLAGS=-g -Wall  # -O3
SRC=newshound2.c
OBJ=newshound2.o error.o
LDLIBS=

$(P): $(OBJ)
 $(CC) $(CFLAGS) $(LDLIBS) $(OBJ) -o $@

newshound2.o:
 $(CC) $(CFLAGS) -c newshound2.c -o $@

入出力結果(Terminal)

$ make
cc -g -Wall   -c newshound2.c -o newshound2.o
cc -g -Wall    newshound2.o error.o -o newshound2
$ ./newshound2 U.S.
$ cat stories.txt 
Sources: U.S reviews ISIS plan
Assad in U.S. crosshairs 
Injured test pilot questioned
U.S. sailors attacked in Turkey
Russia 'advancing into Ukraine'
Russian troops, tanks near border
Louis Vuitton's Paris landmark 
'Walking Dead' on the U.S. zombie trail
Sex museums: A global guide
Michelin's 'undisputed leader' in cuisine
$

0 コメント:

コメントを投稿