[GNU Manual] [No POSIX requirement] [Linux man] [FreeBSD man]
Summary
hostname - set or print the name of current host system
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]
- None
die()
- Exit with mandatory non-zero error and message to stderrerror()
- 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