2014年4月20日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信)、コースを外れる(curl, p.449)を考えてみる。

その他参考書籍

コースを外れる(curl, p.449)

コード(BBEdit, Emacs)

get_p.c

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

#include "error.h"

int main(int argc, char *argv[])
{
  char *url = argv[1];
  char *element = argv[2];
  FILE *out_file;
  int fd[2];

  switch (argc) {
  case 3:
    out_file = stdout;
    break;
  case 4:
    out_file = fopen(argv[3], "w");
    break;
  default:
    puts("Usage: ./get_p <url> <element> [output]");
    exit (0);
  }
    
  if (pipe(fd) == -1)
    error("パイプを作成できません");

  pid_t pid = fork();
  if (pid == -1)
    error("プロセスをフォークできません");
  if (!pid) {
    close(fd[0]);
    dup2(fd[1], 1);
    if (execlp("curl", "curl", url, NULL) == -1)
      error("curlを実行出来ません");
  }
  dup2(fd[0], 0);
  close(fd[1]);
  char line[255];
  while (fgets(line, sizeof(line), stdin)) {
    if (strstr(line, element))
      fprintf(out_file, "%s", line);
  }
  return (0);
}

Makefile

all: get_p

get_p: get_p.c error.o
 cc -g -o get_p get_p.c error.o

error.o: error.c error.h
 cc -c -o error.o error.c

clean:
 rm -rf get_p

入出力結果(Terminal)

$ make
cc -g -o get_p get_p.c error.o open_url.o
$ ./get_p
Usage: ./get_p <url> <element> [output]
$ ./get_p http://www.example.com
Usage: ./get_p <url> <element> [output]
$ ./get_p http://www.example.com '<p>'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   2963      0 --:--:-- --:--:-- --:--:--  3009
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    <p><a href="http://www.iana.org/domains/example">More information...</a></p>
$ ./get_p http://www.example.com '<p>' temp.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   2794      0 --:--:-- --:--:-- --:--:--  2834
$ cat temp.txt
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    <p><a href="http://www.iana.org/domains/example">More information...</a></p>
$ curl http://www.example.com | grep '<p>'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   1855      0 --:--:-- --:--:-- --:--:--  1912
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    <p><a href="http://www.iana.org/domains/example">More information...</a></p>
$ rm temp.txt 
$ curl http://www.example.com | grep '<p>' > temp.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   1861      0 --:--:-- --:--:-- --:--:--  2009
$ cat temp.txt 
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    <p><a href="http://www.iana.org/domains/example">More information...</a></p>
$ ./get_p http://www.example.com '<h1>'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   3009      0 --:--:-- --:--:-- --:--:--  3143
    <h1>Example Domain</h1>
$ ./get_p http://www.example.com '<h1>' temp.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   2593      0 --:--:-- --:--:-- --:--:--  2623
$ cat temp.txt 
    <h1>Example Domain</h1>
$ curl http://www.example.com | grep '<h1>'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   1747      0 --:--:-- --:--:-- --:--:--  1814
    <h1>Example Domain</h1>
$ curl http://www.example.com | grep '<h1>' > temp.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   2138      0 --:--:-- --:--:-- --:--:--  2216
$ cat temp.txt
    <h1>Example Domain</h1>
$ 

0 コメント:

コメントを投稿