Question

I don't know if this question, is really fit for this fora, it likely isn't, but nevertheless here we go;

After just running an invalid command on a unix program; I got the familiar;

Usage: PROGRAM [OPTION]... [FILE]...

As I was in the progress of reviewing a program, while I did this a thought sprang to mind. Namely that a C++ program, could easily be implemented, to change behavior based upon the name of the executable (i.e. by the first argument in argv).

I have absolutely no idea why this would be useful, I know that the argv[0] can be useful, but has anyone ever seen a program, which did this (change behavior on name change) and achieved something by doing so?

Was it helpful?

Solution

Yes, it is routine (but well done for realizing that it can be done). For example:

$ (cd /usr/bin; ls -li | grep -w vim)
 2704450 lrwxr-xr-x   1 root      wheel         3 Oct 31  2013 ex -> vim
 2704451 lrwxr-xr-x   1 root      wheel         3 Oct 31  2013 rview -> vim
 2704452 lrwxr-xr-x   1 root      wheel         3 Oct 31  2013 rvim -> vim
 2378579 lrwxr-xr-x   1 root      wheel         3 Oct 31  2013 vi -> vim
 2704453 lrwxr-xr-x   1 root      wheel         3 Oct 31  2013 view -> vim
 2343880 -rwxr-xr-x   1 root      wheel   1472736 Oct 31  2013 vim
 2704454 lrwxr-xr-x   1 root      wheel         3 Oct 31  2013 vimdiff -> vim
$

So, the vim executable can also be invoked as vimdiff or view or vi or ex and it will behave differently depending on how it is invoked. On some systems, these will be hard links instead of symlinks (the data is from Mac OS X 10.9.2 Mavericks), so you'd need to grep for the inode number instead of vim.

Another example:

Bash operates differently when invoked as bash compared with when it is invoked as sh. Classically, shells recognize when they're invoked with a dash - in front of their name (-sh, -bash, etc), and act as login shells when you do that:

$ ps
  PID TTY           TIME CMD
39301 ttys000    0:01.30 -bash
46766 ttys001    0:00.24 -bash
$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top