2015年2月25日水曜日

開発環境

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

その他参考書籍

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

コード(BBEdit, Emacs)

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.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"};
  FILE *out = fopen("stories.txt", "w");
  Stopif(!out, return 1, "sotires.txtを開けません: %s", strerror(errno));  
  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();
    Stopif(pid == -1, exit(1), "プロセスをフォークできません: %s",
           strerror(errno));
    Stopif(dup2(fileno(out), 1) == -1, exit(1),
           "標準出力をリダイレクトできません: %s", strerror(errno));
    if (!pid) {
      Stopif(execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py",
                    phrase, NULL, vars) == -1,
             exit(1), "スクリプトを実行できません: %s", strerror(errno))
        }
    
  }
}

入出力結果(Terminal)

$ make newshound2
clang ...
$ ./newshound2 US
$ cat stories.txt 
U.S. missionary accused of aiding rebels
Lord Vader's pact with Mickey Mouse
Argentine President accused of bombing cover-up
15 must-try restaurants in 2015
Spain arrests 4 people suspected of recruiting for ISIS 
Thousands march for Putin
UK jets intercept Russian aircraft
LGBT group gets VIP status before pope
Sebastien Ogier: Rally driver leaves rivals in the dust 
Old, rusty cars expected to sell for $20 million
U.S. court: Palestinians 'must pay' for attacks
Ousted Yemeni President wins support
Jerusalem's mayor makes arrest
Beyond Muscat: 10 reasons to visit Oman
Obama's use of rhetoric in the fight vs. ISIS
30 Shiite Muslim men kidnapped
Suspect in school massacre arrested
Yellow dust blankets Seoul
Australia to strengthen terror laws 
Abused maid: I can forgive my former employer
160 Boko Haram suspects arrested
Is ISIS shifting its focus to Libya?
Teacher turns $10 into booming business
Tech genius transforms people's lives
2 suicide bombers hit bus depot
$3M reward for Russian hacker
Palestinians 'must pay' for attacks 
New Cosby accuser: 'Remember me?'
Who wins battle between octopus and crab?
Octopus leaps out of water, grabs crab
Suicide bombers attack Nigeria bus station
More than 20 women have come forward to accuse Cosby
Cosby accusations eerily similar
Who are Cosby's accusers?
Suspect in Las Vegas shooting due in court
The inside story of the famous Iwo Jima photo
More than $28K raised for hero dog hurt in house fire
Real estate agent lives in Witch's House
5 terror groups making vicious headlines
Suspect in Pakistan school massacre arrested
Octopus leaps out of water, nabs crab
An unusual semisecret British trial
Bus station bomb kills at least 13
Jerusalem's mayor arrests suspected attacker
Teacher turns $10 into booming business
Backstage at a traveling circus
Spectacular ancient city deserted and mysterious
'It just went ballistic': Shark attacks teenager 
Is this the site of Jesus' trial? 
$ 

0 コメント:

コメントを投稿