manpagez: man pages & more
info gawk
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

C.3.3.3 Integrating the Extensions

Now that the code is written, it must be possible to add it at runtime to the running gawk interpreter. First, the code must be compiled. Assuming that the functions are in a file named ‘filefuncs.c’, and idir is the location of the gawk include files, the following steps create a GNU/Linux shared library:

 
$ gcc -fPIC -shared -DHAVE_CONFIG_H -c -O -g -Iidir filefuncs.c
$ ld -o filefuncs.so -shared filefuncs.o

Once the library exists, it is loaded by calling the extension() built-in function. This function takes two arguments: the name of the library to load and the name of a function to call when the library is first loaded. This function adds the new functions to gawk. It returns the value returned by the initialization function within the shared library:

 
# file testff.awk
BEGIN {
    extension("./filefuncs.so", "dlload")

    chdir(".")  # no-op

    data[1] = 1 # force `data' to be an array
    print "Info for testff.awk"
    ret = stat("testff.awk", data)
    print "ret =", ret
    for (i in data)
        printf "data[\"%s\"] = %s\n", i, data[i]
    print "testff.awk modified:",
        strftime("%m %d %y %H:%M:%S", data["mtime"])

    print "\nInfo for JUNK"
    ret = stat("JUNK", data)
    print "ret =", ret
    for (i in data)
        printf "data[\"%s\"] = %s\n", i, data[i]
    print "JUNK modified:", strftime("%m %d %y %H:%M:%S", data["mtime"])
}

Here are the results of running the program:

 
$ gawk -f testff.awk
-| Info for testff.awk
-| ret = 0
-| data["size"] = 607
-| data["ino"] = 14945891
-| data["name"] = testff.awk
-| data["pmode"] = -rw-rw-r--
-| data["nlink"] = 1
-| data["atime"] = 1293993369
-| data["mtime"] = 1288520752
-| data["mode"] = 33204
-| data["blksize"] = 4096
-| data["dev"] = 2054
-| data["type"] = file
-| data["gid"] = 500
-| data["uid"] = 500
-| data["blocks"] = 8
-| data["ctime"] = 1290113572
-| testff.awk modified: 10 31 10 12:25:52
-| 
-| Info for JUNK
-| ret = -1
-| JUNK modified: 01 01 70 02:00:00

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.