]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/extra/scripts/getent
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / extra / scripts / getent
1 #!/bin/sh
2 #
3 # Closely (not perfectly) emulate the behavior of glibc's getent utility
4 #
5 #passwd|shadow|group|aliases|hosts|networks|ethers|netgroup|protocols|services|rpc
6 # only returns the first match (by design)
7 # dns based search is not supported (hosts,networks)
8 # case-insensitive matches not supported (ethers; others?)
9 # may return false-positives (hosts,protocols,rpc,services,ethers)
10
11 [ -z "$PATH" ] && PATH="/bin:/usr/bin" || PATH="${PATH}:/bin:/usr/bin"
12 export PATH
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