2018年7月3日火曜日

開発環境

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

エクササイズ(p. 417)

Makefile

CC = cc

all: sample run

sample: sample.c
 $(CC) sample.c -o sample

run: sample
 ./sample

コード

#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)

$ make
cc sample.c -o sample
./sample
Traceback (most recent call last):
  File "rssgossip.py", line 51, in <module>
    searcher = re.compile(args[0], re.IGNORECASE)
IndexError: list index out of range
make: *** [run] Error 1
$ ./sample Japan
$ ./sample JP
$ ./sample US
US intel agency believes Kim won't fully denuclearize
Cohen must be making Trump very nervous right about now
Princess Ayako introduces her future husband, a shipping employee
Canada hits $13B of US goods with tariffs
Australia banning plastic bags
Someone bought $1M in Toys 'R' Us toys right before it went out of business
Massive brawl at Australia-Philippines game
$ 

0 コメント:

コメントを投稿