世界史上最高のプログラマのコードを読んでみる。

いろいろなプロジェクトを歩き渡って来た。相対したコードも様々。
機能分割がうまくできてるもの、理解しやすいお手本になりそうなものから、応急処理的な
つぎはぎだらけもの、最長不倒関数を遙かに上を行くようなとんでもコードまで。
ここはあのプロジェクトのコードの方がまともだなと思って、いいところは真似してきた。
悪いものは、できるものはリファクタリングしたりしたこともあるけど、いかんせん元のコードが
理解不能な場合はどうしても塩漬けのままとなる。
せめて、自分のところぐらいは・・・と書いてきたつもり。


しかし、未だに「美しい」と言われる絶対的なコードには出会ったことがない。
幸せの青い鳥を追い求めているのだろうか。


OSSならお手本としてどんなものがあるだろうと思って探してみたら以下の記事が見つかった。
Aaron Swartz氏「みんなそろそろD.J. Bernsteinが世界史上最高のプログラマだというのを思い出すべき」
D.J.Bernsteinが書いたコードには以下のものがあるようだ。

djbdnsを読もうかなとおもったけど、こっちの2つの方が興味があるので、この2つを読んでみることにする。
ucspi-tcpは、inetd/xinetdのようなスーパーサーバーでTCP クライアント・サーバ・アプリケーションを
構築するための使いやすいコマンドラインツール。
daemontools は、プロセスをデーモン化するのに必要なプロセス、ログ管理するツール。


ソースの入手

ucspi-tcpのソース一覧
[oc@centos5 djb]$ wget http://cr.yp.to/ucspi-tcp/ucspi-tcp-0.88.tar.gz
[oc@centos5 djb]$ tar zxvf ucspi-tcp-0.88.tar.gz
[oc@centos5 ucspi-tcp-0.88]$ ls
CHANGES        case_diffs.c    env.h            iopause.c        rules.c          stralloc_cat.c   trylsock.c
FILES          cdb.c           error.c          iopause.h1       rules.h          stralloc_catb.c  trypoll.c
Makefile       cdb.h           error.h          iopause.h2       scan.h           stralloc_cats.c  trysgact.c
README         cdb_hash.c      error_str.c      ip4.h            scan_ulong.c     stralloc_copy.c  trysgprm.c
SYSDEPS        cdb_make.c      exit.h           ip4_fmt.c        seek.h           stralloc_eady.c  tryshsgr.c
TARGETS        cdb_make.h      fd.h             ip4_scan.c       seek_set.c       stralloc_opyb.c  trysysel.c
TODO           chkshsgr.c      fd_copy.c        mconnect-io.c    select.h1        stralloc_opys.c  tryulong32.c
VERSION        choose.sh       fd_move.c        mconnect.sh      select.h2        stralloc_pend.c  tryulong64.c
addcr.c        commands.c      find-systype.sh  ndelay.h         sgetopt.c        strerr.h         tryvfork.c
alloc.c        commands.h      finger@.sh       ndelay_off.c     sgetopt.h        strerr_die.c     trywaitp.c
alloc.h        conf-cc         fixcrio.c        ndelay_on.c      sig.c            strerr_sys.c     uint16.h
alloc_re.c     conf-home       fmt.h            open.h           sig.h            subgetopt.c      uint16_pack.c
argv0.c        conf-ld         fmt_ulong.c      open_read.c      sig_block.c      subgetopt.h      uint16_unpack.c
auto-str.c     date@.sh        fork.h1          open_trunc.c     sig_catch.c      tai.h            uint32.h1
auto_home.h    delcr.c         fork.h2          open_write.c     sig_pause.c      tai_pack.c       uint32.h2
buffer.c       dns.h           gen_alloc.h      openreadclose.c  socket.h         taia.h           uint32_pack.c
buffer.h       dns_dfd.c       gen_allocdefs.h  openreadclose.h  socket_accept.c  taia_add.c       uint32_unpack.c
buffer_0.c     dns_domain.c    getln.c          pathexec.h       socket_bind.c    taia_approx.c    uint64.h1
buffer_1.c     dns_dtda.c      getln.h          pathexec_env.c   socket_conn.c    taia_frac.c      uint64.h2
buffer_2.c     dns_ip.c        getln2.c         pathexec_run.c   socket_delay.c   taia_less.c      wait.h
buffer_copy.c  dns_ipq.c       hassgact.h1      prot.c           socket_listen.c  taia_now.c       wait_nohang.c
buffer_get.c   dns_name.c      hassgact.h2      prot.h           socket_local.c   taia_pack.c      wait_pid.c
buffer_put.c   dns_nd.c        hassgprm.h1      rblsmtpd.c       socket_opts.c    taia_sub.c       warn-auto.sh
byte.h         dns_packet.c    hassgprm.h2      readclose.c      socket_remote.c  taia_uint.c      warn-shsgr
byte_chr.c     dns_random.c    hasshsgr.h1      readclose.h      socket_tcp.c     tcpcat.sh        who@.sh
byte_copy.c    dns_rcip.c      hasshsgr.h2      readwrite.h      socket_udp.c     tcpclient.c      x86cpuid.c
byte_cr.c      dns_rcrw.c      haswaitp.h1      recordio.c       str.h            tcprules.c
byte_diff.c    dns_resolve.c   haswaitp.h2      remoteinfo.c     str_chr.c        tcprulescheck.c
byte_rchr.c    dns_sortip.c    hier.c           remoteinfo.h     str_diff.c       tcpserver.c
byte_zero.c    dns_transmit.c  http@.sh         rts.exp          str_len.c        timeoutconn.c
case.h         dns_txt.c       install.c        rts.sh           str_start.c      timeoutconn.h
case_diffb.c   env.c           instcheck.c      rts.tests        stralloc.h       trycpp.c
daemontoolsのソース一覧
[oc@centos5 djb]$ wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
[oc@centos5 djb]$ tar zxvf daemontools-0.76.tar.gz
[oc@centos5 djb]$ cd admin/
[oc@centos5 admin]$ ls -R
.:
daemontools-0.76/

