2015年5月30日土曜日

開発環境

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

その他参考書籍

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

コード(BBEdit, Emacs)

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.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 = 3;
  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 == -1) {
      fprintf(stderr, "プロセスをフォークできません: %s\n", strerror(errno));
      return 1;
    }
    if (!pid)
      if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", phrase,
                 NULL, vars) == -1) {
        fprintf(stderr, "スクリプトを実行できません: %s\n", strerror(errno));
        return 1;
      }
  }
}

入出力結果(Terminal)

$ make newshound
clang ...
$ ./newshound US > newshound.txt &
[1] 16785
$ fg
bash: fg: job has terminated
[1]+  Done                    ./newshound US > newshound.txt
$ cat newshound.txt
Five things Muhammadu Buhari must tackle now that he's Nigeria's president 
Social media users tell #BuhariFixThis
Air France flight's brush with volcano
Ancient religious sites discovered in Ethiopia
One of Nigeria's most dangerous jobs
Struck by wanderlust? Here's 7 of the best African holidays
Five things Buhari must tackle
Egyptian mummies reveal ancient scam industry
Exclusive look in China's space city
Source: Hastert paid hush money
Confused by FIFA scandal? This will help.
Exclusive interview: Star biker's growing pains
20 of the world's top museums
HRW: Migrant rights must be priority
UN: Myanmar must address migrant crisis
The homeowners who refused to budge
Stunning images: Chinese houses laid bare
ISIS mom would face 'full severity' of Australian law
Russia's military exercise surprises
Confused by FIFA scandal? This will help
Putin accuses U.S. of 'illegally persecuting people'
The homeowners who refuse to budge
Top 20 museums around the world
Israel cries foul over FIFA suspension talk
Suspected spy pigeon nabbed 
Police: British TV star abused 22 students 
On TV, a new Jesus comes into view 
Spectacular ancient city deserted and mysterious
'It just went ballistic': Shark attacks teenager 
Is this the site of Jesus' trial? 
$ 

0 コメント:

コメントを投稿

Comments on Google+: