aboutsummaryrefslogtreecommitdiffstats
path: root/awklib/eg/prog/id.awk
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2010-07-16 12:41:09 +0300
committerArnold D. Robbins <arnold@skeeve.com>2010-07-16 12:41:09 +0300
commit8c042f99cc7465c86351d21331a129111b75345d (patch)
tree9656e653be0e42e5469cec77635c20356de152c2 /awklib/eg/prog/id.awk
parent8ceb5f934787eb7be5fb452fb39179df66119954 (diff)
downloadegawk-8c042f99cc7465c86351d21331a129111b75345d.tar.gz
egawk-8c042f99cc7465c86351d21331a129111b75345d.tar.bz2
egawk-8c042f99cc7465c86351d21331a129111b75345d.zip
Move to gawk-3.0.0.
Diffstat (limited to 'awklib/eg/prog/id.awk')
-rw-r--r--awklib/eg/prog/id.awk69
1 files changed, 69 insertions, 0 deletions
diff --git a/awklib/eg/prog/id.awk b/awklib/eg/prog/id.awk
new file mode 100644
index 00000000..b29ef61a
--- /dev/null
+++ b/awklib/eg/prog/id.awk
@@ -0,0 +1,69 @@
+# id.awk --- implement id in awk
+# Arnold Robbins, arnold@gnu.ai.mit.edu, 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 ""
+}