0

"No news is good news", which, sticking with the normal convention should return 0. Why does

hostname -h 

return 255, especially when, 255 by convention, is meant to mean "Exit status out of range"

2
  • I'm not sure where you're getting that it should return 0. Most tools return a 1 when I give them the -h switch, which I assume you're thinking is for help, right? Commented Feb 12, 2014 at 2:59
  • That's true...but why 255 ? I guess my point is that the convention was not as universal as I expected, and why the authors of hostname chose 255 for the help exit code. Commented Feb 12, 2014 at 3:03

3 Answers 3

2

"hostname -h"

does not return 255 on my RHEL machine.

it does return error 4 which make sense

hostname -h
Usage: hostname [-v] {hostname|-F file}      set hostname (from file)
........
echo $?
4

If you take a look at hostname.c from net-utils you will clearly see that:

static void usage(void)
{
    fprintf(stderr, _("Usage: hostname [-v] {hostname|-F file}      set hostname (from file)\n"));
........
  exit(4); /* E_USAGE */
}

and reference to usage() in here in the same file:

........
    case '?':
    case 'h':
    default:
        usage();

    };

So i am not really sure why you are getting 255, on MAC version you might get "1", but i never seen "255".

EDIT you are right, i've just looked at fedora19 and hostname comes from hostname RPM and indeed for usage they do return 255:

void
usage(FILE *stream)
{
        fprintf(stream,
.....
 exit(-1);
}

;

So here I will try to explain your confusion.

Well, first doing exit(-1) there is a mistake by a programmer, at least for POSIX environment, since in such OS anything that not 0 in exit status considered FAILURE, but:

What about the exit() function found in the standard C library?

It’s manpage tells us that “the exit() function causes normal process termination and the value of status & 0377 is returned to the parent”.

Now, status & 0377?

What does that mean?

Well, 0377 is 377 in octal, which is 255 in decimal, or 0xFF in hexadecimal.

#include <stdlib.h>
void exit(int status);

So, if exit takes a signed argument, but returns a 0xFF masked one to its parent( which is shell), and “-1” is represented as all ones in two’s-complement, this only means that the value the shell is going to see and, thus, $? is going to store = 0xff Recall that 0xff is 255, and that’s pretty much it.

Even if programmer is aware that all user will get is 255 when he calls exit(-1), it is a poor habit to do that while programming in POSIX environment. The code will eventually reach someone that will misunderstand the meaning of the -1(255) argument and raise a question like you(OP) did.

2
  • It returns 255 on Fedora 19. On newer distros hostname is it's own package too. Commented Feb 12, 2014 at 3:17
  • Thanks Danila, @slm is right, chosen this as the correct answer for the effort. Commented Feb 12, 2014 at 4:22
1

In my Ubuntu, hostname -h return 255.

Using apt-get source hostname to get its source, diving into it, we can see that, if using -h options, hostname command will return -1, which is out of range:

void                                                                            
usage(FILE *stream)
{
....
    exit(-1);                                                                   
}

In main function, line 501:

case 'h':                                                               
            usage(stdout);                                                      
            break;
1

In later versions of hostname, version 3.15, it returns -1.

usage(FILE *stream)
{
  fprintf(stream,
    "Usage: hostname [-b] {hostname|-F file}         set host name (from file)\n"
...
...
  exit(-1);
}

This looks like a situation where the status is managed as a signed int, but is then at some point converted to an unsigned int.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.