| 1 |
# passwd.awk --- access password file information |
| 2 |
# |
| 3 |
# Arnold Robbins, [email protected], Public Domain |
| 4 |
# May 1993 |
| 5 |
# Revised October 2000 |
| 6 |
# Revised December 2010 |
| 7 |
# Revised July 2017 |
| 8 |
|
| 9 |
@namespace "foo" # this is foo |
| 10 |
|
| 11 |
@namespace "bar" # this is bar |
| 12 |
|
| 13 |
@namespace "passwd" # move to passwd namespace |
| 14 |
|
| 15 |
BEGIN { |
| 16 |
# tailor this to suit your system |
| 17 |
Awklib = "/usr/local/libexec/awk/" |
| 18 |
} |
| 19 |
|
| 20 |
function awk::endpwent() |
| 21 |
{ |
| 22 |
Count = 0 |
| 23 |
} |
| 24 |
|
| 25 |
function awk::getpwent() |
| 26 |
{ |
| 27 |
Init() |
| 28 |
if (Count < Total) { |
| 29 |
return Bycount[++Count] |
| 30 |
} |
| 31 |
return "" |
| 32 |
} |
| 33 |
|
| 34 |
function awk::getpwnam(name) |
| 35 |
{ |
| 36 |
Init() |
| 37 |
return Byname[name] |
| 38 |
} |
| 39 |
|
| 40 |
function awk::getpwuid(uid) |
| 41 |
{ |
| 42 |
Init() |
| 43 |
return Byuid[uid] |
| 44 |
} |
| 45 |
|
| 46 |
function Init(oldfs, oldrs, olddol0, pwcat, using_fw, using_fpat) |
| 47 |
{ |
| 48 |
if (Inited) { |
| 49 |
return |
| 50 |
} |
| 51 |
oldfs = FS |
| 52 |
oldrs = RS |
| 53 |
olddol0 = $0 |
| 54 |
using_fw = (PROCINFO["FS"] == "FIELDWIDTHS") |
| 55 |
using_fpat = (PROCINFO["FS"] == "FPAT") |
| 56 |
FS = ":" |
| 57 |
RS = "\n" |
| 58 |
pwcat = Awklib "pwcat" |
| 59 |
while ((pwcat | getline) > 0) { |
| 60 |
Byname[$1] = $0 |
| 61 |
Byuid[$3] = $0 |
| 62 |
Bycount[++Total] = $0 |
| 63 |
} |
| 64 |
close(pwcat) |
| 65 |
Count = 0 |
| 66 |
Inited = 1 |
| 67 |
FS = oldfs |
| 68 |
if (using_fw) { |
| 69 |
FIELDWIDTHS = FIELDWIDTHS |
| 70 |
} else if (using_fpat) { |
| 71 |
FPAT = FPAT |
| 72 |
} |
| 73 |
RS = oldrs |
| 74 |
$0 = olddol0 |
| 75 |
} |