(gawk) Id Program
Info Catalog
(gawk) Egrep Program
(gawk) Clones
(gawk) Split Program
Printing Out User Information
-----------------------------
The `id' utility lists a user's real and effective user-id numbers,
real and effective group-id numbers, and the user's group set, if any.
`id' will only print the effective user-id and group-id if they are
different from the real ones. If possible, `id' will also supply the
corresponding user and group names. The output might look like this:
$ id
-| uid=2076(arnold) gid=10(staff) groups=10(staff),4(tty)
This information is exactly what is provided by `gawk''s `/dev/user'
special file ( Special File Names in `gawk' Special Files.).
However, the `id' utility provides a more palatable output than just a
string of numbers.
Here is a simple version of `id' written in `awk'. It uses the user
DONTPRINTYET database library functions ( Reading the User Database Passwd
Functions.), and the group database library functions (*note Reading
DONTPRINTYET database library functions ( Reading the User Database Passwd
Functions.), and the group database library functions ( Reading
the Group Database Group Functions.).
The program is fairly straightforward. All the work is done in the
`BEGIN' rule. The user and group id numbers are obtained from
`/dev/user'. If there is no support for `/dev/user', the program gives
up.
The code is repetitive. The entry in the user database for the real
user-id number is split into parts at the `:'. The name is the first
field. Similar code is used for the effective user-id number, and the
group numbers.
# id.awk --- implement id in awk
# Arnold Robbins, arnold@gnu.org, Public Domain
# May 1993
# output is:
# uid=12(foo) euid=34(bar) gid=3(baz) \
# egid=5(blat) groups=9(nine),2(two),1(one)
BEGIN \
{
if ((getline < "/dev/user") < 0) {
err = "id: no /dev/user support - cannot run"
print err > "/dev/stderr"
exit 1
}
close("/dev/user")
uid = $1
euid = $2
gid = $3
egid = $4
printf("uid=%d", uid)
pw = getpwuid(uid)
if (pw != "") {
split(pw, a, ":")
printf("(%s)", a[1])
}
if (euid != uid) {
printf(" euid=%d", euid)
pw = getpwuid(euid)
if (pw != "") {
split(pw, a, ":")
printf("(%s)", a[1])
}
}
printf(" gid=%d", gid)
pw = getgrgid(gid)
if (pw != "") {
split(pw, a, ":")
printf("(%s)", a[1])
}
if (egid != gid) {
printf(" egid=%d", egid)
pw = getgrgid(egid)
if (pw != "") {
split(pw, a, ":")
printf("(%s)", a[1])
}
}
if (NF > 4) {
printf(" groups=");
for (i = 5; i <= NF; i++) {
printf("%d", $i)
pw = getgrgid($i)
if (pw != "") {
split(pw, a, ":")
printf("(%s)", a[1])
}
if (i < NF)
printf(",")
}
}
print ""
}
Info Catalog
(gawk) Egrep Program
(gawk) Clones
(gawk) Split Program
automatically generated byinfo2html