diff --git a/share/functions/__fish_print_hostnames.fish b/share/functions/__fish_print_hostnames.fish index f4f07690f..3ccf2e80e 100644 --- a/share/functions/__fish_print_hostnames.fish +++ b/share/functions/__fish_print_hostnames.fish @@ -1,22 +1,26 @@ function __fish_print_hostnames -d "Print a list of known hostnames" - # Print all hosts from /etc/hosts. Use 'getent hosts' on OSes that support it - # (apparently just Cygwin does not). - # - # Test if 'getent hosts' works and redirect output so errors don't print. - # - # This is all done under the assumption that `getent` *might* print more hosts than the static /etc/hosts. - type -q getent - # Ignore zero IPs. - and getent hosts 2>/dev/null | string match -r -v '^0.0.0.0' | string replace -r '^\s*\S+\s+' '' | string split ' ' - # We care about _getent_s status, not `string split`s. - if test $pipestatus[1] -ne 0; and test -r /etc/hosts - # Ignore commented lines and functionally empty lines. - string match -r -v '^\s*0.0.0.0|^\s*#|^\s*$' /dev/null + end | + # Ignore comments, own IP addresses (127.*, 0.0[.0[.0]], ::1), non-host IPs (fe00::*, ff00::*), + # and leading/trailing whitespace. Split results on whitespace to handle multiple aliases for + # one IP. + string replace -irf '^\s*?(?!(?:#|0\.|127\.|ff0|fe0|::1))\S+\s*(.*?)\s*$' '$1' | + string split ' ' # Print nfs servers from /etc/fstab if test -r /etc/fstab - string match -r '^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3]:|^[a-zA-Z\.]*:'