blob: 30d515b7efaf4b53e095b770909687645c7ca473 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#!/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
12export PATH="${PATH}:/bin:/usr/bin"
13
14file="/etc/$1"
15case $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 ;;
32esac
33
34if [ ! -f "$file" ] ; then
35 echo "$0: Could not find database file for $1" 1>&2
36 exit 1
37fi
38
39if [ $# -eq 1 ] ; then
40 exec cat "$file"
41else
42 sed "s/#.*//; /$match/q; d" "$file" | grep . || exit 2
43fi