./daemontools-0.76:
package/  src/

./daemontools-0.76/package:
README        boot.rclocal  compile*  install*  run.inittab*  sharing
boot.inittab  commands      files     run*      run.rclocal*  upgrade*

./daemontools-0.76/src:
CHANGES         conf-cc          gen_allocdefs.h  ndelay_on.c      select.h1        strerr.h       taia_sub.c
Makefile        conf-ld          hasflock.h1      open.h           select.h2        strerr_die.c   taia_uint.c
TODO            deepsleep.c      hasflock.h2      open_append.c    setlock.c        strerr_sys.c   timestamp.c
alloc.c         deepsleep.h      hasmkffo.h1      open_read.c      setuidgid.c      subgetopt.c    timestamp.h
alloc.h         direntry.h1      hasmkffo.h2      open_trunc.c     sgetopt.c        subgetopt.h    trycpp.c
alloc_re.c      direntry.h2      hassgact.h1      open_write.c     sgetopt.h        supervise.c    trydrent.c
buffer.c        env.c            hassgact.h2      openreadclose.c  sig.c            svc.c          tryflock.c
buffer.h        env.h            hassgprm.h1      openreadclose.h  sig.h            svok.c         trymkffo.c
buffer_0.c      envdir.c         hassgprm.h2      pathexec.h       sig_block.c      svscan.c       trypoll.c
buffer_1.c      envuidgid.c      hasshsgr.h1      pathexec_env.c   sig_catch.c      svscanboot.sh  trysgact.c
buffer_2.c      error.c          hasshsgr.h2      pathexec_run.c   sig_pause.c      svstat.c       trysgprm.c
buffer_get.c    error.h          haswaitp.h1      pgrphack.c       softlimit.c      tai.h          tryshsgr.c
buffer_put.c    error_str.c      haswaitp.h2      print-ar.sh      str.h            tai64n.c       trysysel.c
buffer_read.c   fd.h             iopause.c        print-cc.sh      str_chr.c        tai64nlocal.c  tryulong64.c
buffer_write.c  fd_copy.c        iopause.h1       print-ld.sh      str_diff.c       tai_now.c      trywaitp.c
byte.h          fd_move.c        iopause.h2       prot.c           str_len.c        tai_pack.c     uint64.h1
byte_chr.c      fghack.c         lock.h           prot.h           str_start.c      tai_sub.c      uint64.h2
byte_copy.c     fifo.c           lock_ex.c        readclose.c      stralloc.h       tai_unpack.c   wait.h
byte_cr.c       fifo.h           lock_exnb.c      readclose.h      stralloc_cat.c   taia.h         wait_nohang.c
byte_diff.c     find-systype.sh  match.c          readproctitle.c  stralloc_catb.c  taia_add.c     wait_pid.c
byte_rchr.c     fmt.h            match.h          rts.tests        stralloc_cats.c  taia_approx.c  warn-auto.sh
chkshsgr.c      fmt_uint.c       matchtest.c      scan.h           stralloc_eady.c  taia_frac.c    warn-shsgr
choose.sh       fmt_uint0.c      multilog.c       scan_ulong.c     stralloc_opyb.c  taia_less.c    x86cpuid.c
coe.c           fmt_ulong.c      ndelay.h         seek.h           stralloc_opys.c  taia_now.c
coe.h           gen_alloc.h      ndelay_off.c     seek_set.c       stralloc_pend.c  taia_pack.c