fdda79363a348e77552eadd76239114a11ec2d26
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / getent / getent
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 # Taken from uClibc 0.9.33.
13
14 export PATH="${PATH}:/bin:/usr/bin"
15
16 file="/etc/$1"
17 case $1 in
18         passwd|group)
19                 match="^$2:\|^[^:]*:[^:]*:$2:" ;;
20         shadow)
21                 match="^$2:" ;;
22         networks|netgroup)
23                 match="^[[:space:]]*$2\>" ;;
24         hosts|protocols|rpc|services|ethers)
25                 match="\<$2\>" ;;
26         aliases)
27                 match="^[[:space:]]*$2[[:space:]]*:" ;;
28         ""|-h|--help)
29                 echo "USAGE: $0 database [key]"
30                 exit 0 ;;
31         *)
32                 echo "$0: Unknown database: $1" 1>&2
33                 exit 1 ;;
34 esac
35
36 if [ ! -f "$file" ] ; then
37         echo "$0: Could not find database file for $1" 1>&2
38         exit 1
39 fi
40
41 if [ $# -eq 1 ] ; then
42         exec cat "$file"
43 else
44         sed "s/#.*//; /$match/q; d" "$file" | grep . || exit 2
45 fi