"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"
"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.
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;
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.
-hswitch, which I assume you're thinking is for help, right?