aboutsummaryrefslogtreecommitdiffstats
path: root/extension/readfile2.c
diff options
context:
space:
mode:
Diffstat (limited to 'extension/readfile2.c')
-rw-r--r--extension/readfile2.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/extension/readfile2.c b/extension/readfile2.c
new file mode 100644
index 00000000..4d8cd245
--- /dev/null
+++ b/extension/readfile2.c
@@ -0,0 +1,116 @@
+/*
+ * readfile.c - Read an entire file into a string.
+ *
+ * Arnold Robbins
+ * Tue Apr 23 17:43:30 IDT 2002
+ * Revised per Peter Tillier
+ * Mon Jun 9 17:05:11 IDT 2003
+ * Revised for new dynamic function facilities
+ * Mon Jun 14 14:53:07 IDT 2004
+ */
+
+/*
+ * Copyright (C) 2002, 2003, 2004, 2011 the Free Software Foundation, Inc.
+ *
+ * This file is part of GAWK, the GNU implementation of the
+ * AWK Programming Language.
+ *
+ * GAWK is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GAWK is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#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 "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 int
+do_readfile(int nargs)
+{
+ const awk_value_t *filename;
+ awk_value_t result;
+ double ret = -1;
+ struct stat sbuf;
+ char *text;
+ int fd;
+
+ if (do_lint && nargs > 1)
+ lintwarn(ext_id, "readfile: called with too many arguments");
+
+ filename = get_curfunc_param(0, AWK_PARAM_STRING);
+ if (filename != NULL) {
+ ret = stat(filename->str_value.str, & sbuf);
+ if (ret < 0) {
+ update_ERRNO_int(errno);
+ goto done;
+ } else if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
+ errno = EINVAL;
+ ret = -1;
+ update_ERRNO_int(errno);
+ goto done;
+ }
+
+ if ((fd = open(filename->str_value.str, O_RDONLY|O_BINARY)) < 0) {
+ ret = -1;
+ update_ERRNO_int(errno);
+ goto done;
+ }
+
+ emalloc(text, char *, sbuf.st_size + 2, "do_readfile");
+ memset(text, '\0', sbuf.st_size + 2);
+
+ if ((ret = read(fd, text, sbuf.st_size)) != sbuf.st_size) {
+ (void) close(fd);
+ ret = -1;
+ update_ERRNO_int(errno);
+ goto done;
+ }
+
+ close(fd);
+ set_return_value(make_string(text, sbuf.st_size));
+ return 1;
+ } else if (do_lint)
+ lintwarn(ext_id, "readfile: called with no arguments");
+
+
+done:
+ /* Set the return value */
+ set_return_value(make_number(ret));
+ return 0;
+}
+
+static awk_ext_func_t func_table[] = {
+ { "readfile", do_readfile, 1 },
+};
+
+/* define the dl_load function using the boilerplate macro */
+
+dl_load_func(api, ext_id, func_table, readfile, "")