Search code examples
linuxeditornano

Why nano and rnano are different?


I tried to edit files by using nano and rnano. The latter is really in restrict mode, but they are exactly same binary files. Why do they behave differently?

# which nano rnano
/usr/bin/nano
/bin/rnano

# md5sum /usr/bin/nano /bin/rnano
fa670e309a033718bad4b2051f5974fd  /usr/bin/nano
fa670e309a033718bad4b2051f5974fd  /bin/rnano

(In ubuntu 12.04 x64 LTS)


Solution

  • They behave differently because of the argument vector (argv), whose first element (argv[0]) contains the name of the file being executed.

    Because rnano is a separate file from nano (even though it is just a symbolic link), it has its own, separate argv[0].

    You can see this check in nano.c's main function:

    /* If the executable filename starts with 'r', enable restricted
     * mode.
     */
    if (*(tail(argv[0])) == 'r')
        SET(RESTRICTED);
    

    You can also test this with a simple shell script. Create a shell script with only one statement, echo $0. Then, create a symbolic link to it with a separate name. Observe the difference.