Decoded: hostname (coreutils)

[Back to Project Main Page]

Note: This page explores the design of command-line utilities. It is not a user guide.
[GNU Manual] [No POSIX requirement] [Linux man] [FreeBSD man]

Logical flow of hostname command (coreutils)

Summary

hostname - set or print the name of current host system

[Source] [Code Walkthrough]

Lines of code: 124
Principal syscall: sethostname()
Support syscalls: None
Options: 2 (help and version)

Added to Shellutils in May 1994 [First version]
Number of revisions: 94 [Code Evolution]

Helpers:
  • None
External non-standard helpers:
  • die() - Exit with mandatory non-zero error and message to stderr
  • error() - Outputs error message to standard error with possible process termination

Setup

hostname requires little setup. main() initializes one local variable, *hostname, to hold the string pointer for the resulting name


Parsing

Parsing for hostname only looks for the default long options. It may include a single argument, interpreted as the name to set the host

Parsing failures

Thess failure cases are explicitly checked:

  • Unknown options used
  • Extra arguments are provided

This failure result in a short error message followed by the usage instructions.


Execution

We either need to get and print the host name, or set the host name. A user provided argument indicates which task we're performing. We either get or set using the appropriate call to xgethostname() or sethostname() respectively.

Failure cases:

  • Failure to set hostname
  • Inability to set host name due to lack of functionality
  • Unable to retrieve hostname

[Back to Project Main Page]