開発環境
- OS: macOS High Sierra - Apple
- Text Editor: Emacs
- コンパイラー: LLVM/Clang, GCC(gcc)
- プログラミング言語: C
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信 - お話は楽しい)、エクササイズ(p. 447)を取り組んでみる。
エクササイズ(p. 447)
コード
#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> void error(char *msg) { fprintf(stderr, "%s: %s\n", msg, strerror(errno)); exit(1); } void open_url(char *url) { char launch[255]; sprintf(launch, "cmd /c start %s", url); system(launch); sprintf(launch, "x-www-browser %s' &", url); system(launch); sprintf(launch, "open '%s'", url); system(launch); } 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) { dup2(fd[1], fileno(stdout)); close(fd[0]); if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", "-u", phrase, NULL, vars) == -1) { error("スクリプトを実行できません"); } } dup2(fd[0], fileno(stdin)); close(fd[1]); char line[255]; while (fgets(line, 255, stdin)) { printf("%s", line); if (line[0] == '\t') { open_url(line + 1); } } }
入出力結果(Terminal)
$ cc sample.c -o news_opener $ ./news_opener us China says the US has started 'the biggest trade war' in history http://money.cnn.com/2018/07/06/news/economy/us-china-trade-war-tariffs/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file Pruitt resigns as US environment chief amid ethics scandals https://www.cnn.com/2018/07/05/politics/scott-pruitt-epa-resigns/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file US pushes back on Iranian officials as spat over oil exports continues https://www.cnn.com/2018/07/04/middleeast/iran-guards-rouhani-intl/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file Pompeo seeking progress in North Korea as US struggles to craft strategy https://www.cnn.com/2018/07/05/politics/trump-pompeo-north-korea-pressure/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file Trump's 'get off my lawn' response to the US immigration crisis https://www.cnn.com/2018/07/05/politics/immigration-separation-border-trump/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file Brexit nightmare coming true for businesses http://money.cnn.com/2018/07/05/news/economy/brexit-business-nightmare/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file DNA tests to reunite migrants separated at US border https://www.cnn.com/2018/07/05/politics/dna-testing-migrant-family-separation/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file Power photos capture world's sustainability crisis https://www.cnn.com/style/article/prix-pictet-10th-anniversary/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file ZTE names new CEO as part of deal with US http://money.cnn.com/2018/07/05/technology/zte-new-ceo/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file Lions kill suspected rhino poachers who sneaked onto game reserve https://www.cnn.com/2018/07/05/world/south-africa-poachers-killed-trnd/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file World's largest waterfall cluster https://www.cnn.com/travel/article/huangguoshu-waterfalls-guizhou-china/index.html sh: cmd: command not found sh: x-www-browser: command not found sh: -c: line 1: unexpected EOF while looking for matching `'' sh: -c: line 2: syntax error: unexpected end of file $
0 コメント:
コメントを投稿