開発環境
- OS: macOS High Sierra - Apple
- Text Editor: Emacs
- コンパイラー: LLVM/Clang, GCC(gcc)
- プログラミング言語: C
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセスとシステムサービス - 限界を超える)、コードマグネット(p. 422)を取り組んでみる。
コードマグネット(p. 422)
コード
#include <errno.h> #include <stdio.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) { char *feeds[] = {"http://rss.cnn.com/rss/edition.rss", "http://rss.cnn.com/rss/edition_world.rss", "http://rss.cnn.com/rss/edition_asia.rss"}; int times = 3; char *phrase = argv[1]; for (int i = 0; i < times; i++) { puts(feeds[i]); char var[255]; sprintf(var, "RSS_FEED=%s", feeds[i]); char *vars[] = {var, NULL}; pid_t pid = fork(); if (pid == -1) { fprintf(stderr, "プロセスをフォークできません: %s\n", strerror(errno)); return 1; } if (!pid) { if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", phrase, NULL, vars) == -1) { fprintf(stderr, "スクリプトを実行できません: %s\n", strerror(errno)); return 1; } } } }
入出力結果(Terminal)
$ cc sample.c -o sample $ ./sample us > rss.txt $ cat rss.txt http://rss.cnn.com/rss/edition.rss http://rss.cnn.com/rss/edition_world.rss http://rss.cnn.com/rss/edition_asia.rss Princess Ayako introduces her future husband to Japan US trying to make friends, counter China with massive naval exercise Businesses have 22 Brexit questions no one can answer US moves to keep China Mobile out Trump admin. to reverse guidance on use of race in college admissions Airbus unveils new Beluga XL Justin Trudeau rejects groping claim from 2000 US moves to keep China Mobile out Australian scientists successfully map koala genome The darkest year in modern US history? British divers who found teens are pros at perilous rescues Wait for freedom will take dangerous toll on boys' health US Senate panel agrees with intel community: Putin was trying to help Trump Paul Callan: Michael Cohen's road ahead is perilous and uncertain $
0 コメント:
コメントを投稿