2014年4月13日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセスとシステムサービス)、コードマグネット(p.399)を解いてみる。

その他参考書籍

コードマグネット(p.399)

コード(BBEdit, Emacs)

sample399.c

#include <stdio.h>
#include <stdlib.h>
#include "sample399.h"

int main(int argc, char *argv[])
{
  char comment[80];
  char cmd[120];

  fgets(comment, 80, stdin);
  sprintf(cmd, "echo '%s %s' >> reports.log", comment, now());
  system(cmd);
  return (0);
}

libsample399.c

#include <time.h>
#include "sample399.h"

char *now()
{
  time_t t;
  time (&t);
  return asctime(localtime (&t));
}

Makefile

all:sample399

sample399: sample399.o libsample399.o
 cc -g -o sample399 sample399.o libsample399.o

sample399.o: sample399.c
 cc -c -o sample399.o sample399.c

libsample399.o:
 cc -c -o libsample399.o libsample399.c

clean:
 rm -f sample399

入出力結果(Terminal)

$ make && ./sample399
cc -c -o sample399.o sample399.c
cc -g -o sample399 sample399.o libsample399.o
テキストコメント
$ cat reports.log 
テキストコメント
 Sun Apr 11 13:48:38 2014

$ ./sample399
Hello, World!
$ cat reports.log 
テキストコメント
 Sun Apr 11 13:48:38 2014

Hello, World!
 Sun Apr 11 13:52:07 2014

$

0 コメント:

コメントを投稿