Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- LLVM/Clang (コンパイラ, Xcode - Apple)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信: お話は楽しい)、エクササイズ(p.447)を解いてみる。
その他参考書籍
- 21st Century C: C Tips from the New School
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
エクササイズ(p.447)
コード(BBEdit, Emacs)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stopif.h>
void open_url(char *url) {
char *launch;
asprintf(&launch, "cmd /c start %s", url);
system(launch);
asprintf(&launch, "x-www-browser '%s' &", url);
system(launch);
asprintf(&launch, "open '%s'", url);
system(launch);
}
int main(int argc, char **argv) {
int fd[2];
Stopif(pipe(fd) == -1, exit(1), "パイプを作成できません");
pid_t pid = fork();
Stopif(pid == -1, exit(1), "プロセスをフォークできません");
if (!pid) {
close(fd[0]);
dup2(fd[1], 1);
char *phrase = argv[1];
char *vars[] = {"RSS_FEED=http://rss.cnn.com/rss/edition.rss", NULL};
Stopif(execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", "-u",
phrase, NULL, vars) == -1,
exit(1),
"スクリプトを実行できません");
}
dup2(fd[0], 0);
close(fd[1]);
char line[255];
while (fgets(line, 255, stdin)) {
printf("%s\n", line);
if (line[0] == '\t') open_url(line + 1);
}
}
入出力結果(Terminal)
$ make news_opener clang ... $ ./news_opener US Eritrea's vast, vile 'abuses' http://edition.cnn.com/2015/06/09/world/eritrea-human-rights-violations/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found White House rooms evacuated http://edition.cnn.com/2015/06/09/politics/hearing-evacuated-bomb-threat-tsa/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Meet the female Muslim wrestlers http://edition.cnn.com/2015/06/09/sport/european-games-azerbaijan-wrestling/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Russia's stars eye gold at beach soccer http://edition.cnn.com/2015/06/03/sport/baku-azerbaijan-european-games-beach-soccer/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Maths genius' mansion: Yours for $23M http://edition.cnn.com/2015/06/04/travel/curve-house-23-million-integral-toronto/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Business advice from Jay-Z, Beyonce http://edition.cnn.com/2015/06/09/world/how-to-start-your-own-charity/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Thousands rescued in 'migrant season' http://edition.cnn.com/video/data/2.0/video/tv/2015/06/09/rescued-in-migrant-season-robertson-pkg.cnn.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Could Russia, Qatar 'lose World Cup rights'? http://edition.cnn.com/2015/06/08/football/fifa-scandal/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found What weaponry is ISIS using? http://edition.cnn.com/2015/06/09/opinions/isis-iraq-weapons/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Why Jerusalem is such a sticky issue http://edition.cnn.com/2015/06/08/opinions/ghitis-supreme-court-jerusalem/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found The homeowners who refused to budge http://edition.cnn.com/2015/05/19/asia/gallery/china-nail-houses/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found The most ambitious luxury home ever? http://edition.cnn.com/2015/06/03/americas/architect-3d-prints-luxury-estate/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found Stunning images: Chinese houses laid bare http://edition.cnn.com/2015/05/18/asia/china-families-belongings/index.html?eref=edition sh: cmd: command not found sh: line 1: x-www-browser: command not found $
1 コメント :
Already many web I have read and I die, but not all web can make me amazed awe as read the news of your website
コメントを投稿