開発環境
- 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, "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) { close(fd[0]); dup2(fd[1], 1); if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", "-u", phrase, NULL, vars) == -1) { error("スクリプトを実行できません"); } } close(fd[1]); dup2(fd[0], 0); char line[255]; for (; fgets(line, 255, stdin);) { printf("%s", line); if (line[0] == '\t') { open_url(line + 1); } } }
入出力結果(Terminal)
$ cc sample.c -o sample $ ./sample US US offers Libya as model for North Korea talks https://www.cnn.com/2018/04/30/asia/north-korea-bolton-libya-intl/index.html Sajid Javid named new UK home secretary after Windrush scandal https://www.cnn.com/2018/04/30/europe/sajid-javid-uk-new-home-secretary-intl/index.html The 57 most outlandish, outrageous and offensive lines from Trump's Michigan rally https://www.cnn.com/2018/04/30/politics/donald-trump-michigan-speech-annotation/index.html Iran accuses US of breaching nuclear deal as Macron fights to save it https://www.cnn.com/2018/04/30/asia/rouhani-macron-iran-deal-intl/index.html Migrant caravan reaches US-Mexico border https://www.cnn.com/2018/04/29/americas/migrant-caravan-us-border-crossing/index.html US ready to pull out of Iran deal, top diplomat says https://www.cnn.com/2018/04/29/politics/pompeo-iran-nuclear-deal-intl/index.html US mobile carriers agree to merge http://money.cnn.com/2018/04/29/news/companies/t-mobile-sprint-merger/index.html Trump slams 'lousy' London embassy again https://www.cnn.com/2018/04/29/politics/trump-london-embassy-lousy-intl/index.html 3 US Marines charged with rape https://www.cnn.com/2018/04/29/us/marines-charged-with-rape/index.html Superstitions of rich and famous https://www.cnn.com/style/article/strange-habits-creative-people-beyonce-benjamin-franklin/index.html Intelligence: US raises monitoring before Trump-Kim summit https://www.cnn.com/2018/04/29/politics/us-north-korea-intelligence/index.html Top Australian financial firm is in crisis http://money.cnn.com/2018/04/30/news/companies/amp-australia-scandal-catherine-brenner/index.html Who is Meghan Markle's future husband? https://www.cnn.com/2018/04/28/europe/prince-harry-chris-jackson-intl/index.html Miller and Sokolsky: As Pompeo's star rises, Haley and Kushner risk getting eclipsed https://www.cnn.com/2018/04/29/opinions/pompeo-star-rises-eclipsing-kushner-haley-opinion-miller/index.html Don Lincoln: This satellite gave us a new star map that is out of this world https://www.cnn.com/2018/04/28/opinions/gaia-star-map-opinion-lincoln/index.html $
0 コメント:
コメントを投稿