2015年1月3日土曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセスとシステムサービス: 限界を超える)、コードマグネット(p.422)を解いてみる。

その他参考書籍

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

コード(BBEdit, Emacs)

newshound.c

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

int main(int argc, char *argv[])
{
  char *feeds[] = {"http://rss.cnn.com/rss/edition.rss",
                   "http://rss.cnn.com/rss/edition_world.rss",
                   "http://rss.cnn.com/rss/edition_africa.rss",
                   "http://rss.cnn.com/rss/edition_americas.rss",
                   "http://rss.cnn.com/rss/edition_asia.rss",
                   "http://rss.cnn.com/rss/edition_europe.rss",
                   "http://rss.cnn.com/rss/edition_meast.rss",
                   "http://rss.cnn.com/rss/edition_us.rss"};
  int times = 8;
  char *phrase = argv[1];
  for (int i = 0; i < times; i++) {
    char *var;
    asprintf(&var, "RSS_FEED=%s", feeds[i]);
    char *vars[] = {var, NULL};
    pid_t pid = fork();
    if (!pid)
      Stopif(execle("/usr/bin/python", "/usr/bin/python", "./rssgossip.py",
                    phrase, NULL, vars) == -1,
             free(var); exit(1),
             "スクリプトを実行できません: %s", strerror(errno));
  }
}

newshound_before.c

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

int main(int argc, char *argv[])
{
  char *feeds[] = {"http://rss.cnn.com/rss/edition.rss",
                   "http://rss.cnn.com/rss/edition_world.rss",
                   "http://rss.cnn.com/rss/edition_africa.rss",
                   "http://rss.cnn.com/rss/edition_americas.rss",
                   "http://rss.cnn.com/rss/edition_asia.rss",
                   "http://rss.cnn.com/rss/edition_europe.rss",
                   "http://rss.cnn.com/rss/edition_meast.rss",
                   "http://rss.cnn.com/rss/edition_us.rss"};
  int times = 8;
  char *phrase = argv[1];
  for (int i = 0; i < times; i++) {
    char *var;
    asprintf(&var, "RSS_FEED=%s", feeds[i]);
    char *vars[] = {var, NULL};
    Stopif(execle("/usr/bin/python", "/usr/bin/python", "./rssgossip.py",
                  phrase, NULL, vars) == -1,
           free(var); exit(1),
           "スクリプトを実行できません: %s", strerror(errno));
  }
}

入出力結果(Terminal)

$ ./newshound world > news.txt & ./newshound_before world > news_before.txt
[1] 57533
[1]+  Done                    ./newshound world > news.txt
$ diff news.txt  news_before.txt
1,3d0
< NASA's plan for an off-world colony
< World's most bizarre-looking coffins
< Rwandan fashion tailored for world
6,8d2
< CNN.com - World
< CNN.com - World
< World's most colorful landscapes
$ cat news.txt 
NASA's plan for an off-world colony
World's most bizarre-looking coffins
Rwandan fashion tailored for world
Is the world now a more deadly place?
Is the world now more deadly?
CNN.com - World
CNN.com - World
World's most colorful landscapes
$ cat news_before.txt 
Is the world now a more deadly place?
Is the world now more deadly?
$

0 コメント:

コメントを投稿