From fca11f60390cf607f68b497c3909b1fb40251070 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 29 Jan 2012 18:12:22 +0000 Subject: Create a new function $(file ...) --- function.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'function.c') diff --git a/function.c b/function.c index 29b106f..5acbb76 100644 --- a/function.c +++ b/function.c @@ -2104,6 +2104,45 @@ func_realpath (char *o, char **argv, const char *funcname UNUSED) return o; } +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) { @@ -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}, -- cgit v1.2.3