lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | # $Header: /var/cvs/uClibc/extra/scripts/getent,v 1.2 2005/02/02 14:18:01 solar Exp $ |
| 3 | # |
| 4 | # Closely (not perfectly) emulate the behavior of glibc's getent utility |
| 5 | # |
| 6 | #passwd|shadow|group|aliases|hosts|networks|ethers|netgroup|protocols|services|rpc |
| 7 | # only returns the first match (by design) |
| 8 | # dns based search is not supported (hosts,networks) |
| 9 | # case-insensitive matches not supported (ethers; others?) |
| 10 | # may return false-positives (hosts,protocols,rpc,services,ethers) |
| 11 | |
| 12 | export PATH="${PATH}:/bin:/usr/bin" |
| 13 | |
| 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 | |
| 34 | if [ ! -f "$file" ] ; then |
| 35 | echo "$0: Could not find database file for $1" 1>&2 |
| 36 | exit 1 |
| 37 | fi |
| 38 | |
| 39 | if [ $# -eq 1 ] ; then |
| 40 | exec cat "$file" |
| 41 | else |
| 42 | sed "s/#.*//; /$match/q; d" "$file" | grep . || exit 2 |
| 43 | fi |