summaryrefslogtreecommitdiff
path: root/awklib/eg
diff options
context:
space:
mode:
Diffstat (limited to 'awklib/eg')
-rw-r--r--awklib/eg/data/BBS-list11
-rw-r--r--awklib/eg/data/mail-list11
-rw-r--r--awklib/eg/lib/ctime.awk1
-rw-r--r--awklib/eg/lib/div.awk17
-rw-r--r--awklib/eg/lib/ftrans.awk4
-rw-r--r--awklib/eg/lib/getopt.awk7
-rw-r--r--awklib/eg/lib/gettime.awk4
-rw-r--r--awklib/eg/lib/grcat.c2
-rw-r--r--awklib/eg/lib/groupawk.in6
-rw-r--r--awklib/eg/lib/inplace.awk14
-rw-r--r--awklib/eg/lib/noassign.awk2
-rw-r--r--awklib/eg/lib/processarray.awk12
-rw-r--r--awklib/eg/lib/pwcat.c2
-rw-r--r--awklib/eg/lib/quicksort.awk5
-rw-r--r--awklib/eg/lib/readable.awk2
-rw-r--r--awklib/eg/lib/readfile.awk15
-rw-r--r--awklib/eg/lib/shellquote.awk22
-rw-r--r--awklib/eg/lib/strtonum.awk21
-rw-r--r--awklib/eg/misc/arraymax.awk10
-rw-r--r--awklib/eg/misc/findpat.awk13
-rw-r--r--awklib/eg/prog/alarm.awk9
-rw-r--r--awklib/eg/prog/cut.awk23
-rw-r--r--awklib/eg/prog/egrep.awk14
-rw-r--r--awklib/eg/prog/extract.awk19
-rw-r--r--awklib/eg/prog/id.awk38
-rw-r--r--awklib/eg/prog/igawk.sh2
-rw-r--r--awklib/eg/prog/indirectcall.awk2
-rw-r--r--awklib/eg/prog/labels.awk3
-rw-r--r--awklib/eg/prog/pi.awk18
-rw-r--r--awklib/eg/prog/split.awk12
-rw-r--r--awklib/eg/prog/tee.awk6
-rw-r--r--awklib/eg/prog/uniq.awk10
32 files changed, 206 insertions, 131 deletions
diff --git a/awklib/eg/data/BBS-list b/awklib/eg/data/BBS-list
deleted file mode 100644
index 1007417f..00000000
--- a/awklib/eg/data/BBS-list
+++ /dev/null
@@ -1,11 +0,0 @@
-aardvark 555-5553 1200/300 B
-alpo-net 555-3412 2400/1200/300 A
-barfly 555-7685 1200/300 A
-bites 555-1675 2400/1200/300 A
-camelot 555-0542 300 C
-core 555-2912 1200/300 C
-fooey 555-1234 2400/1200/300 B
-foot 555-6699 1200/300 B
-macfoo 555-6480 1200/300 A
-sdace 555-3430 2400/1200/300 A
-sabafoo 555-2127 1200/300 C
diff --git a/awklib/eg/data/mail-list b/awklib/eg/data/mail-list
new file mode 100644
index 00000000..37ff350a
--- /dev/null
+++ b/awklib/eg/data/mail-list
@@ -0,0 +1,11 @@
+Amelia 555-5553 amelia.zodiacusque@gmail.com F
+Anthony 555-3412 anthony.asserturo@hotmail.com A
+Becky 555-7685 becky.algebrarum@gmail.com A
+Bill 555-1675 bill.drowning@hotmail.com A
+Broderick 555-0542 broderick.aliquotiens@yahoo.com R
+Camilla 555-2912 camilla.infusarum@skynet.be R
+Fabius 555-1234 fabius.undevicesimus@ucb.edu F
+Julie 555-6699 julie.perscrutabor@skeeve.com F
+Martin 555-6480 martin.codicibus@hotmail.com A
+Samuel 555-3430 samuel.lanceolis@shu.edu A
+Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
diff --git a/awklib/eg/lib/ctime.awk b/awklib/eg/lib/ctime.awk
index f37856c6..cea25b7a 100644
--- a/awklib/eg/lib/ctime.awk
+++ b/awklib/eg/lib/ctime.awk
@@ -5,6 +5,7 @@
function ctime(ts, format)
{
format = "%a %b %e %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/div.awk b/awklib/eg/lib/div.awk
new file mode 100644
index 00000000..5939024d
--- /dev/null
+++ b/awklib/eg/lib/div.awk
@@ -0,0 +1,17 @@
+# div --- do integer division
+
+#
+# Arnold Robbins, arnold@skeeve.com, Public Domain
+# July, 2014
+
+function div(numerator, denominator, result)
+{
+ split("", result)
+
+ numerator = int(numerator)
+ denominator = int(denominator)
+ result["quotient"] = int(numerator / denominator)
+ result["remainder"] = int(numerator % denominator)
+
+ return 0.0
+}
diff --git a/awklib/eg/lib/ftrans.awk b/awklib/eg/lib/ftrans.awk
index 1709ac82..6461efff 100644
--- a/awklib/eg/lib/ftrans.awk
+++ b/awklib/eg/lib/ftrans.awk
@@ -1,4 +1,4 @@
-# ftrans.awk --- handle data file transitions
+# ftrans.awk --- handle datafile transitions
#
# user supplies beginfile() and endfile() functions
#
@@ -12,4 +12,4 @@ FNR == 1 {
beginfile(FILENAME)
}
-END { endfile(_filename_) }
+END { endfile(_filename_) }
diff --git a/awklib/eg/lib/getopt.awk b/awklib/eg/lib/getopt.awk
index 0b81aa09..6b1f4c50 100644
--- a/awklib/eg/lib/getopt.awk
+++ b/awklib/eg/lib/getopt.awk
@@ -17,7 +17,7 @@
# <c> a character representing the current option
# Private Data:
-# _opti -- index in multi-flag option, e.g., -abc
+# _opti -- index in multiflag option, e.g., -abc
function getopt(argc, argv, options, thisopt, i)
{
if (length(options) == 0) # no options given
@@ -38,8 +38,7 @@ function getopt(argc, argv, options, thisopt, i)
i = index(options, thisopt)
if (i == 0) {
if (Opterr)
- printf("%c -- invalid option\n",
- thisopt) > "/dev/stderr"
+ printf("%c -- invalid option\n", thisopt) > "/dev/stderr"
if (_opti >= length(argv[Optind])) {
Optind++
_opti = 0
@@ -70,7 +69,7 @@ BEGIN {
# test program
if (_getopt_test) {
while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != -1)
- printf("c = <%c>, optarg = <%s>\n",
+ printf("c = <%c>, Optarg = <%s>\n",
_go_c, Optarg)
printf("non-option arguments:\n")
for (; Optind < ARGC; Optind++)
diff --git a/awklib/eg/lib/gettime.awk b/awklib/eg/lib/gettime.awk
index 95f9c329..4cb56330 100644
--- a/awklib/eg/lib/gettime.awk
+++ b/awklib/eg/lib/gettime.awk
@@ -1,4 +1,4 @@
-# gettimeofday.awk --- get the time of day in a usable format
+# getlocaltime.awk --- get the time of day in a usable format
#
# Arnold Robbins, arnold@skeeve.com, Public Domain, May 1993
#
@@ -25,7 +25,7 @@
# time["weeknum"] -- week number, Sunday first day
# time["altweeknum"] -- week number, Monday first day
-function gettimeofday(time, ret, now, i)
+function getlocaltime(time, ret, now, i)
{
# get time once, avoids unnecessary system calls
now = systime()
diff --git a/awklib/eg/lib/grcat.c b/awklib/eg/lib/grcat.c
index ff2913a1..7d6b6a74 100644
--- a/awklib/eg/lib/grcat.c
+++ b/awklib/eg/lib/grcat.c
@@ -1,7 +1,7 @@
/*
* grcat.c
*
- * Generate a printable version of the group database
+ * Generate a printable version of the group database.
*/
/*
* Arnold Robbins, arnold@skeeve.com, May 1993
diff --git a/awklib/eg/lib/groupawk.in b/awklib/eg/lib/groupawk.in
index 0917b923..54a27f3d 100644
--- a/awklib/eg/lib/groupawk.in
+++ b/awklib/eg/lib/groupawk.in
@@ -5,8 +5,7 @@
# Revised October 2000
# Revised December 2010
-BEGIN \
-{
+BEGIN {
# Change to suit your system
_gr_awklib = "/usr/local/libexec/awk/"
}
@@ -39,8 +38,7 @@ function _gr_init( oldfs, oldrs, olddol0, grcat,
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
+ _gr_groupsbyuser[a[i]] = gr_groupsbyuser[a[i]] " " $1
else
_gr_groupsbyuser[a[i]] = $1
diff --git a/awklib/eg/lib/inplace.awk b/awklib/eg/lib/inplace.awk
new file mode 100644
index 00000000..6403a228
--- /dev/null
+++ b/awklib/eg/lib/inplace.awk
@@ -0,0 +1,14 @@
+# inplace --- load and invoke the inplace extension.
+
+@load "inplace"
+
+# Please set INPLACE_SUFFIX to make a backup copy. For example, you may
+# want to set INPLACE_SUFFIX to .bak on the command line or in a BEGIN rule.
+
+BEGINFILE {
+ inplace_begin(FILENAME, INPLACE_SUFFIX)
+}
+
+ENDFILE {
+ inplace_end(FILENAME, INPLACE_SUFFIX)
+}
diff --git a/awklib/eg/lib/noassign.awk b/awklib/eg/lib/noassign.awk
index 1f750edf..99227b37 100644
--- a/awklib/eg/lib/noassign.awk
+++ b/awklib/eg/lib/noassign.awk
@@ -7,7 +7,7 @@
function disable_assigns(argc, argv, i)
{
for (i = 1; i < argc; i++)
- if (argv[i] ~ /^[[:alpha:]_][[:alnum:]_]*=.*/)
+ if (argv[i] ~ /^[a-zA-Z_][a-zA-Z0-9_]*=.*/)
argv[i] = ("./" argv[i])
}
diff --git a/awklib/eg/lib/processarray.awk b/awklib/eg/lib/processarray.awk
new file mode 100644
index 00000000..79a86d1f
--- /dev/null
+++ b/awklib/eg/lib/processarray.awk
@@ -0,0 +1,12 @@
+function process_array(arr, name, process, do_arrays, i, new_name)
+{
+ for (i in arr) {
+ new_name = (name "[" i "]")
+ if (isarray(arr[i])) {
+ if (do_arrays)
+ @process(new_name, arr[i])
+ process_array(arr[i], new_name, process, do_arrays)
+ } else
+ @process(new_name, arr[i])
+ }
+}
diff --git a/awklib/eg/lib/pwcat.c b/awklib/eg/lib/pwcat.c
index 910e0329..934ef34e 100644
--- a/awklib/eg/lib/pwcat.c
+++ b/awklib/eg/lib/pwcat.c
@@ -1,7 +1,7 @@
/*
* pwcat.c
*
- * Generate a printable version of the password database
+ * Generate a printable version of the password database.
*/
/*
* Arnold Robbins, arnold@skeeve.com, May 1993
diff --git a/awklib/eg/lib/quicksort.awk b/awklib/eg/lib/quicksort.awk
index 7a635d6f..3ba2d6e3 100644
--- a/awklib/eg/lib/quicksort.awk
+++ b/awklib/eg/lib/quicksort.awk
@@ -1,8 +1,9 @@
# quicksort.awk --- Quicksort algorithm, with user-supplied
# comparison function
#
-# Arnold Robbins, arnoldskeeve.com, Public Domain
+# Arnold Robbins, arnold@skeeve.com, Public Domain
# January 2009
+
# quicksort --- C.A.R. Hoare's quick sort algorithm. See Wikipedia
# or almost any algorithms or computer science text
#
@@ -25,7 +26,7 @@ function quicksort(data, left, right, less_than, i, last)
# quicksort_swap --- helper function for quicksort, should really be inline
-function quicksort_swap(data, i, j, temp)
+function quicksort_swap(data, i, j, temp)
{
temp = data[i]
data[i] = data[j]
diff --git a/awklib/eg/lib/readable.awk b/awklib/eg/lib/readable.awk
index 6942dcca..37970a82 100644
--- a/awklib/eg/lib/readable.awk
+++ b/awklib/eg/lib/readable.awk
@@ -6,7 +6,7 @@
BEGIN {
for (i = 1; i < ARGC; i++) {
- if (ARGV[i] ~ /^[[:alpha:]_][[:alnum:]_]*=.*/ \
+ if (ARGV[i] ~ /^[a-zA-Z_][a-zA-Z0-9_]*=.*/ \
|| ARGV[i] == "-" || ARGV[i] == "/dev/stdin")
continue # assignment or standard input
else if ((getline junk < ARGV[i]) < 0) # unreadable
diff --git a/awklib/eg/lib/readfile.awk b/awklib/eg/lib/readfile.awk
new file mode 100644
index 00000000..9137b26d
--- /dev/null
+++ b/awklib/eg/lib/readfile.awk
@@ -0,0 +1,15 @@
+# readfile.awk --- read an entire file at once
+#
+# Original idea by Denis Shirokov, cosmogen@gmail.com, April 2013
+#
+
+function readfile(file, tmp, save_rs)
+{
+ save_rs = RS
+ RS = "^$"
+ getline tmp < file
+ close(file)
+ RS = save_rs
+
+ return tmp
+}
diff --git a/awklib/eg/lib/shellquote.awk b/awklib/eg/lib/shellquote.awk
new file mode 100644
index 00000000..cd943dc7
--- /dev/null
+++ b/awklib/eg/lib/shellquote.awk
@@ -0,0 +1,22 @@
+# shell_quote --- quote an argument for passing to the shell
+#
+# Michael Brennan
+# brennan@madronabluff.com
+# September 2014
+
+function shell_quote(s, # parameter
+ SINGLE, QSINGLE, i, X, n, ret) # locals
+{
+ if (s == "")
+ return "\"\""
+
+ SINGLE = "\x27" # single quote
+ QSINGLE = "\"\x27\""
+ n = split(s, X, SINGLE)
+
+ ret = SINGLE X[1] SINGLE
+ for (i = 2; i <= n; i++)
+ ret = ret QSINGLE SINGLE X[i] SINGLE
+
+ return ret
+}
diff --git a/awklib/eg/lib/strtonum.awk b/awklib/eg/lib/strtonum.awk
index a56ab50c..783496e4 100644
--- a/awklib/eg/lib/strtonum.awk
+++ b/awklib/eg/lib/strtonum.awk
@@ -3,8 +3,9 @@
#
# Arnold Robbins, arnold@skeeve.com, Public Domain
# February, 2004
+# Revised June, 2014
-function mystrtonum(str, ret, chars, n, i, k, c)
+function mystrtonum(str, ret, n, i, k, c)
{
if (str ~ /^0[0-7]*$/) {
# octal
@@ -12,12 +13,13 @@ function mystrtonum(str, ret, chars, n, i, k, c)
ret = 0
for (i = 1; i <= n; i++) {
c = substr(str, i, 1)
- if ((k = index("01234567", c)) > 0)
- k-- # adjust for 1-basing in awk
+ # index() returns 0 if c not in string,
+ # includes c == "0"
+ k = index("1234567", c)
ret = ret * 8 + k
}
- } else if (str ~ /^0[xX][[:xdigit:]]+/) {
+ } else if (str ~ /^0[xX][[:xdigit:]]+$/) {
# hexadecimal
str = substr(str, 3) # lop off leading 0x
n = length(str)
@@ -25,10 +27,9 @@ function mystrtonum(str, ret, chars, n, i, k, c)
for (i = 1; i <= n; i++) {
c = substr(str, i, 1)
c = tolower(c)
- if ((k = index("0123456789", c)) > 0)
- k-- # adjust for 1-basing in awk
- else if ((k = index("abcdef", c)) > 0)
- k += 9
+ # index() returns 0 if c not in string,
+ # includes c == "0"
+ k = index("123456789abcdef", c)
ret = ret * 16 + k
}
@@ -50,8 +51,8 @@ function mystrtonum(str, ret, chars, n, i, k, c)
# a[5] = "123.45"
# a[6] = "1.e3"
# a[7] = "1.32"
-# a[7] = "1.32E2"
-#
+# a[8] = "1.32E2"
+#
# for (i = 1; i in a; i++)
# print a[i], strtonum(a[i]), mystrtonum(a[i])
# }
diff --git a/awklib/eg/misc/arraymax.awk b/awklib/eg/misc/arraymax.awk
index 20dd1768..64197f56 100644
--- a/awklib/eg/misc/arraymax.awk
+++ b/awklib/eg/misc/arraymax.awk
@@ -1,10 +1,10 @@
{
- if ($1 > max)
- max = $1
- arr[$1] = $0
+ if ($1 > max)
+ max = $1
+ arr[$1] = $0
}
END {
- for (x = 1; x <= max; x++)
- print arr[x]
+ for (x = 1; x <= max; x++)
+ print arr[x]
}
diff --git a/awklib/eg/misc/findpat.awk b/awklib/eg/misc/findpat.awk
index e9bef9ea..9d799434 100644
--- a/awklib/eg/misc/findpat.awk
+++ b/awklib/eg/misc/findpat.awk
@@ -1,10 +1,9 @@
{
- if ($1 == "FIND")
- regex = $2
- else {
- where = match($0, regex)
- if (where != 0)
- print "Match of", regex, "found at",
- where, "in", $0
+ if ($1 == "FIND")
+ regex = $2
+ else {
+ where = match($0, regex)
+ if (where != 0)
+ print "Match of", regex, "found at", where, "in", $0
}
}
diff --git a/awklib/eg/prog/alarm.awk b/awklib/eg/prog/alarm.awk
index 53563d15..59630ea8 100644
--- a/awklib/eg/prog/alarm.awk
+++ b/awklib/eg/prog/alarm.awk
@@ -1,6 +1,6 @@
# alarm.awk --- set an alarm
#
-# Requires gettimeofday() library function
+# Requires getlocaltime() library function
#
# Arnold Robbins, arnold@skeeve.com, Public Domain
# May 1993
@@ -8,8 +8,7 @@
# usage: alarm time [ "message" [ count [ delay ] ] ]
-BEGIN \
-{
+BEGIN {
# Initial argument sanity checking
usage1 = "usage: alarm time ['message' [count [delay]]]"
usage2 = sprintf("\t(%s) time ::= hh:mm", ARGV[1])
@@ -53,7 +52,7 @@ BEGIN \
minute = atime[2] + 0 # force numeric
# get current broken down time
- gettimeofday(now)
+ getlocaltime(now)
# if time given is 12-hour hours and it's after that
# hour, e.g., `alarm 5:30' at 9 a.m. means 5:30 p.m.,
@@ -71,7 +70,7 @@ BEGIN \
# how long to sleep for
naptime = target - current
if (naptime <= 0) {
- print "time is in the past!" > "/dev/stderr"
+ print "alarm: time is in the past!" > "/dev/stderr"
exit 1
}
# zzzzzz..... go away if interrupted
diff --git a/awklib/eg/prog/cut.awk b/awklib/eg/prog/cut.awk
index fb4717c1..080279bc 100644
--- a/awklib/eg/prog/cut.awk
+++ b/awklib/eg/prog/cut.awk
@@ -12,16 +12,13 @@
#
# Requires getopt() and join() library functions
-function usage( e1, e2)
+function usage()
{
- e1 = "usage: cut [-f list] [-d c] [-s] [files...]"
- e2 = "usage: cut [-c list] [files...]"
- print e1 > "/dev/stderr"
- print e2 > "/dev/stderr"
+ print("usage: cut [-f list] [-d c] [-s] [files...]") > "/dev/stderr"
+ print("usage: cut [-c list] [files...]") > "/dev/stderr"
exit 1
}
-BEGIN \
-{
+BEGIN {
FS = "\t" # default
OFS = FS
while ((c = getopt(ARGC, ARGV, "sf:c:d:")) != -1) {
@@ -34,7 +31,7 @@ BEGIN \
OFS = ""
} else if (c == "d") {
if (length(Optarg) > 1) {
- printf("Using first character of %s" \
+ printf("cut: using first character of %s" \
" for delimiter\n", Optarg) > "/dev/stderr"
Optarg = substr(Optarg, 1, 1)
}
@@ -43,7 +40,7 @@ BEGIN \
if (FS == " ") # defeat awk semantics
FS = "[ ]"
} else if (c == "s")
- suppress++
+ suppress = 1
else
usage()
}
@@ -75,7 +72,7 @@ function set_fieldlist( n, m, i, j, k, f, g)
if (index(f[i], "-") != 0) { # a range
m = split(f[i], g, "-")
if (m != 2 || g[1] >= g[2]) {
- printf("bad field list: %s\n",
+ printf("cut: bad field list: %s\n",
f[i]) > "/dev/stderr"
exit 1
}
@@ -86,7 +83,7 @@ function set_fieldlist( n, m, i, j, k, f, g)
}
nfields = j - 1
}
-function set_charlist( field, i, j, f, g, t,
+function set_charlist( field, i, j, f, g, n, m, t,
filler, last, len)
{
field = 1 # count total fields
@@ -96,7 +93,7 @@ function set_charlist( field, i, j, f, g, t,
if (index(f[i], "-") != 0) { # range
m = split(f[i], g, "-")
if (m != 2 || g[1] >= g[2]) {
- printf("bad character list: %s\n",
+ printf("cut: bad character list: %s\n",
f[i]) > "/dev/stderr"
exit 1
}
@@ -126,7 +123,7 @@ function set_charlist( field, i, j, f, g, t,
nfields = j - 1
}
{
- if (by_fields && suppress && index($0, FS) != 0)
+ if (by_fields && suppress && index($0, FS) == 0)
next
for (i = 1; i <= nfields; i++) {
diff --git a/awklib/eg/prog/egrep.awk b/awklib/eg/prog/egrep.awk
index 56d199c8..a4165a90 100644
--- a/awklib/eg/prog/egrep.awk
+++ b/awklib/eg/prog/egrep.awk
@@ -88,16 +88,12 @@ function endfile(file)
print
}
}
-END \
-{
- if (total == 0)
- exit 1
- exit 0
+END {
+ exit (total == 0)
}
-function usage( e)
+function usage()
{
- e = "Usage: egrep [-csvil] [-e pat] [files ...]"
- e = e "\n\tegrep [-csvil] pat [files ...]"
- print e > "/dev/stderr"
+ print("Usage: egrep [-csvil] [-e pat] [files ...]") > "/dev/stderr"
+ print("\n\tegrep [-csvil] pat [files ...]") > "/dev/stderr"
exit 1
}
diff --git a/awklib/eg/prog/extract.awk b/awklib/eg/prog/extract.awk
index dc105728..24f40ce5 100644
--- a/awklib/eg/prog/extract.awk
+++ b/awklib/eg/prog/extract.awk
@@ -1,5 +1,4 @@
-# extract.awk --- extract files and run programs
-# from texinfo files
+# extract.awk --- extract files and run programs from texinfo files
#
# Arnold Robbins, arnold@skeeve.com, Public Domain
# May 1993
@@ -7,10 +6,9 @@
BEGIN { IGNORECASE = 1 }
-/^@c(omment)?[ \t]+system/ \
-{
+/^@c(omment)?[ \t]+system/ {
if (NF < 3) {
- e = (FILENAME ":" FNR)
+ e = ("extract: " FILENAME ":" FNR)
e = (e ": badly formed `system' line")
print e > "/dev/stderr"
next
@@ -19,15 +17,14 @@ BEGIN { IGNORECASE = 1 }
$2 = ""
stat = system($0)
if (stat != 0) {
- e = (FILENAME ":" FNR)
+ e = ("extract: " FILENAME ":" FNR)
e = (e ": warning: system returned " stat)
print e > "/dev/stderr"
}
}
-/^@c(omment)?[ \t]+file/ \
-{
+/^@c(omment)?[ \t]+file/ {
if (NF != 3) {
- e = (FILENAME ":" FNR ": badly formed `file' line")
+ e = ("extract: " FILENAME ":" FNR ": badly formed `file' line")
print e > "/dev/stderr"
next
}
@@ -65,8 +62,8 @@ BEGIN { IGNORECASE = 1 }
}
function unexpected_eof()
{
- printf("%s:%d: unexpected EOF or error\n",
- FILENAME, FNR) > "/dev/stderr"
+ printf("extract: %s:%d: unexpected EOF or error\n",
+ FILENAME, FNR) > "/dev/stderr"
exit 1
}
diff --git a/awklib/eg/prog/id.awk b/awklib/eg/prog/id.awk
index 8b60a245..b6061f9b 100644
--- a/awklib/eg/prog/id.awk
+++ b/awklib/eg/prog/id.awk
@@ -5,13 +5,14 @@
# Arnold Robbins, arnold@skeeve.com, Public Domain
# May 1993
# Revised February 1996
+# Revised May 2014
+# Revised September 2014
# output is:
# uid=12(foo) euid=34(bar) gid=3(baz) \
# egid=5(blat) groups=9(nine),2(two),1(one)
-BEGIN \
-{
+BEGIN {
uid = PROCINFO["uid"]
euid = PROCINFO["euid"]
gid = PROCINFO["gid"]
@@ -19,34 +20,22 @@ BEGIN \
printf("uid=%d", uid)
pw = getpwuid(uid)
- if (pw != "") {
- split(pw, a, ":")
- printf("(%s)", a[1])
- }
+ pr_first_field(pw)
if (euid != uid) {
printf(" euid=%d", euid)
pw = getpwuid(euid)
- if (pw != "") {
- split(pw, a, ":")
- printf("(%s)", a[1])
- }
+ pr_first_field(pw)
}
printf(" gid=%d", gid)
pw = getgrgid(gid)
- if (pw != "") {
- split(pw, a, ":")
- printf("(%s)", a[1])
- }
+ pr_first_field(pw)
if (egid != gid) {
printf(" egid=%d", egid)
pw = getgrgid(egid)
- if (pw != "") {
- split(pw, a, ":")
- printf("(%s)", a[1])
- }
+ pr_first_field(pw)
}
for (i = 1; ("group" i) in PROCINFO; i++) {
@@ -55,13 +44,18 @@ BEGIN \
group = PROCINFO["group" i]
printf("%d", group)
pw = getgrgid(group)
- if (pw != "") {
- split(pw, a, ":")
- printf("(%s)", a[1])
- }
+ pr_first_field(pw)
if (("group" (i+1)) in PROCINFO)
printf(",")
}
print ""
}
+
+function pr_first_field(str, a)
+{
+ if (str != "") {
+ split(str, a, ":")
+ printf("(%s)", a[1])
+ }
+}
diff --git a/awklib/eg/prog/igawk.sh b/awklib/eg/prog/igawk.sh
index 03d1c996..70edf606 100644
--- a/awklib/eg/prog/igawk.sh
+++ b/awklib/eg/prog/igawk.sh
@@ -115,7 +115,7 @@ BEGIN {
}
fpath = pathto($2)
if (fpath == "") {
- printf("igawk:%s:%d: cannot find %s\n",
+ printf("igawk: %s:%d: cannot find %s\n",
input[stackptr], FNR, $2) > "/dev/stderr"
continue
}
diff --git a/awklib/eg/prog/indirectcall.awk b/awklib/eg/prog/indirectcall.awk
index 3ecb2887..165b022a 100644
--- a/awklib/eg/prog/indirectcall.awk
+++ b/awklib/eg/prog/indirectcall.awk
@@ -27,7 +27,7 @@ function do_sort(first, last, compare, data, i, retval)
retval = data[1]
for (i = 2; i in data; i++)
retval = retval " " data[i]
-
+
return retval
}
# sort --- sort the data in ascending order and return it as a string
diff --git a/awklib/eg/prog/labels.awk b/awklib/eg/prog/labels.awk
index abf53c3b..3195809b 100644
--- a/awklib/eg/prog/labels.awk
+++ b/awklib/eg/prog/labels.awk
@@ -48,7 +48,6 @@ function printpage( i, j)
Count++
}
-END \
-{
+END {
printpage()
}
diff --git a/awklib/eg/prog/pi.awk b/awklib/eg/prog/pi.awk
new file mode 100644
index 00000000..3297beff
--- /dev/null
+++ b/awklib/eg/prog/pi.awk
@@ -0,0 +1,18 @@
+# pi.awk --- compute the digits of pi
+#
+# Katie Wasserman, katie@wass.net
+# August 2014
+
+BEGIN {
+ digits = 100000
+ two = 2 * 10 ^ digits
+ pi = two
+ for (m = digits * 4; m > 0; --m) {
+ d = m * 2 + 1
+ x = pi * m
+ div(x, d, result)
+ pi = result["quotient"]
+ pi = pi + two
+ }
+ print pi
+}
diff --git a/awklib/eg/prog/split.awk b/awklib/eg/prog/split.awk
index c907530b..b878fa50 100644
--- a/awklib/eg/prog/split.awk
+++ b/awklib/eg/prog/split.awk
@@ -4,8 +4,9 @@
#
# Arnold Robbins, arnold@skeeve.com, Public Domain
# May 1993
+# Revised slightly, May 2014
-# usage: split [-num] [file] [outname]
+# usage: split [-count] [file] [outname]
BEGIN {
outfile = "x" # default
@@ -14,14 +15,14 @@ BEGIN {
usage()
i = 1
- if (ARGV[i] ~ /^-[[:digit:]]+$/) {
+ if (i in ARGV && ARGV[i] ~ /^-[[:digit:]]+$/) {
count = -ARGV[i]
ARGV[i] = ""
i++
}
# test argv in case reading from stdin instead of file
if (i in ARGV)
- i++ # skip data file name
+ i++ # skip datafile name
if (i in ARGV) {
outfile = ARGV[i]
ARGV[i] = ""
@@ -49,9 +50,8 @@ BEGIN {
}
print > out
}
-function usage( e)
+function usage()
{
- e = "usage: split [-num] [file] [outname]"
- print e > "/dev/stderr"
+ print("usage: split [-num] [file] [outname]") > "/dev/stderr"
exit 1
}
diff --git a/awklib/eg/prog/tee.awk b/awklib/eg/prog/tee.awk
index 639b9f80..fd9985f1 100644
--- a/awklib/eg/prog/tee.awk
+++ b/awklib/eg/prog/tee.awk
@@ -7,8 +7,7 @@
# May 1993
# Revised December 1995
-BEGIN \
-{
+BEGIN {
for (i = 1; i < ARGC; i++)
copy[i] = ARGV[i]
@@ -35,8 +34,7 @@ BEGIN \
print > copy[i]
print
}
-END \
-{
+END {
for (i in copy)
close(copy[i])
}
diff --git a/awklib/eg/prog/uniq.awk b/awklib/eg/prog/uniq.awk
index 990387ac..7dd16099 100644
--- a/awklib/eg/prog/uniq.awk
+++ b/awklib/eg/prog/uniq.awk
@@ -5,10 +5,9 @@
# Arnold Robbins, arnold@skeeve.com, Public Domain
# May 1993
-function usage( e)
+function usage()
{
- e = "Usage: uniq [-udc [-n]] [+n] [ in [ out ]]"
- print e > "/dev/stderr"
+ print("Usage: uniq [-udc [-n]] [+n] [ in [ out ]]") > "/dev/stderr"
exit 1
}
@@ -18,8 +17,7 @@ function usage( e)
# -n skip n fields
# +n skip n characters, skip fields first
-BEGIN \
-{
+BEGIN {
count = 1
outputfile = "/dev/stdout"
opts = "udc0:1:2:3:4:5:6:7:8:9:"
@@ -31,7 +29,7 @@ BEGIN \
else if (c == "c")
do_count++
else if (index("0123456789", c) != 0) {
- # getopt requires args to options
+ # getopt() requires args to options
# this messes us up for things like -5
if (Optarg ~ /^[[:digit:]]+$/)
fcount = (c Optarg) + 0