2014年6月10日火曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信: お話は楽しい)、エクササイズ(p.447)を解いてみる。

その他参考書籍

エクササイズ(p.447)

コード(BBEdit, Emacs)

news_opener.c

#include <stdio.h>
#include <unistd.h>

#include "error.h"
#include "open_url.h"

int main(int argc, char *argv[])
{
  char *phrase = argv[1];
  char *vars[] = {
    "RSS_FEED=http://rss.cnn.com/rss/edition.rss",
    NULL
  };
  int fd[2];
  if (pipe(fd) == -1)
    error("パイプを作成出来ません");
  pid_t pid = fork();
  if (pid == -1)
    error("プロセスをフォーク出来ません。");
  if (!pid) {
    close(fd[0]);
    if (dup2(fd[1], 1) == -1)
      error("標準出力をリダイレクト出来ません。");
    if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", "-u",
               phrase, NULL, vars) == -1)
      error("スクリプトを実行出来ません。");
  }
  if (dup2(fd[0], 0) == -1)
    error("標準入力にリダイレクト出来ません。");
  close(fd[1]);
  char line[255];
  while (fgets(line, 255, stdin)) {
    if (line[0] == '\t')
      open_url(line + 1);
  }

  return (0);
}

Makefile

CC=cc
CFLAGS = -g -Wall
SRC=news_opener.c error.c open_url.c
OBJ=news_opener.o error.o open_url.o

all: news_opener

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

news_opener.o: error.h open_url.h news_opener.c
 $(CC) $(CFLAGS) -c news_opener.c -o news_opener.o

error.o: error.h error.c
 $(CC) $(CFLAGS) -c error.c -o error.o

open_url.o: open_url.h open_url.c
 $(CC) $(CFLAGS) -c open_url.c -o open_url.o

clean:
 rm -rf news_opener news_opener error.o open_url.o

入出力結果(Terminal)

$ make
cc -g -Wall -c news_opener.c -o news_opener.o
cc -g -Wall -c open_url.c -o open_url.o
cc -g -Wall news_opener.o error.o open_url.o -o news_opener
$ ./news_opener 'Japan'
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
$ ./news_opener 'US'
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
sh: cmd: command not found
sh: line 1: x-www-browser: command not found
$ 

0 コメント:

コメントを投稿