2018年4月28日土曜日

開発環境

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

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

#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};
    pid_t pid = fork();
    if (pid == -1) {
      fprintf(stderr, "プロセスをフォークできません: %s\n", strerror(errno));
      return 1;
    }
    if (!pid) {
      puts(feeds[i]);
      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  > rssgossip.txt 2> error.txt
$ cat rssgossip.txt 
http://rss.cnn.com/rss/edition.rss
http://rss.cnn.com/rss/edition_world.rss
http://rss.cnn.com/rss/edition_asia.rss
$ cat error.txt 
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
Traceback (most recent call last):
Traceback (most recent call last):
  File "rssgossip.py", line 50, in <module>
  File "rssgossip.py", line 50, in <module>
    searcher = re.compile(args[0], re.IGNORECASE)
    IndexError: searcher = re.compile(args[0], re.IGNORECASE)
list index out of range
IndexError: list index out of range
$ ./sample US  > rssgossip.txt 2> error.txt
bash: rssgossip.txt: cannot overwrite existing file
$ ./sample US  >| rssgossip.txt 2>| error.txt
$ cat rssgossip.txt 
Sexual abuse survivor wants to fix a broken system
It's possible that after decades of mutual distrust, peace is a realistic prospect. But experts are skeptical
US flies bombers over South China Sea
N Korea's nuclear test caused collapse: study
Ignored, humiliated: How Japan is accused of failing survivors of sexual abuse
Senior Vatican defense says to drop abuse charges
Australia's forgotten indigenous World War II veterans
Exclusive video tells of Rohingya massacre
Why does North Korea hate the US?
From above: The Rohingya mass exodus
Russians 'followed up' on Trump Tower meeting
US flies bombers over South China Sea
Who is Meghan Markle's future husband?
Donald Trump may have just had a breakthrough
E. coli outbreak expands in US; still no source
Mamma Mia! ABBA is making new music
Airbus can't get enough engines for its jets
Serious sailing, serious fun
Mushrooms are the latest superfood
Jack Nicklaus' secret stem cell therapy
'Mass firing' at conservative US blog
$ cat error.txt 
$ ./sample JP  >| rssgossip.txt 2>| error.txt
$ cat rssgossip.txt 
$ cat error.txt 
$ ./sample japan  >| rssgossip.txt 2>| error.txt
$ cat rssgossip.txt 
Ignored, humiliated: How Japan is accused of failing survivors of sexual abuse
Japan objects to the dessert South Korea is serving Kim Jong Un
Mud near Japanese island is a 'game changer'
8 of Japan's best foodie destinations
$ cat error.txt 
$ 

0 コメント:

コメントを投稿