gawk: Using Internal File Ops

1 
1 16.6.3 Integrating the Extensions
1 ---------------------------------
1 
1 Now that the code is written, it must be possible to add it at runtime
1 to the running 'gawk' interpreter.  First, the code must be compiled.
1 Assuming that the functions are in a file named 'filefuncs.c', and IDIR
1 is the location of the 'gawkapi.h' header file, the following steps(1)
1 create a GNU/Linux shared library:
1 
1      $ gcc -fPIC -shared -DHAVE_CONFIG_H -c -O -g -IIDIR filefuncs.c
1      $ gcc -o filefuncs.so -shared filefuncs.o
1 
1    Once the library exists, it is loaded by using the '@load' keyword:
1 
1      # file testff.awk
1      @load "filefuncs"
1 
1      BEGIN {
1          "pwd" | getline curdir  # save current directory
1          close("pwd")
1 
1          chdir("/tmp")
1          system("pwd")   # test it
1          chdir(curdir)   # go back
1 
1          print "Info for testff.awk"
1          ret = stat("testff.awk", data)
1          print "ret =", ret
1          for (i in data)
1              printf "data[\"%s\"] = %s\n", i, data[i]
1          print "testff.awk modified:",
1              strftime("%m %d %Y %H:%M:%S", data["mtime"])
1 
1          print "\nInfo for JUNK"
1          ret = stat("JUNK", data)
1          print "ret =", ret
1          for (i in data)
1              printf "data[\"%s\"] = %s\n", i, data[i]
1          print "JUNK modified:", strftime("%m %d %Y %H:%M:%S", data["mtime"])
1      }
1 
1    The 'AWKLIBPATH' environment variable tells 'gawk' where to find
1 extensions (⇒Finding Extensions).  We set it to the current
1 directory and run the program:
1 
1      $ AWKLIBPATH=$PWD gawk -f testff.awk
1      -| /tmp
1      -| Info for testff.awk
1      -| ret = 0
1      -| data["blksize"] = 4096
1      -| data["devbsize"] = 512
1      -| data["mtime"] = 1412004710
1      -| data["mode"] = 33204
1      -| data["type"] = file
1      -| data["dev"] = 2053
1      -| data["gid"] = 1000
1      -| data["ino"] = 10358899
1      -| data["ctime"] = 1412004710
1      -| data["blocks"] = 8
1      -| data["nlink"] = 1
1      -| data["name"] = testff.awk
1      -| data["atime"] = 1412004716
1      -| data["pmode"] = -rw-rw-r--
1      -| data["size"] = 666
1      -| data["uid"] = 1000
1      -| testff.awk modified: 09 29 2014 18:31:50
1      -|
1      -| Info for JUNK
1      -| ret = -1
1      -| JUNK modified: 01 01 1970 02:00:00
1 
1    ---------- Footnotes ----------
1 
1    (1) In practice, you would probably want to use the GNU Autotools
1 (Automake, Autoconf, Libtool, and 'gettext') to configure and build your
1 libraries.  Instructions for doing so are beyond the scope of this Info
1 file.  ⇒gawkextlib for Internet links to the tools.
1