2018年4月27日金曜日

開発環境

Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセスとシステムサービス - 限界を超える)、エクササイズ(p. 417)を取り組んでみる。

エクササイズ(p. 417)

#include <errno.h>
#include <stdio.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_asia.rss"};
  int times = 3;
  char *phrase = argv[1];
  for (int i = 0; i < times; i++) {
    char var[255];
    sprintf(var, "RSS_FEED=%s", feeds[i]);
    char *vars[] = {var, NULL};
    if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", phrase,
               NULL, vars) == -1) {
      fprintf(stderr, "スクリプトを実行できません: %s\n", strerror(errno));
      return 1;
    }
  }
}

入出力結果(Terminal)

$ cc sample.c -o sample
$ ./sample 
Traceback (most recent call last):
  File "rssgossip.py", line 50, in <module>
    searcher = re.compile(args[0], re.IGNORECASE)
IndexError: list index out of range
$ ./sample US
US and Israeli officials intensify the drumbeat against Iran
Genealogy websites used to track down Golden State Killer suspect
US steps up surveillance of Iranian assets in Syria 
Pompeo confirmed as US secretary of state
Trump says he's been too busy to get Melania a birthday present
US billionaire bids $1.4B for Wembley Stadium
US-owned Christian radio station in Rwanda shut down for calling women evil
Golden State Killer suspect was a recluse, neighbors say 
Family faces fine to keep house painted like this
Autism prevalence increases: 1 in 59 US kids
The future of affordable housing
$ ./sample JP
$ ./sample Japan
$ echo $?
0
$ 

0 コメント:

コメントを投稿

Comments on Google+: