aboutsummaryrefslogtreecommitdiffstats
path: root/awklib/eg/lib
diff options
context:
space:
mode:
Diffstat (limited to 'awklib/eg/lib')
-rw-r--r--awklib/eg/lib/assert.awk18
-rw-r--r--awklib/eg/lib/ctime.awk11
-rw-r--r--awklib/eg/lib/ftrans.awk15
-rw-r--r--awklib/eg/lib/getopt.awk82
-rw-r--r--awklib/eg/lib/gettime.awk61
-rw-r--r--awklib/eg/lib/grcat.c34
-rw-r--r--awklib/eg/lib/groupawk.in80
-rw-r--r--awklib/eg/lib/join.awk15
-rw-r--r--awklib/eg/lib/mktime.awk106
-rw-r--r--awklib/eg/lib/nextfile.awk15
-rw-r--r--awklib/eg/lib/ord.awk54
-rw-r--r--awklib/eg/lib/passwdawk.in56
-rw-r--r--awklib/eg/lib/pwcat.c29
13 files changed, 576 insertions, 0 deletions
diff --git a/awklib/eg/lib/assert.awk b/awklib/eg/lib/assert.awk
new file mode 100644
index 00000000..914aa632
--- /dev/null
+++ b/awklib/eg/lib/assert.awk
@@ -0,0 +1,18 @@
+# assert --- assert that a condition is true. Otherwise exit.
+# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
+# May, 1993
+
+function assert(condition, string)
+{
+ if (! condition) {
+ printf("%s:%d: assertion failed: %s\n",
+ FILENAME, FNR, string) > "/dev/stderr"
+ _assert_exit = 1
+ exit 1
+ }
+}
+
+END {
+ if (_assert_exit)
+ exit 1
+}
diff --git a/awklib/eg/lib/ctime.awk b/awklib/eg/lib/ctime.awk
new file mode 100644
index 00000000..0a50d262
--- /dev/null
+++ b/awklib/eg/lib/ctime.awk
@@ -0,0 +1,11 @@
+# ctime.awk
+#
+# awk version of C ctime(3) function
+
+function ctime(ts, format)
+{
+ format = "%a %b %d %H:%M:%S %Z %Y"
+ if (ts == 0)
+ ts = systime() # use current time as default
+ return strftime(format, ts)
+}
diff --git a/awklib/eg/lib/ftrans.awk b/awklib/eg/lib/ftrans.awk
new file mode 100644
index 00000000..0d6e8108
--- /dev/null
+++ b/awklib/eg/lib/ftrans.awk
@@ -0,0 +1,15 @@
+# ftrans.awk --- handle data file transitions
+#
+# user supplies beginfile() and endfile() functions
+#
+# Arnold Robbins, arnold@gnu.ai.mit.edu. November 1992
+# Public Domain
+
+FNR == 1 {
+ if (_filename_ != "")
+ endfile(_filename_)
+ _filename_ = FILENAME
+ beginfile(FILENAME)
+}
+
+END { endfile(_filename_) }
diff --git a/awklib/eg/lib/getopt.awk b/awklib/eg/lib/getopt.awk
new file mode 100644
index 00000000..70a1ec0f
--- /dev/null
+++ b/awklib/eg/lib/getopt.awk
@@ -0,0 +1,82 @@
+# getopt --- do C library getopt(3) function in awk
+#
+# arnold@gnu.ai.mit.edu
+# Public domain
+#
+# Initial version: March, 1991
+# Revised: May, 1993
+
+# External variables:
+# Optind -- index of ARGV for first non-option argument
+# Optarg -- string value of argument to current option
+# Opterr -- if non-zero, print our own diagnostic
+# Optopt -- current option letter
+
+# Returns
+# -1 at end of options
+# ? for unrecognized option
+# <c> a character representing the current option
+
+# Private Data
+# _opti index in multi-flag option, e.g., -abc
+function getopt(argc, argv, options, optl, thisopt, i)
+{
+ optl = length(options)
+ if (optl == 0) # no options given
+ return -1
+
+ if (argv[Optind] == "--") { # all done
+ Optind++
+ _opti = 0
+ return -1
+ } else if (argv[Optind] !~ /^-[^: \t\n\f\r\v\b]/) {
+ _opti = 0
+ return -1
+ }
+ if (_opti == 0)
+ _opti = 2
+ thisopt = substr(argv[Optind], _opti, 1)
+ Optopt = thisopt
+ i = index(options, thisopt)
+ if (i == 0) {
+ if (Opterr)
+ printf("%c -- invalid option\n",
+ thisopt) > "/dev/stderr"
+ if (_opti >= length(argv[Optind])) {
+ Optind++
+ _opti = 0
+ } else
+ _opti++
+ return "?"
+ }
+ if (substr(options, i + 1, 1) == ":") {
+ # get option argument
+ if (length(substr(argv[Optind], _opti + 1)) > 0)
+ Optarg = substr(argv[Optind], _opti + 1)
+ else
+ Optarg = argv[++Optind]
+ _opti = 0
+ } else
+ Optarg = ""
+ if (_opti == 0 || _opti >= length(argv[Optind])) {
+ Optind++
+ _opti = 0
+ } else
+ _opti++
+ return thisopt
+}
+BEGIN {
+ Opterr = 1 # default is to diagnose
+ Optind = 1 # skip ARGV[0]
+
+ # test program
+ if (_getopt_test) {
+ while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != -1)
+ printf("c = <%c>, optarg = <%s>\n",
+ _go_c, Optarg)
+ printf("non-option arguments:\n")
+ for (; Optind < ARGC; Optind++)
+ printf("\tARGV[%d] = <%s>\n",
+ Optind, ARGV[Optind])
+ }
+}
diff --git a/awklib/eg/lib/gettime.awk b/awklib/eg/lib/gettime.awk
new file mode 100644
index 00000000..500dfcef
--- /dev/null
+++ b/awklib/eg/lib/gettime.awk
@@ -0,0 +1,61 @@
+# gettimeofday --- get the time of day in a usable format
+# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain, May 1993
+#
+# Returns a string in the format of output of date(1)
+# Populates the array argument time with individual values:
+# time["second"] -- seconds (0 - 59)
+# time["minute"] -- minutes (0 - 59)
+# time["hour"] -- hours (0 - 23)
+# time["althour"] -- hours (0 - 12)
+# time["monthday"] -- day of month (1 - 31)
+# time["month"] -- month of year (1 - 12)
+# time["monthname"] -- name of the month
+# time["shortmonth"] -- short name of the month
+# time["year"] -- year within century (0 - 99)
+# time["fullyear"] -- year with century (19xx or 20xx)
+# time["weekday"] -- day of week (Sunday = 0)
+# time["altweekday"] -- day of week (Monday = 0)
+# time["weeknum"] -- week number, Sunday first day
+# time["altweeknum"] -- week number, Monday first day
+# time["dayname"] -- name of weekday
+# time["shortdayname"] -- short name of weekday
+# time["yearday"] -- day of year (0 - 365)
+# time["timezone"] -- abbreviation of timezone name
+# time["ampm"] -- AM or PM designation
+
+function gettimeofday(time, ret, now, i)
+{
+ # get time once, avoids unnecessary system calls
+ now = systime()
+
+ # return date(1)-style output
+ ret = strftime("%a %b %d %H:%M:%S %Z %Y", now)
+
+ # clear out target array
+ for (i in time)
+ delete time[i]
+
+ # fill in values, force numeric values to be
+ # numeric by adding 0
+ time["second"] = strftime("%S", now) + 0
+ time["minute"] = strftime("%M", now) + 0
+ time["hour"] = strftime("%H", now) + 0
+ time["althour"] = strftime("%I", now) + 0
+ time["monthday"] = strftime("%d", now) + 0
+ time["month"] = strftime("%m", now) + 0
+ time["monthname"] = strftime("%B", now)
+ time["shortmonth"] = strftime("%b", now)
+ time["year"] = strftime("%y", now) + 0
+ time["fullyear"] = strftime("%Y", now) + 0
+ time["weekday"] = strftime("%w", now) + 0
+ time["altweekday"] = strftime("%u", now) + 0
+ time["dayname"] = strftime("%A", now)
+ time["shortdayname"] = strftime("%a", now)
+ time["yearday"] = strftime("%j", now) + 0
+ time["timezone"] = strftime("%Z", now)
+ time["ampm"] = strftime("%p", now)
+ time["weeknum"] = strftime("%U", now) + 0
+ time["altweeknum"] = strftime("%W", now) + 0
+
+ return ret
+}
diff --git a/awklib/eg/lib/grcat.c b/awklib/eg/lib/grcat.c
new file mode 100644
index 00000000..9742c592
--- /dev/null
+++ b/awklib/eg/lib/grcat.c
@@ -0,0 +1,34 @@
+/*
+ * grcat.c
+ *
+ * Generate a printable version of the group database
+ *
+ * Arnold Robbins, arnold@gnu.ai.mit.edu
+ * May 1993
+ * Public Domain
+ */
+
+#include <stdio.h>
+#include <grp.h>
+
+int
+main(argc, argv)
+int argc;
+char **argv;
+{
+ struct group *g;
+ int i;
+
+ while ((g = getgrent()) != NULL) {
+ printf("%s:%s:%d:", g->gr_name, g->gr_passwd,
+ g->gr_gid);
+ for (i = 0; g->gr_mem[i] != NULL; i++) {
+ printf("%s", g->gr_mem[i]);
+ if (g->gr_mem[i+1] != NULL)
+ putchar(',');
+ }
+ putchar('\n');
+ }
+ endgrent();
+ exit(0);
+}
diff --git a/awklib/eg/lib/groupawk.in b/awklib/eg/lib/groupawk.in
new file mode 100644
index 00000000..a8103a04
--- /dev/null
+++ b/awklib/eg/lib/groupawk.in
@@ -0,0 +1,80 @@
+# group.awk --- functions for dealing with the group file
+# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
+# May 1993
+
+BEGIN \
+{
+ # Change to suit your system
+ _gr_awklib = "/usr/local/libexec/awk/"
+}
+function _gr_init( oldfs, oldrs, olddol0, grcat, n, a, i)
+{
+ if (_gr_inited)
+ return
+
+ oldfs = FS
+ oldrs = RS
+ olddol0 = $0
+ FS = ":"
+ RS = "\n"
+
+ grcat = _gr_awklib "grcat"
+ while ((grcat | getline) > 0) {
+ if ($1 in _gr_byname)
+ _gr_byname[$1] = _gr_byname[$1] "," $4
+ else
+ _gr_byname[$1] = $0
+ if ($3 in _gr_bygid)
+ _gr_bygid[$3] = _gr_bygid[$3] "," $4
+ else
+ _gr_bygid[$3] = $0
+
+ n = split($4, a, "[ \t]*,[ \t]*")
+ for (i = 1; i <= n; i++)
+ if (a[i] in _gr_groupsbyuser)
+ _gr_groupsbyuser[a[i]] = \
+ _gr_groupsbyuser[a[i]] " " $1
+ else
+ _gr_groupsbyuser[a[i]] = $1
+
+ _gr_bycount[++_gr_count] = $0
+ }
+ close(grcat)
+ _gr_count = 0
+ _gr_inited++
+ FS = oldfs
+ RS = oldrs
+ $0 = olddol0
+}
+function getgrnam(group)
+{
+ _gr_init()
+ if (group in _gr_byname)
+ return _gr_byname[group]
+ return ""
+}
+function getgrgid(gid)
+{
+ _gr_init()
+ if (gid in _gr_bygid)
+ return _gr_bygid[gid]
+ return ""
+}
+function getgruser(user)
+{
+ _gr_init()
+ if (user in _gr_groupsbyuser)
+ return _gr_groupsbyuser[user]
+ return ""
+}
+function getgrent()
+{
+ _gr_init()
+ if (++gr_count in _gr_bycount)
+ return _gr_bycount[_gr_count]
+ return ""
+}
+function endgrent()
+{
+ _gr_count = 0
+}
diff --git a/awklib/eg/lib/join.awk b/awklib/eg/lib/join.awk
new file mode 100644
index 00000000..e6b81656
--- /dev/null
+++ b/awklib/eg/lib/join.awk
@@ -0,0 +1,15 @@
+# join.awk --- join an array into a string
+# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
+# May 1993
+
+function join(array, start, end, sep, result, i)
+{
+ if (sep == "")
+ sep = " "
+ else if (sep == SUBSEP) # magic value
+ sep = ""
+ result = array[start]
+ for (i = start + 1; i <= end; i++)
+ result = result sep array[i]
+ return result
+}
diff --git a/awklib/eg/lib/mktime.awk b/awklib/eg/lib/mktime.awk
new file mode 100644
index 00000000..60c5b60d
--- /dev/null
+++ b/awklib/eg/lib/mktime.awk
@@ -0,0 +1,106 @@
+# mktime.awk --- convert a canonical date representation
+# into a timestamp
+# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
+# May 1993
+
+BEGIN \
+{
+ # Initialize table of month lengths
+ _tm_months[0,1] = _tm_months[1,1] = 31
+ _tm_months[0,2] = 28; _tm_months[1,2] = 29
+ _tm_months[0,3] = _tm_months[1,3] = 31
+ _tm_months[0,4] = _tm_months[1,4] = 30
+ _tm_months[0,5] = _tm_months[1,5] = 31
+ _tm_months[0,6] = _tm_months[1,6] = 30
+ _tm_months[0,7] = _tm_months[1,7] = 31
+ _tm_months[0,8] = _tm_months[1,8] = 31
+ _tm_months[0,9] = _tm_months[1,9] = 30
+ _tm_months[0,10] = _tm_months[1,10] = 31
+ _tm_months[0,11] = _tm_months[1,11] = 30
+ _tm_months[0,12] = _tm_months[1,12] = 31
+}
+# decide if a year is a leap year
+function _tm_isleap(year, ret)
+{
+ ret = (year % 4 == 0 && year % 100 != 0) ||
+ (year % 400 == 0)
+
+ return ret
+}
+# convert a date into seconds
+function _tm_addup(a, total, yearsecs, daysecs,
+ hoursecs, i, j)
+{
+ hoursecs = 60 * 60
+ daysecs = 24 * hoursecs
+ yearsecs = 365 * daysecs
+
+ total = (a[1] - 1970) * yearsecs
+
+ # extra day for leap years
+ for (i = 1970; i < a[1]; i++)
+ if (_tm_isleap(i))
+ total += daysecs
+
+ j = _tm_isleap(a[1])
+ for (i = 1; i < a[2]; i++)
+ total += _tm_months[j, i] * daysecs
+
+ total += (a[3] - 1) * daysecs
+ total += a[4] * hoursecs
+ total += a[5] * 60
+ total += a[6]
+
+ return total
+}
+# mktime --- convert a date into seconds,
+# compensate for time zone
+
+function mktime(str, res1, res2, a, b, i, j, t, diff)
+{
+ i = split(str, a, " ") # don't rely on FS
+
+ if (i != 6)
+ return -1
+
+ # force numeric
+ for (j in a)
+ a[j] += 0
+
+ # validate
+ if (a[1] < 1970 ||
+ a[2] < 1 || a[2] > 12 ||
+ a[3] < 1 || a[3] > 31 ||
+ a[4] < 0 || a[4] > 23 ||
+ a[5] < 0 || a[5] > 59 ||
+ a[6] < 0 || a[6] > 61 )
+ return -1
+
+ res1 = _tm_addup(a)
+ t = strftime("%Y %m %d %H %M %S", res1)
+
+ if (_tm_debug)
+ printf("(%s) -> (%s)\n", str, t) > "/dev/stderr"
+
+ split(t, b, " ")
+ res2 = _tm_addup(b)
+
+ diff = res1 - res2
+
+ if (_tm_debug)
+ printf("diff = %d seconds\n", diff) > "/dev/stderr"
+
+ res1 += diff
+
+ return res1
+}
+BEGIN {
+ if (_tm_test) {
+ printf "Enter date as yyyy mm dd hh mm ss: "
+ getline _tm_test_date
+
+ t = mktime(_tm_test_date)
+ r = strftime("%Y %m %d %H %M %S", t)
+ printf "Got back (%s)\n", r
+ }
+}
diff --git a/awklib/eg/lib/nextfile.awk b/awklib/eg/lib/nextfile.awk
new file mode 100644
index 00000000..0f729a87
--- /dev/null
+++ b/awklib/eg/lib/nextfile.awk
@@ -0,0 +1,15 @@
+# nextfile --- skip remaining records in current file
+# correctly handle successive occurrences of the same file
+# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
+# May, 1993
+
+# this should be read in before the "main" awk program
+
+function nextfile() { _abandon_ = FILENAME; next }
+
+_abandon_ == FILENAME {
+ if (FNR == 1)
+ _abandon_ = ""
+ else
+ next
+}
diff --git a/awklib/eg/lib/ord.awk b/awklib/eg/lib/ord.awk
new file mode 100644
index 00000000..7e62cb88
--- /dev/null
+++ b/awklib/eg/lib/ord.awk
@@ -0,0 +1,54 @@
+# ord.awk --- do ord and chr
+#
+# Global identifiers:
+# _ord_: numerical values indexed by characters
+# _ord_init: function to initialize _ord_
+#
+# Arnold Robbins
+# arnold@gnu.ai.mit.edu
+# Public Domain
+# 16 January, 1992
+# 20 July, 1992, revised
+
+BEGIN { _ord_init() }
+function _ord_init( low, high, i, t)
+{
+ low = sprintf("%c", 7) # BEL is ascii 7
+ if (low == "\a") { # regular ascii
+ low = 0
+ high = 127
+ } else if (sprintf("%c", 128 + 7) == "\a") {
+ # ascii, mark parity
+ low = 128
+ high = 255
+ } else { # ebcdic(!)
+ low = 0
+ high = 255
+ }
+
+ for (i = low; i <= high; i++) {
+ t = sprintf("%c", i)
+ _ord_[t] = i
+ }
+}
+function ord(str, c)
+{
+ # only first character is of interest
+ c = substr(str, 1, 1)
+ return _ord_[c]
+}
+function chr(c)
+{
+ # force c to be numeric by adding 0
+ return sprintf("%c", c + 0)
+}
+#### test code ####
+# BEGIN \
+# {
+# for (;;) {
+# printf("enter a character: ")
+# if (getline var <= 0)
+# break
+# printf("ord(%s) = %d\n", var, ord(var))
+# }
+# }
diff --git a/awklib/eg/lib/passwdawk.in b/awklib/eg/lib/passwdawk.in
new file mode 100644
index 00000000..7b64f60d
--- /dev/null
+++ b/awklib/eg/lib/passwdawk.in
@@ -0,0 +1,56 @@
+# passwd.awk --- access password file information
+# Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
+# May 1993
+
+BEGIN {
+ # tailor this to suit your system
+ _pw_awklib = "/usr/local/libexec/awk/"
+}
+
+function _pw_init( oldfs, oldrs, olddol0, pwcat)
+{
+ if (_pw_inited)
+ return
+ oldfs = FS
+ oldrs = RS
+ olddol0 = $0
+ FS = ":"
+ RS = "\n"
+ pwcat = _pw_awklib "pwcat"
+ while ((pwcat | getline) > 0) {
+ _pw_byname[$1] = $0
+ _pw_byuid[$3] = $0
+ _pw_bycount[++_pw_total] = $0
+ }
+ close(pwcat)
+ _pw_count = 0
+ _pw_inited = 1
+ FS = oldfs
+ RS = oldrs
+ $0 = olddol0
+}
+function getpwnam(name)
+{
+ _pw_init()
+ if (name in _pw_byname)
+ return _pw_byname[name]
+ return ""
+}
+function getpwuid(uid)
+{
+ _pw_init()
+ if (uid in _pw_byuid)
+ return _pw_byuid[uid]
+ return ""
+}
+function getpwent()
+{
+ _pw_init()
+ if (_pw_count < _pw_total)
+ return _pw_bycount[++_pw_count]
+ return ""
+}
+function endpwent()
+{
+ _pw_count = 0
+}
diff --git a/awklib/eg/lib/pwcat.c b/awklib/eg/lib/pwcat.c
new file mode 100644
index 00000000..ecd25861
--- /dev/null
+++ b/awklib/eg/lib/pwcat.c
@@ -0,0 +1,29 @@
+/*
+ * pwcat.c
+ *
+ * Generate a printable version of the password database
+ *
+ * Arnold Robbins
+ * arnold@gnu.ai.mit.edu
+ * May 1993
+ * Public Domain
+ */
+
+#include <stdio.h>
+#include <pwd.h>
+
+int
+main(argc, argv)
+int argc;
+char **argv;
+{
+ struct passwd *p;
+
+ while ((p = getpwent()) != NULL)
+ printf("%s:%s:%d:%d:%s:%s:%s\n",
+ p->pw_name, p->pw_passwd, p->pw_uid,
+ p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
+
+ endpwent();
+ exit(0);
+}