cleanup.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <html><head><title>toybox cleanup</title></head>
  2. <!--#include file="header.html" -->
  3. <h1>Index</h1>
  4. <ul>
  5. <li><a href=#intro>Introduction</a></li>
  6. <li><a href=#advice>Advice</a></li>
  7. <li>Commands:</li>
  8. <ul>
  9. <li><a href=#uuencode>uuencode</a></li>
  10. <li><a href=#uudecode>uudecode</a></li>
  11. <li><a href=#ifconfig>ifconfig</a></li>
  12. <li><a href=#stat>stat</a></li>
  13. </ul>
  14. </ul>
  15. <hr>
  16. <a name=intro />
  17. <h1>Cleaning up the toybox code.</h1>
  18. <p>Toybox <a href=http://landley.net/notes.html#31-03-2013>hasn't always</a>
  19. taken proper advantage of external contributions</a>.
  20. Lots of people want to help, but their contributions languish out of tree
  21. or in the "pending" directory, awaiting cleanup.</p>
  22. <p>Toybox's design goals require simpler, tighter, and more explicit code
  23. than most other implementations, among other reasons to allow better security
  24. auditing. Writing "another" implementation of standard command line tools
  25. isn't very interesting, they should be _better_ implementations.
  26. Unfortunately, this means existing, working commands often take more effort to
  27. clean up to Toybox's standards than writing a new one from scratch, not
  28. because they don't work, but because we aim for an unusual level of polish.</p>
  29. <p>In hopes of teaching more people how to do this
  30. cleanup work, I've started breaking cleanup changes into smaller chunks and
  31. posting explanations of each change to the mailing list.
  32. Below are indexes of such cleanup series. Each commit and post are meant to
  33. be read together: each description explains what the corresponding patch
  34. was trying to accomplish.</p>
  35. <p>Line/byte totals of completed series are given for scale, but the point
  36. of this work is simplicity and compactness, not size per se.</p>
  37. <p>(Note: mercurial's web viewer doesn't follow renames, so although each
  38. command name links to a commit list with the bulk of the changes, it may
  39. not include the final version of each file moved from the "pending"
  40. directory to its final location. The summaries link the initial and cleaned
  41. versions of each file when giving line counts.)</p>
  42. <hr>
  43. <a name=advice />
  44. <h1>General advice and/or policy.</h1>
  45. <p>The <a href=design.html>design of toybox</a> page and the coding
  46. style section at the start of the <a href=code.html>source code walkthrough</a>
  47. don't cover everything. Here are some
  48. links to mailing list messages that cover various programming topics
  49. not directly related to a specific cleanup series:</p>
  50. <ul>
  51. <li><a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000850.html>Error messages and internationalization.</a></li>
  52. <li><a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000891.html>Why not "const"?</a> (Exception: global variables
  53. outside of GLOBALS can be static const, to go in rodata instead of data.
  54. This means the pages can be shared between instances.)</li>
  55. <li><a href=http://lkml.indiana.edu/hypermail/linux/kernel/1308.3/03890.html>Why not "bool"?</a> (explanation from Linus Torvalds)</li>
  56. <li><a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000893.html>Why not to check in debug code.</a></li>
  57. <li><a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-June/001044.html>Relationship between /proc and /sys</a> (/proc isn't obsolete and /sys is an ABI)</li>
  58. <li>"Hiding numbers that are used just once into defines to put them out of
  59. sight does not really help readability."</a> -
  60. <a href=http://lkml.iu.edu/hypermail/linux/kernel/1407.1/00299.html>Pavel
  61. Machek</a></li>
  62. <li>"Infrastructure in search of a user" is a bad thing, so don't put code
  63. in lib/ that doesn't already have at least two users. Don't preemptively
  64. factor stuff out, it's easy enough to rewrite it uin future if it needs
  65. to change. The "extreme programming" fad called this "You Ain't Gonna
  66. Need It" (while inexplicably screaming at cans of Mountain Dew, back in the
  67. 90's). Here's <a href=https://lwn.net/Articles/683745/>Linus Torvalds' take</a>.</li>
  68. </ul>
  69. <hr>
  70. <a name="uuencode"><h1><a href=/hg/toybox/log/900/toys/pending/uuencode.c>uuencode</a></h1>
  71. <p>This is an example of cleaning up something most projects would be quite
  72. happy with. The initial submission of uuencode and uudecode was high
  73. quality code, written by a seasoned developer who did an excellent
  74. job, but it was still possible to shrink the result almost by half:</p>
  75. <ul>
  76. <li>old total: <a href=/hg/toybox/file/828/toys/pending/uuencode.c>116 lines (2743
  77. bytes) in 7 functions</a></li>
  78. <li>new total: <a href=/hg/toybox/file/841/toys/posix/uuencode.c>67 lines (1440
  79. bytes) in 1 function</a></li>
  80. </ul>
  81. <ul>
  82. <li>commit: <a href=/hg/toybox/rev/830>830</a>: first pass, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000904.html>part 1</a>,
  83. <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000903.html>part 2</a></li>
  84. <li>commit: <a href=/hg/toybox/rev/831>831</a>,
  85. second pass, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000919.html>part 3</a></li>
  86. <li>commit: <a href=/hg/toybox/rev/837>837</a>,
  87. description: fix test suite.</li>
  88. <li>commit: <a href=/hg/toybox/rev/853>853</a>, description: bugfix.</li>
  89. </ul>
  90. <p>Status: COMPLETE</p>
  91. <a name="uudecode"><h1><a href=/hg/toybox/log/900/toys/pending/uudecode.c>uudecode</a></h1>
  92. <p>The uudecode cleanup was my second "explain as I go along" cleanup,
  93. and I tried to do it in smaller stages so it was easier to see what
  94. changed from the diff:</p>
  95. <ul>
  96. <li>old: <a href=/hg/toybox/file/828/toys/pending/uudecode.c>175
  97. lines (4534 bytes) in 9 functions</a></li>
  98. <li>new: <a href=/hg/toybox/file/840/toys/posix/uudecode.c>107 lines
  99. (2300 bytes) in 1 function</a></li>
  100. </ul>
  101. <ul>
  102. <li>commit: <a href=/hg/toybox/rev/833>833</a>,
  103. description: preparatory adjustments to test suite.</li>
  104. <li>commit: <a href=/hg/toybox/rev/835>835</a>,
  105. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2014-January/001532.html>Redo command line parsing, redo parsing loop.</a></li>
  106. <li>commit: <a href=/hg/toybox/rev/838>838</a>,
  107. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2014-January/001533.html>Redo b64_1byte, b64_4bytes, and uu_line()</a></li>
  108. <li>commit: <a href=/hg/toybox/rev/839>839</a>,
  109. description: todo</a></li>
  110. <li>commit: <a href=/hg/toybox/rev/840>840</a>,
  111. description: todo (finish, move pending->posix, default y)</a></li>
  112. </ul>
  113. <p>Status: COMPLETE</p>
  114. <a name=ifconfig>
  115. <h1><a href=/hg/toybox/log/tip/toys/pending/ifconfig.c>ifconfig</a></h1>
  116. <p>This series describes a thorough cleanup that took a while to do.</p>
  117. <p>When ifconfig was submitted, it touched a half-dozen files. I glued it
  118. together into a single self-contained file, which needed a lot of
  119. cleanup. The final version is about 1/3 the size of the original.</p>
  120. <ul>
  121. <li>old total: <a href=/hg/toybox/file/841/toys/pending/ifconfig.c>1504 lines (44268 bytes) in 38 functions</a></li>
  122. <li>new total: <a href=/hg/toybox/file/1133/toys/other/ifconfig.c>521 lines (15963 bytes) in 4 functions</a></li>
  123. </ul>
  124. <p>This was the first command I started cleaning up with the intent of
  125. describing the process, and especially the first few entries in this
  126. series describe many of the low hanging fruit techniques used to clean
  127. up a codebase.</p>
  128. <ul>
  129. <li>commit: <a href=/hg/toybox/rev/843>843</a>, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000882.html>Infrastructure in search of a user, code proximity,
  130. unnecessary #defines, shorter code is easier to read.</a></li>
  131. <li>commit: <a href=/hg/toybox/rev/844>844</a>,
  132. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000881.html>Headers, replacing repeated code with loops,
  133. logical operators guaranteed to return 0 or 1, math on string constants,
  134. removing unnecessary variables and tests.</a></li>
  135. <li>commit: <a href=/hg/toybox/rev/852>852</a>,
  136. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000921.html>hg commit numbers, documenting the obvious, ordering
  137. to avoid prototypes, returning void, collate local declarations,
  138. use error_exit(), unnecessary parentheses, inline to remove variables/functions
  139. used only once, using *var instead of var[0], unnecessary typecasts,
  140. xprintf("\n") vs xputc('\n')</a></li>
  141. <li>commit: <a href=/hg/toybox/rev/856>856</a>,
  142. description: one line portability fix from Isaac Dunham</li>
  143. <li>commit: <a href=/hg/toybox/rev/861>861</a>
  144. and <a href=/hg/toybox/rev/863>863</a>,
  145. description:
  146. <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000910.html>Help
  147. infrastructure cleanup from Isaac Dunham</a>
  148. (which I mis-applied and then <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000920.html>fixed plus some whitespace changes</a>)</li>
  149. <li>commit: <a href=/hg/toybox/rev/862>862</a>, description:
  150. <a href=http://lists.landley.net/pipermail/toybox-landley.net/2014-January/001525.html>remove unused headers and function, replace local buffer with toybuf, perror_exit(), avoid unnecessary assignment.</a></li>
  151. <li>commit: <a href=/hg/toybox/rev/864>864</a>, description:
  152. <a href=http://lists.landley.net/pipermail/toybox-landley.net/2014-January/001526.html>use common linked list functions, inline set_data, add xioctl, clean up error messages, whitespace and comment tweaks, remove NOP return statements</a></li>
  153. <li>commit: <a href=/hg/toybox/rev/866>866</a>, description:
  154. <a href=http://lists.landley.net/pipermail/toybox-landley.net/2014-January/001528.html>move standalone globals into GLOBALS() block, collate structs into
  155. iface_list. Inline/rewrite/remove field_format, iface_flags_str,
  156. omit_whitespace(), print_iface_flags(), print_media(), get_ifconfig_info(),
  157. and clear_list(). Merge duplicate function
  158. calls. Show get_proc_info() version field can't matter in 2.6 or newer kernel,
  159. and that SIOCGIFMAP has been there since 1994 so doesn't need an #ifdef.
  160. Loop simplification in readconf() and show_iface().</a></li>
  161. <li>commit: <a href=/hg/toybox/rev/869>869</a> and <a href=/hg/toybox/rev/870>870</a>,
  162. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000928.html>869:
  163. reorder to eliminate prototypes, put command_main() at end of file,
  164. 870: long repeated variable prefixes, replacing struct+sscanf()+printf with a
  165. loop and a table (from iface_list, get_proc_info(), display_ifconfig()),
  166. use lib/xwrap.c functions to return void, why xstrcpy() fails closed,
  167. (functional comment: why multicast failed, CSLIP obsolecense), not being
  168. _too_ clever.</a></li>
  169. <li>commit: <a href=/hg/toybox/rev/878>878</a> and <a href=/hg/toybox/rev/879>879</a>:
  170. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000946.html>878: add xsocket(), free(NULL) is a safe NOP (posix!).
  171. 879: inline three functions and simplify, some simplifications only show up
  172. after repeated inlining</a></li>
  173. <li>commit: <a href=/hg/toybox/rev/883>883</a>,
  174. description: move some common code to lib/ and posix headers to toys.h.</li>
  175. <li>commit: <a href=/hg/toybox/rev/898>898</a>,
  176. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-May/000974.html>Argument parsing. (Replace ifconfig_main() if/else staircase with a loop over
  177. an array, genericize minus prefix logic, inline a use of set_flags().)</a></li>
  178. <li>commit: <a href=/hg/toybox/rev/905>905</a>,
  179. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-May/000992.html>remove unnecessary wrapper function, inlining more functions,
  180. relying on the values of constants that don't change across architectures
  181. (binary backwards compatability), more ifconfig_main table work,
  182. man ioctl_list.</a></li>
  183. <li>commit: <a href=/hg/toybox/rev/906>906</a>,
  184. description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-May/000994.html>More ifconfig_main() table work, remove vestigial arguments
  185. to functions, "a frenzy of inlining", slightly better error reporting,
  186. don't reinvent libc functions.</a></li>
  187. <li>commit: <a href=/hg/toybox/rev/907>907</a>,
  188. <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-May/000995.html>inlining show_ip_addr() with a loop and a table, inlining hex_to_binary()
  189. and set_hw_address(), stop validating data we supplied, remove a table
  190. that's overkill (two entries, we don't even loop over it), when you don't need a
  191. NULL terminator for array, remove unnecessary memcpy(offsetof()) with
  192. assignment, trusting -funsigned-char.</a></li>
  193. <li>commit: <a href=/hg/toybox/rev/919>919</a>,
  194. <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-June/001027.html>todo whitespace damage, introduce IFREQ_OFFSZ() and poke() to
  195. ifconfig_main() table logic to fold more if/else parts into the table</a></li>
  196. <li>Status update: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-June/001033.html>Entering the home stretch on ifconfig</a> (and a <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-June/001043.html>note about infiniband</a>)</li>
  197. <li>commit: <a href=/hg/toybox/rev/921>921</a>, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2014-June/003508.html>Inline a couple more functions and make sockfd a global.</li>
  198. <li>commit: <a href=/hg/toybox/rev/957>957</a>, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-July/001121.html>Remove unused socklen and addr_to_len(), cleanup so we can merge get_device_info()/display_ifconfig().</a></li>
  199. <li>commit: <a href=/hg/toybox/rev/958>958</a>, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-July/001131.html>This commit removes struct if_list by unifying get_device_info() and display_ifconfig().</a></li>
  200. <li>commit: <a href=/hg/toybox/rev/1104>1104</a>, description: Merge toynet into toys.h: musl supports it and micromanaging uClibc config options isn't very interesting anymore.</li>
  201. <li>commit: <a href=/hg/toybox/rev/1127>1127</a>, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-November/001464.html>Start tacling ipv6 issues, beginning with display_ifconfig().</a></li>
  202. <li>commit: <a href=/hg/toybox/rev/1128>1128</a>, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-November/001463.html>More ipv6, make struct sockaddr_with_len go away, merge more arguments into the table in main().</a></li>
  203. <li>commit: <a href=/hg/toybox/rev/1132>1132</a>, description: promotion from pending to other</li>
  204. <li>commit: <a href=/hg/toybox/rev/1133>1133</a>, description: <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-November/001462.html>cleanup help text, remove obsolete/NOP commands</a></li>
  205. </ul>
  206. <p>Status: COMPLETE.</p>
  207. <p>Status: in progress.</p>
  208. <a name=stat>
  209. <h1><a href=/hg/toybox/log/917/toys/pending/stat.c>stat</a></h1>
  210. <p>A lot of the stat cleanup was done by Felix Janda.</p>
  211. <ul>
  212. <li>commit <a href=/hg/toybox/rev/747>747</a>: initial submission</a></li>
  213. <li>commit <a href=/hg/toybox/rev/810>810</a>: whitespace</li>
  214. <li>commit <a href=/hg/toybox/rev/811>811</a>: description in commit message.</li>
  215. <li>commit <a href=/hg/toybox/rev/871>871</a>: whitespace (reindent from 4 spaces to 2)</li>
  216. <li>commit <a href=/hg/toybox/rev/872>872</a>: Felix Janda - <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000923.html>cleanup</a> (with discussion thread)</li>
  217. <li>commit <a href=/hg/toybox/rev/875>885</a>: Felix Janda - <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-April/000936.html>move permission formatting (777 -> -rwxrwxrwx) from ls to lib so stat can reuse it.</a></li>
  218. <li>commit <a href=/hg/toybox/rev/885>886</a>: Felix Janda - remove unimplemented options and clean up help text</li>
  219. <li>commit <a href=/hg/toybox/rev/910>910</a>: Felix Janda - <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-May/001013.html>Add support for stating multiple files</a>.</li>
  220. <li>commit <a href=/hg/toybox/rev/911>911</a>: Felix Janda - Separate stat and statfs, give stat_main() a ds[2] array to distinguish FLAG_f vs not cases, and some whitespace changes.</li>
  221. <li>commit <a href=/hg/toybox/rev/912>912</a>: description in commit message (also <a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-May/001019.html>here</a>)</li>
  222. <li><a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-May/001024.html>design pondering</a> (leading to peek() function in lib/)</li>
  223. <li>commit <a href=/hg/toybox/rev/914>914</a>: description in commit message.</li>
  224. <li>commit <a href=/hg/toybox/rev/916>916</a>: description in commit message.</li>
  225. <li>commit: <a href=/hg/toybox/rev/917>917</a>: description in commit message.</li>
  226. <li>commit: <a href=/hg/toybox/rev/918>918</a>,
  227. description: done: move pending to posix, default y, no other changes</a>.</li>
  228. <li><a href=http://lists.landley.net/pipermail/toybox-landley.net/2013-June/001026.html>summary</a></li>
  229. </ul>
  230. <p>Status: COMPLETE.</p>
  231. <!--#include file="footer.html" -->