summaryrefslogtreecommitdiff
path: root/function.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2012-01-29 18:12:22 +0000
committerPaul Smith <psmith@gnu.org>2012-01-29 18:12:22 +0000
commitfca11f60390cf607f68b497c3909b1fb40251070 (patch)
treee8541f1110fab2d1ffc0c4b226405d85ad43c0ad /function.c
parentd6e1c6e6c5961b8e7dee7ff1fc7d1792ab0fb4f8 (diff)
downloadgunmake-fca11f60390cf607f68b497c3909b1fb40251070.tar.gz
Create a new function $(file ...)
Diffstat (limited to 'function.c')
-rw-r--r--function.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/function.c b/function.c
index 29b106f..5acbb76 100644
--- a/function.c
+++ b/function.c
@@ -2105,6 +2105,45 @@ func_realpath (char *o, char **argv, const char *funcname UNUSED)
}
static char *
+func_file (char *o, char **argv, const char *funcname UNUSED)
+{
+ char *fn = argv[0];
+
+ if (fn[0] == '>')
+ {
+ FILE *fp;
+ const char *mode = "w";
+
+ /* We are writing a file. */
+ ++fn;
+ if (fn[0] == '>')
+ {
+ mode = "a";
+ ++fn;
+ }
+ fn = next_token (fn);
+
+ fp = fopen (fn, mode);
+ if (fp == NULL)
+ fatal (reading_file, _("open: %s: %s"), fn, strerror (errno));
+ else
+ {
+ int l = strlen (argv[1]);
+ int nl = (l == 0 || argv[1][l-1] != '\n');
+
+ if (fputs (argv[1], fp) == EOF || (nl && fputc('\n', fp) == EOF))
+ fatal (reading_file, _("write: %s: %s"), fn, strerror (errno));
+
+ fclose (fp);
+ }
+ }
+ else
+ fatal (reading_file, _("Invalid file operation: %s"), fn);
+
+ return o;
+}
+
+static char *
func_abspath (char *o, char **argv, const char *funcname UNUSED)
{
/* Expand the argument. */
@@ -2191,6 +2230,7 @@ static struct function_table_entry function_table_init[] =
{ STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
{ STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
{ STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
+ { STRING_SIZE_TUPLE("file"), 1, 2, 1, func_file},
#ifdef EXPERIMENTAL
{ STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
{ STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},