getent 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/sh
  2. # This files is part of uClibc.
  3. # Distributed under the terms of the Lesser GNU General Public License v2
  4. #
  5. # Closely (not perfectly) emulate the behavior of glibc's getent utility
  6. #
  7. #passwd|shadow|group|aliases|hosts|networks|ethers|netgroup|protocols|services|rpc
  8. # only returns the first match (by design)
  9. # dns based search is not supported (hosts,networks)
  10. # case-insensitive matches not supported (ethers; others?)
  11. # may return false-positives (hosts,protocols,rpc,services,ethers)
  12. [ -z "$PATH" ] && PATH="/bin:/usr/bin" || PATH="${PATH}:/bin:/usr/bin"
  13. export PATH
  14. file="/etc/$1"
  15. case $1 in
  16. passwd|group)
  17. match="^$2:\|^[^:]*:[^:]*:$2:" ;;
  18. shadow)
  19. match="^$2:" ;;
  20. networks|netgroup)
  21. match="^[[:space:]]*$2\>" ;;
  22. hosts|protocols|rpc|services|ethers)
  23. match="\<$2\>" ;;
  24. aliases)
  25. match="^[[:space:]]*$2[[:space:]]*:" ;;
  26. ""|-h|--help)
  27. echo "USAGE: $0 database [key]"
  28. exit 0 ;;
  29. *)
  30. echo "$0: Unknown database: $1" 1>&2
  31. exit 1 ;;
  32. esac
  33. if [ ! -f "$file" ] ; then
  34. echo "$0: Could not find database file for $1" 1>&2
  35. exit 1
  36. fi
  37. if [ $# -eq 1 ] ; then
  38. exec cat "$file"
  39. else
  40. sed "s/#.*//; /$match/q; d" "$file" | grep . || exit 2
  41. fi