Solaris /bin/true



  • While playing with the Solaris OS and reading through some of the included programs I came across /bin/true.  For those not familiar with a *NIX system, this simply does nothing and returns sucessfully.  Realizing that this was a shell script and not a binary executable, I decided to see how it was implemented.

    bash-2.05$ cat /bin/true
    #!/usr/bin/sh
    #       Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
    #         All Rights Reserved

    #       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
    #       The copyright notice above does not evidence any
    #       actual or intended publication of such source code.

    #ident  "@(#)true.sh    1.6     93/01/11 SMI"   /* SVr4.0 1.4   */
    bash-2.05$

    I am not sure which is worse, that this is copyrighted, or that this is version 1.6.  It really makes me wonder what the version history looks like.



  • Great! Another piece of unix code reliesed into the wild. Now BSD will not have to re-write this code, and can replace the re-written UCB code with original UNIX!!!

    Hmm, [code]//BSD Copyright snipped: Go read it elsewhere.

    int
    main(void)
    {
    return 0;
    }
    [/code]
     I think we'll keep our version.



  • So? Now look at GNU /bin/true and /bin/false... they contain a full-fledged option parser just to catch --help and --version:

    $ /bin/true --version
    true (GNU coreutils) 5.93
    Copyright (C) 2005 Free Software Foundation, Inc.
    This is free software.  You may redistribute copies of it under the terms of
    the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
    There is NO WARRANTY, to the extent permitted by law.

    Written by Jim Meyering.
    $ /bin/true --help  
    Usage: /bin/true [ignored command line arguments]
      or:  /bin/true OPTION
    Exit with a status code indicating success.

          --help     display this help and exit
          --version  output version information and exit

    NOTE: your shell may have its own version of true, which usually supersedes
    the version described here.  Please refer to your shell's documentation
    for details about the options it supports.

    Report bugs to <bug-coreutils@gnu.org>.


    What worries me more... the version 5.93, that true can be so complicated or that the GNU people actually expect other "true"s to have extra command line arguments too ("please refer to your shell's documentation for details about the options it supports")?



  • @OpBaI said:

    So? Now look at GNU /bin/true and /bin/false... they contain a full-fledged option parser just to catch --help and --version:

    I think that's been removed in more recent versions.

    What worries me more... the version 5.93, that true can be so complicated or

    To be fair, the 5.93 is the version of the whole coreutils package, not just /bin/true.

    that the GNU people actually expect other "true"s to have extra command line arguments too ("please refer to your shell's documentation for details about the options it supports")?

    That is a bit bizarre, but it's probably just standard boilerplate for when they write a program that's also implemented as a builtin.



  • Can we please see what /bin/false looks like? I'm betting it will make /bin/true looks sensible.

    But that is a really sucky way of implementing true. I mean, what if there's something wrong with your sh? OK that's unlikely, but not impossible. Then true could potentially return non-zero. That would seriously mess things up.



  • [quote user="j_johnso"]

    While playing with the Solaris OS and reading through some of the included programs I came across /bin/true.  For those not familiar with a *NIX system, this simply does nothing and returns sucessfully.  Realizing that this was a shell script and not a binary executable, I decided to see how it was implemented.

    bash-2.05$ cat /bin/true
    #!/usr/bin/sh
    #       Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
    #         All Rights Reserved

    #       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
    #       The copyright notice above does not evidence any
    #       actual or intended publication of such source code.

    #ident  "@(#)true.sh    1.6     93/01/11 SMI"   /* SVr4.0 1.4   */
    bash-2.05$

    I am not sure which is worse, that this is copyrighted, or that this is version 1.6.  It really makes me wonder what the version history looks like.

    [/quote]

    1.1 bump copyright notice (dont forget it next years, as we did last year)

     -#       Copyright (c) 1984 AT&T

    +#       Copyright (c) 1984, 1986 AT&T

    1.2 bump copyright notice

     -#       Copyright (c) 1984, 1986 AT&T

    +#       Copyright (c) 1984, 1986, 1987 AT&T

    1.3 bump copyright notice

     -#       Copyright (c) 1984, 1986, 1987 AT&T

    +#       Copyright (c) 1984, 1986, 1987, 1988 AT&T

    1.4 bump copyright notice

     -#       Copyright (c) 1984, 1986, 1987,1988 AT&T

    +#       Copyright (c) 1984, 1986, 1987, 1988,1989 AT&T

    1.5 bump copyright notice

     -#       Copyright (c) 1984, 1986, 1987,1988, 1989 AT&T

    +#       Copyright (c) 1984, 1986, 1987, 1988,1989, 1990 AT&T

    1.6 damn, we forgot that we dont have the copyright anymore

     -#       Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T

    +#       Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T



  • The null program on IBM mainframes took several tries to get right, too.

     

    http://en.wikipedia.org/wiki/IEFBR14 



  • [quote user="iwpg"]

    That is a bit bizarre, but it's probably just standard boilerplate for when they write a program that's also implemented as a builtin.

    [/quote]

     Anyways, I guess that makes it the most feature rich /bin/true ever created, with option parser and full internationalization (I get all those nice messages in german).



  • What are these programs for anyway? I can't imagine how they could possibly be useful.



  • [quote user="HeroreV"]What are these programs for anyway? I can't imagine how they could possibly be useful.
    [/quote]

    while true; do important_server_program_that_should_be_restarted_even_after_crashed; done 



  • Oh boy, check out GNU true:

     

    /* Exit with a status code indicating success.
       Copyright (C) 1999-2003, 2005 Free Software Foundation, Inc.
    

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */

    #include
    #include
    #include
    #include "system.h"

    /* Act like "true" by default; false.c overrides this. */
    #ifndef EXIT_STATUS

    define EXIT_STATUS EXIT_SUCCESS

    #endif

    #if EXIT_STATUS == EXIT_SUCCESS

    define PROGRAM_NAME "true"

    #else

    define PROGRAM_NAME "false"

    #endif

    #define AUTHORS "Jim Meyering"

    /* The name this program was run with. */
    char *program_name;

    void
    usage (int status)
    {
    printf (_("
    Usage: %s [ignored command line arguments]\n
    or: %s OPTION\n
    "),
    program_name, program_name);
    printf ("%s\n\n",
    (EXIT_STATUS == EXIT_SUCCESS
    ? "Exit with a status code indicating success."
    : "Exit with a status code indicating failure."));
    fputs (HELP_OPTION_DESCRIPTION, stdout);
    fputs (VERSION_OPTION_DESCRIPTION, stdout);
    printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
    printf (
    ("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    exit (status);
    }

    int
    main (int argc, char **argv)
    {
    initialize_main (&argc, &argv);
    program_name = argv[0];
    setlocale (LC_ALL, "");
    bindtextdomain (PACKAGE, LOCALEDIR);
    textdomain (PACKAGE);

    atexit (close_stdout);

    /* Recognize --help or --version only if it's the only command-line
    argument. */
    if (argc == 2)
    {
    if (STREQ (argv[1], "--help"))
    usage (EXIT_STATUS);

      if (STREQ (argv[1], &quot;--version&quot;))
    version_etc (stdout, PROGRAM_NAME, GNU_PACKAGE, VERSION, AUTHORS,
    	     (char *) NULL);
    }
    

    exit (EXIT_STATUS);
    }



  • [quote user="OpBaI"]So? Now look at GNU /bin/true and /bin/false... they contain a full-fledged option parser just to catch --help and --version:

    $ /bin/true --version
    true (GNU coreutils) 5.93
    Copyright (C) 2005 Free Software Foundation, Inc.
    This is free software.  You may redistribute copies of it under the terms of
    the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
    There is NO WARRANTY, to the extent permitted by law.

    Written by Jim Meyering.
    $ /bin/true --help  
    Usage: /bin/true [ignored command line arguments]
      or:  /bin/true OPTION
    Exit with a status code indicating success.

          --help     display this help and exit
          --version  output version information and exit

    NOTE: your shell may have its own version of true, which usually supersedes
    the version described here.  Please refer to your shell's documentation
    for details about the options it supports.

    Report bugs to <bug-coreutils@gnu.org>.


    What worries me more... the version 5.93, that true can be so complicated or that the GNU people actually expect other "true"s to have extra command line arguments too ("please refer to your shell's documentation for details about the options it supports")?
    [/quote]

    I'm amazed it doesn't support <font face="CourierNew">the -psych argument</font>



  • If I wrote /bin/true, I'd make it support a --false argument that inverted the return value. I'd also make /bin/false look like this:

     

    #!/bin/sh

    /bin/true --false 



  • [quote user="craiga"]

    If I wrote /bin/true, I'd make it support a --false argument that inverted the return value. I'd also make /bin/false look like this:

     

    #!/bin/sh

    /bin/true --false 

    [/quote]

     

    No.  The correct solution is to have a /bin/filenotfound with supports both --verynotfalse and --verynottrue arguments.  Then, you could impliment true as:

     

    #!/bin/sh

    /bin/filenotfound --verynotfalse

     

    And false would be the opposite. 



  • When you really think about it, the most correct way to implement filenotfound is not to implement it.

     



  • [quote user="craiga"]

    If I wrote /bin/true, I'd make it support a --false argument that inverted the return value.

    [/quote]

    Actually, it'd make more sense to have one binary that returns different things based on executable name. /bin/false could be a symlink to /bin/true.

    GNU takes the same idea, except they hard-code the thing: as the source code above shows, /bin/false is compiled from the same source as /bin/true. Just that there's false.c which does #define EXIT_STATUS EXIT_FAILURE.

    $ ls -l /bin/false /bin/true
    -rwxr-xr-x 1 root root 12000 2006-03-02 22:08 /bin/false*
    -rwxr-xr-x 1 root root 12000 2006-03-02 22:08 /bin/true*
    $ diff -s /bin/false /bin/true
    Binary files /bin/false and /bin/true differ

    Yep, kind of funny. =)



  • The GNU true source code really just looks like a simple template (with the help and version arguments being pretty standard for all command-line tools).



  • Most systems already come with a fully functional filenotfound utility.

    /bin/filenotfound; echo $?

     zsh: no such file or directory: /bin/filenotfound

    127 

    /bin/true; echo $?

    0

    /bin/false; echo $? 


Log in to reply

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.