aboutsummaryrefslogtreecommitdiffstats
path: root/extension/readfile.c
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2012-05-24 15:34:17 -0400
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2012-05-24 15:34:17 -0400
commit577c3fc31a2718461fba2e599d162de96fe838fa (patch)
tree1f45346053a5ae9db3c62761ef214a8a94095d30 /extension/readfile.c
parentc62b9d773bc064bc1dd5d8db35207fd4e6d42f1e (diff)
downloadegawk-577c3fc31a2718461fba2e599d162de96fe838fa.tar.gz
egawk-577c3fc31a2718461fba2e599d162de96fe838fa.tar.bz2
egawk-577c3fc31a2718461fba2e599d162de96fe838fa.zip
First working version of new API mechanism (probably has memory leaks).
Diffstat (limited to 'extension/readfile.c')
-rw-r--r--extension/readfile.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/extension/readfile.c b/extension/readfile.c
index 17e70a6d..639c897a 100644
--- a/extension/readfile.c
+++ b/extension/readfile.c
@@ -30,34 +30,43 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
-#include "awk.h"
+#include <stdio.h>
+#include <errno.h>
#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "config.h"
+#include "gawkapi.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
+static const gawk_api_t *api; /* for convenience macros to work */
+static awk_ext_id_t *ext_id;
+
int plugin_is_GPL_compatible;
/* do_readfile --- read a file into memory */
-static NODE *
-do_readfile(int nargs)
+static awk_value_t *
+do_readfile(int nargs, awk_value_t *result)
{
- NODE *filename;
- int ret = -1;
+ awk_value_t filename;
+ double ret = -1;
struct stat sbuf;
char *text;
int fd;
if (do_lint && nargs > 1)
- lintwarn("readfile: called with too many arguments");
-
- filename = get_scalar_argument(0, false);
- if (filename != NULL) {
- (void) force_string(filename);
+ lintwarn(ext_id, "readfile: called with too many arguments");
- ret = stat(filename->stptr, & sbuf);
+ if (get_curfunc_param(0, AWK_STRING, &filename) != NULL) {
+ ret = stat(filename.str_value.str, & sbuf);
if (ret < 0) {
update_ERRNO_int(errno);
goto done;
@@ -68,7 +77,7 @@ do_readfile(int nargs)
goto done;
}
- if ((fd = open(filename->stptr, O_RDONLY|O_BINARY)) < 0) {
+ if ((fd = open(filename.str_value.str, O_RDONLY|O_BINARY)) < 0) {
ret = -1;
update_ERRNO_int(errno);
goto done;
@@ -85,25 +94,20 @@ do_readfile(int nargs)
}
close(fd);
- return make_string(text, sbuf.st_size);
+ return make_string(text, sbuf.st_size, result);
} else if (do_lint)
- lintwarn("filename: called with no arguments");
+ lintwarn(ext_id, "readfile: called with no arguments");
done:
/* Set the return value */
- return make_number((AWKNUM) ret);
+ return make_number(ret, result);
}
+static awk_ext_func_t func_table[] = {
+ { "readfile", do_readfile, 1 },
+};
-/* dlload --- load new builtins in this library */
+/* define the dl_load function using the boilerplate macro */
-NODE *
-dlload(tree, dl)
-NODE *tree;
-void *dl;
-{
- make_builtin("readfile", do_readfile, 1);
-
- return make_number((AWKNUM) 0);
-}
+dl_load_func(func_table, readfile, "")