Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Very nice; tried it and got a 129 byte working true command. Now I have to understand what's in the 28896 bytes of the true command that comes with the distro I have here.



The real kicker is this comment in GNU true(1).

    /* Note true(1) will return EXIT_FAILURE in the edge case where writes fail with GNU specific options.  */
If you can't program a bug free true(1), please stop with whatever you think you are doing.


So it's basically

    int
    main (int argc, char **argv)
    {
      return EXIT_SUCCESS;
    }
plus all the stuff required to handle --version and --help.

But even compiling just that results in 60 kB on my system. What's in those other kilobytes, indeed!


I don't know how you compiled it, but you can often cut down by using the '-O3' command line option when you are compiling. Then you can use the 'strip a.out' (or whatever your executable name is) to cut it down further.

Then if you open it in a hex editor (or use od -x), I'll bet you'll see pages and pages of zeroes, which are used for aligning it appropriately in RAM when it loads.

At least, those are all things that work on some systems, can't be sure about yours.


There is also an sstrip tool [1], written by the same Brian Raiter already mentioned here. It strips a bit more eagerly:

  $ gcc tiny.c -O3 -o tiny; wc -c tiny
  8456 tiny

  $ strip tiny; wc -c tiny
  6224 tiny

  $ sstrip tiny; wc -c tiny
  4140 tiny

[1] http://www.muppetlabs.com/~breadbox/software/elfkickers.html


I'd guess most of code weight will be setting up and tearing down glibc, initializing stdio etc. Then there is all sorts of metadata that gcc (and ld etc) include by default. There are plenty of resources on how to create minimal C executables, but `-nostdlib` gcc option would be a good starting point as a low-hanging fruit.


People like to dig at the size of GNU's /bin/true and /bin/false, but the reality is - who cares? Does anyone actually ever use it?

    $ type true
    true is a shell builtin


I've frequently bypassed tests/checks by replacing them with a symlink to /bin/true. So, yes. But no I don't care how big it is.


/bin/false is commonly specified instead of a shell in /etc/passwd for accounts that are not supposed to have interactive logins.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: