2014年9月22日月曜日

開発環境

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

その他参考書籍

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

コード(BBEdit, Emacs)

newshound.c

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.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 = 8;
  char *phrase = argv[1];
  int i;

  for (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) {
      if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", phrase,
                 NULL, vars) == -1) {
        fprintf(stderr, "スクリプトを実行できません:%s\n", strerror(errno));
        return (1);
      }
    }
  }

  return (0);
}

Makefile

CC=cc
CFLAGS=-g -Wall
SRC=newshound.c
OBJ=newshound.o

all: newshound

newshound: $(OBJ)
 $(CC) $(CFLAGS) $(OBJ) -o newshound

newshound.o: newshound.c
 $(CC) $(CFLAGS) -c newshound.c -o newshound.o

clear:
 rm -rf newshound $(OBJ)

入出力結果(Terminal)

$ mv newshound old_newshound
$ make
cc -g -Wall -c newshound.c -o newshound.o
cc -g -Wall newshound.o -o newshound
$ ./old_newshound 'world'
Zakaria: Is the world going nuts?
World's most frustrating objects
World's strangest-looking plane turns 20
Are these the world's sexiest accents?
Can the world stop Ebola?
World's 15 best rivers for travelers
$ ./newshound 'world'
Building the world's tallest tower
Post-9/11 world of threats
Spreading ethio-jazz to the world
The world of Somalian literature
World's biggest record collection
Can this school save the world?
World-class Caribbean carnival
The world's sexiest accents
And the world's busiest airport is ...
CNN.com - World
CNN.com - World
What's the world's sexiest accent?
Zakaria: Is the world going nuts?
World's most frustrating objects
World's strangest-looking plane turns 20
Are these the world's sexiest accents?
Can the world stop Ebola?
World's 15 best rivers for travelers
$ 

0 コメント:

コメントを投稿