Lately I’ve discovered that the latex
and pdftex
(with some other one) binaries in Ubuntu are the same:
lrwxrwxrwx 1 root root 6 лип 29 18:03 latex -> pdftex*
lrwxrwxrwx 1 root root 6 лип 29 18:03 pdflatex -> pdftex*
lrwxrwxrwx 1 root root 6 лип 17 20:22 pdfcslatex -> pdftex*
Executing all without any options yields different results; so, my assumption they use argv[0]
.
What is the advantage of using argv[0]
instead of compiling several binaries? And what other usages of argv[0]
is possible?
1
It results in smaller overall executable size when there is a lot of shared functionality. This is taken to the extreme by busybox, which has hundreds of size-optimized commands all linked in to the same binary and distinguished via argv[0].
There are only two other useful ways of using it that I’m aware of:
- It is handy to use it in help messages showing command line usage so that the help references the same command that the user used even if the binary is symlinked or renamed under a different name to its original
- Some programs write into it to provide a way of showing status feedback that will show up in ps.
It’s basically about saved overall effort. Shipping several subtly different binaries is a waste of disk space, build effort (need more compiles for every release), administration effort (need to maintain more build targets and more projects), etc. And the things that differ between those binaries are relatively simply conditionals that would probably end up auto-generated from the same source file via compiler directives anyway; so it’s easier to go dynamic all the way and make the distinctions in-language via proper if
constructs rather than wonky preprocessor commands.
If a lot of code is reused across the commands, like parsing a file and translating into another format then this prevents mostly duplicate executables. You can do this with a uber command and arguments as well.
This does have the downside that it’s dependent on the way the executable is called, a naive person may copy over the executable and rename it which would then stop it from working like expected.